From 7b9f27372c18a3737e83954ad303e94f74d62fa0 Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Wed, 3 May 2023 22:33:11 +0200 Subject: Enable fallback content for missing translations (#27) --- docs/src/content/docs/guides/i18n.md | 2 + docs/src/content/docs/reference/configuration.md | 12 +++ .../components/FallbackContentNotice.astro | 27 +++++ packages/starbook/components/HeadSEO.astro | 2 +- packages/starbook/components/LanguageSelect.astro | 2 +- packages/starbook/index.astro | 41 ++++---- packages/starbook/utils/navigation.ts | 100 +++++++++---------- packages/starbook/utils/routing.ts | 109 +++++++++++++++++++++ packages/starbook/utils/slugs.ts | 57 ++++++++--- packages/starbook/utils/user-config.ts | 53 +++++++++- 10 files changed, 311 insertions(+), 94 deletions(-) create mode 100644 packages/starbook/components/FallbackContentNotice.astro create mode 100644 packages/starbook/utils/routing.ts diff --git a/docs/src/content/docs/guides/i18n.md b/docs/src/content/docs/guides/i18n.md index 28c7cc10..a6f48ffc 100644 --- a/docs/src/content/docs/guides/i18n.md +++ b/docs/src/content/docs/guides/i18n.md @@ -16,6 +16,8 @@ StarBook provides built-in support for multilingual sites. export default defineConfig({ integrations: [ starbook({ + // Set English as the default language for this site. + defaultLocale: 'en', locales: { // English docs in `src/content/docs/en/` en: { diff --git a/docs/src/content/docs/reference/configuration.md b/docs/src/content/docs/reference/configuration.md index fa99e5f7..53ba8746 100644 --- a/docs/src/content/docs/reference/configuration.md +++ b/docs/src/content/docs/reference/configuration.md @@ -114,6 +114,8 @@ import starbook from 'starbook'; export default defineConfig({ integrations: [ starbook({ + // Set English as the default language for this site. + defaultLocale: 'en', locales: { // English docs in `src/content/docs/en/` en: { @@ -177,6 +179,16 @@ starbook({ For example, this allows you to serve `/getting-started/` as an English route and use `/fr/getting-started/` as the equivalent French page. +### `defaultLocale` + +**type:** `string` + +Set the language which is the default for this site. +The value should match one of the keys of your [`locales`](#locales) object. +(If your default language is your [root locale](#root-locale), you can skip this.) + +The default locale will be used to provide fallback content where translations are missing. + ### `social` Optional details about the social media accounts for this site. diff --git a/packages/starbook/components/FallbackContentNotice.astro b/packages/starbook/components/FallbackContentNotice.astro new file mode 100644 index 00000000..e4dc3a4a --- /dev/null +++ b/packages/starbook/components/FallbackContentNotice.astro @@ -0,0 +1,27 @@ +--- +import Icon from './Icon.astro'; +--- + +

+ This content is not available in your language yet. +

+ + diff --git a/packages/starbook/components/HeadSEO.astro b/packages/starbook/components/HeadSEO.astro index 2eafbc4f..9ebdde57 100644 --- a/packages/starbook/components/HeadSEO.astro +++ b/packages/starbook/components/HeadSEO.astro @@ -22,7 +22,7 @@ const description = data.description || config.description; { canonical && - config.locales && + config.isMultilingual && Object.entries(config.locales).map( ([locale, localeOpts]) => localeOpts && ( diff --git a/packages/starbook/components/LanguageSelect.astro b/packages/starbook/components/LanguageSelect.astro index 04343c1f..65d978e2 100644 --- a/packages/starbook/components/LanguageSelect.astro +++ b/packages/starbook/components/LanguageSelect.astro @@ -16,7 +16,7 @@ function localizedPathname(locale: string | undefined): string { --- { - config.locales && Object.keys(config.locales).length > 1 && ( + config.isMultilingual && (