Skip to content

Commit 458b492

Browse files
MilesChouclaude
andcommitted
fix: use region-specific translations for default locale instead of base language override
When a monolingual site uses a region-specific locale as the default (e.g., `zh-TW`), `buildResources` for the default locale was loading both `builtinTranslations['zh-TW']` and `builtinTranslations['zh']` as separate layers. Since `buildResources` applies later layers over earlier ones, the base language (`zh` → Simplified Chinese) was overwriting the region-specific translations (`zh-TW` → Traditional Chinese). This fix aligns the default locale behavior with the locales loop (line 37), using `||` so `stripLangRegion` result is only used as a fallback when the specific locale has no built-in translations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3ce793a commit 458b492

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

packages/starlight/__tests__/i18n-single-root-locale/translations-fs.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ describe('createTranslationSystemFromFs', () => {
2828
expect(t('page.editLink')).toMatchInlineSnapshot('"Changer cette page"');
2929
});
3030

31+
test('uses region-specific translations instead of base language fallback for default locale', async () => {
32+
const useTranslations = await createTranslationSystemFromFs(
33+
{
34+
locales: undefined,
35+
defaultLocale: { label: '繁體中文', lang: 'zh-TW', dir: 'ltr' },
36+
},
37+
// Using non-existent `_src/` to ignore custom files in this test fixture.
38+
{ srcDir: new URL('./_src/', import.meta.url) }
39+
);
40+
const t = useTranslations('zh-TW');
41+
// Should use zh-TW (Traditional Chinese) "搜尋", not zh (Simplified Chinese) "搜索"
42+
expect(t('search.label')).toBe('搜尋');
43+
});
44+
3145
test('returns translation for unknown language', async () => {
3246
const useTranslations = await createTranslationSystemFromFs(
3347
{

packages/starlight/utils/createTranslationSystem.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export async function createTranslationSystem<T extends i18nSchemaOutput>(
2222

2323
const translations = {
2424
[defaultLocale]: buildResources(
25-
builtinTranslations[defaultLocale],
26-
builtinTranslations[stripLangRegion(defaultLocale)],
25+
builtinTranslations[defaultLocale] || builtinTranslations[stripLangRegion(defaultLocale)],
2726
pluginTranslations[defaultLocale],
2827
userTranslations[defaultLocale]
2928
),

0 commit comments

Comments
 (0)