diff options
author | HiDeoo | 2025-07-16 13:07:37 +0200 |
---|---|---|
committer | GitHub | 2025-07-16 13:07:37 +0200 |
commit | 21fcd944d528557b89fc8b351579beabdcc06ff6 (patch) | |
tree | 8698126d9491731043b1e9dbc7bff5e8583ca471 | |
parent | 355cb3c3d9ed8fcae81d684bb52c62ef551f8543 (diff) | |
download | IT.starlight-21fcd944d528557b89fc8b351579beabdcc06ff6.tar.gz IT.starlight-21fcd944d528557b89fc8b351579beabdcc06ff6.tar.bz2 IT.starlight-21fcd944d528557b89fc8b351579beabdcc06ff6.zip |
Fix Astro i18n default locale regression (#3306)
4 files changed, 59 insertions, 1 deletions
diff --git a/.changeset/early-kings-fly.md b/.changeset/early-kings-fly.md new file mode 100644 index 00000000..ec0178a6 --- /dev/null +++ b/.changeset/early-kings-fly.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes a regression in Starlight version `0.34.5` that caused multilingual sites with a default locale explicitly set to `root` to report a configuration error. diff --git a/packages/starlight/__tests__/i18n-root-default-locale/i18n.test.ts b/packages/starlight/__tests__/i18n-root-default-locale/i18n.test.ts new file mode 100644 index 00000000..fb48dcf4 --- /dev/null +++ b/packages/starlight/__tests__/i18n-root-default-locale/i18n.test.ts @@ -0,0 +1,39 @@ +import { assert, describe, expect, test } from 'vitest'; +import config from 'virtual:starlight/user-config'; +import { processI18nConfig } from '../../utils/i18n'; + +describe('processI18nConfig', () => { + test('returns Astro i18n config for a multilingual site with a root default locale', () => { + const { astroI18nConfig, starlightConfig } = processI18nConfig(config, undefined); + + // The default locale matches the root locale's `lang` property. + expect(astroI18nConfig.defaultLocale).toBe('fr'); + expect(astroI18nConfig.locales).toMatchInlineSnapshot(` + [ + { + "codes": [ + "fr", + ], + "path": "fr", + }, + { + "codes": [ + "en-US", + ], + "path": "en", + }, + { + "codes": [ + "ar", + ], + "path": "ar", + }, + ] + `); + assert(typeof astroI18nConfig.routing !== 'string'); + expect(astroI18nConfig.routing?.prefixDefaultLocale).toBe(false); + + // The Starlight configuration should not be modified. + expect(config).toStrictEqual(starlightConfig); + }); +}); diff --git a/packages/starlight/__tests__/i18n-root-default-locale/vitest.config.ts b/packages/starlight/__tests__/i18n-root-default-locale/vitest.config.ts new file mode 100644 index 00000000..a677045a --- /dev/null +++ b/packages/starlight/__tests__/i18n-root-default-locale/vitest.config.ts @@ -0,0 +1,11 @@ +import { defineVitestConfig } from '../test-config'; + +export default defineVitestConfig({ + title: 'i18n with root default locale', + defaultLocale: 'root', + locales: { + root: { label: 'French', lang: 'fr' }, + en: { label: 'English', lang: 'en-US' }, + ar: { label: 'Arabic', dir: 'rtl' }, + }, +}); diff --git a/packages/starlight/utils/i18n.ts b/packages/starlight/utils/i18n.ts index 4b23c0a5..c212c7bf 100644 --- a/packages/starlight/utils/i18n.ts +++ b/packages/starlight/utils/i18n.ts @@ -57,7 +57,10 @@ function getAstroI18nConfig(config: StarlightConfig): NonNullable<AstroConfig['i // In Starlight, this matches the `locale` property if defined, and we fallback to the `lang` // property if not (which would be set to the language’s directory name by default). defaultLocale: - config.defaultLocale.locale ?? config.defaultLocale.lang ?? BuiltInDefaultLocale.lang, + // If the default locale is explicitly set to `root`, we use the `lang` property instead. + (config.defaultLocale.locale === 'root' + ? config.defaultLocale.lang + : (config.defaultLocale.locale ?? config.defaultLocale.lang)) ?? BuiltInDefaultLocale.lang, locales: config.locales ? Object.entries(config.locales).map(([locale, localeConfig]) => { return { |