Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(sdk): locale plural forms are validated as strings at load

esenginee992f632f84e
diff --git a/sdk/src/i18n/Localization.ts b/sdk/src/i18n/Localization.ts
index 496f2466a..16e46e069 100644
--- a/sdk/src/i18n/Localization.ts
+++ b/sdk/src/i18n/Localization.ts
@@ -174,7 +174,17 @@ export function parseLocaleTable(text: string, path: string): LocaleTableAsset {
}
for (const [key, entry] of Object.entries(table.entries)) {
if (typeof entry === 'string') continue;
- if (typeof entry === 'object' && entry !== null && typeof (entry as PluralForms).other === 'string') continue;
+ if (typeof entry === 'object' && entry !== null && typeof (entry as PluralForms).other === 'string') {
+ // Every PROVIDED plural form must be a string template too, not just
+ // `other` — a non-string form (e.g. a stray number) passes here but
+ // throws later in interpolate when that category is selected.
+ let bad: string | null = null;
+ for (const [cat, v] of Object.entries(entry as unknown as Record<string, unknown>)) {
+ if (typeof v !== 'string') { bad = cat; break; }
+ }
+ if (bad === null) continue;
+ throw new Error(`${path}: entry '${key}' plural form '${bad}' must be a string`);
+ }
throw new Error(`${path}: entry '${key}' must be a string or plural forms with an 'other' catch-all`);
}
return { version: LOCALE_TABLE_VERSION, locale: table.locale, entries: table.entries };
diff --git a/sdk/tests/i18n.test.ts b/sdk/tests/i18n.test.ts
index e1de1ab87..f47c86335 100644
--- a/sdk/tests/i18n.test.ts
+++ b/sdk/tests/i18n.test.ts
@@ -6,6 +6,7 @@ import {
interpolate,
selectPluralForm,
defaultPluralSelector,
+ parseLocaleTable,
type PluralSelector,
} from '../src/i18n/Localization';
@@ -97,3 +98,24 @@ describe('LocalizationAPI', () => {
expect(loc.t('apples', { count: 1 })).toBe('1 apples'); // selector forces other
});
});
+
+describe('parseLocaleTable plural-form validation', () => {
+ const wrap = (entries: unknown) =>
+ JSON.stringify({ locale: 'en', entries });
+
+ it('accepts string entries and all-string plural forms', () => {
+ const t = parseLocaleTable(wrap({ a: 'x', b: { one: '{count} apple', other: '{count} apples' } }), 'p');
+ expect(t.locale).toBe('en');
+ });
+
+ it('rejects a non-string plural form (would throw later in interpolate)', () => {
+ // `other` is a valid string, but `one` is a number — must be caught at load.
+ expect(() => parseLocaleTable(wrap({ a: { one: 42, other: 'ok' } }), 'p'))
+ .toThrow(/plural form 'one' must be a string/);
+ });
+
+ it('still rejects plural forms missing the `other` catch-all', () => {
+ expect(() => parseLocaleTable(wrap({ a: { one: 'x' } }), 'p'))
+ .toThrow(/must be a string or plural forms/);
+ });
+});