Skip to content

fix: guard i18n against unregistered locale - #841

Open
NemeZZiZZ wants to merge 1 commit into
klinecharts:mainfrom
NemeZZiZZ:fix/i18n-unknown-locale-guard
Open

fix: guard i18n against unregistered locale#841
NemeZZiZZ wants to merge 1 commit into
klinecharts:mainfrom
NemeZZiZZ:fix/i18n-unknown-locale-guard

Conversation

@NemeZZiZZ

Copy link
Copy Markdown
Contributor

Problem

i18n() indexes the locale registry without a guard:

function i18n(key: string, locale: string): string {
  return locales[locale][key] ?? key   // locales[locale] === undefined → TypeError
}

Only en-US and zh-CN are registered by default, and Store.setLocale accepts any string without validation. So init(el, { locale: 'zh-TW' }) — or any typo — throws TypeError: Cannot read properties of undefined at the first tooltip render (CandleTooltipView.ts). The exception escapes from a canvas animation-frame callback (which has try/finally but no catch), leaving the frame half-drawn and the chart effectively broken from init.

Verified by execution against the real source: i18n('indicator.tooltip.open', 'zh-TW') → TypeError.

Fix

Two lines of defense:

  1. i18n() never throws: locales[locale]?.[key] ?? key — unknown locale degrades to the key itself, same as an unknown key already does.
  2. Store.setLocale() warns and falls back to en-US when the locale is not registered, so the chart still renders readable strings instead of raw keys.

registerLocale was already defensive ({ ...locales[locale], ...ls }); this makes the read side match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant