From 8c88642875e8344396074a780e28fb0860b249f8 Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Tue, 6 Feb 2024 16:08:41 +0100 Subject: Silence i18n content collection warnings (#1458) --- .changeset/tricky-moons-admire.md | 5 +++++ examples/basics/src/content/config.ts | 3 +-- examples/tailwind/src/content/config.ts | 3 +-- packages/starlight/utils/translations.ts | 29 ++++++++++++++++++----------- 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .changeset/tricky-moons-admire.md diff --git a/.changeset/tricky-moons-admire.md b/.changeset/tricky-moons-admire.md new file mode 100644 index 00000000..1752a89e --- /dev/null +++ b/.changeset/tricky-moons-admire.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Silences i18n content collection warnings for projects without custom translations. diff --git a/examples/basics/src/content/config.ts b/examples/basics/src/content/config.ts index 9df91b60..45f60b01 100644 --- a/examples/basics/src/content/config.ts +++ b/examples/basics/src/content/config.ts @@ -1,7 +1,6 @@ import { defineCollection } from 'astro:content'; -import { docsSchema, i18nSchema } from '@astrojs/starlight/schema'; +import { docsSchema } from '@astrojs/starlight/schema'; export const collections = { docs: defineCollection({ schema: docsSchema() }), - i18n: defineCollection({ type: 'data', schema: i18nSchema() }), }; diff --git a/examples/tailwind/src/content/config.ts b/examples/tailwind/src/content/config.ts index 9df91b60..45f60b01 100644 --- a/examples/tailwind/src/content/config.ts +++ b/examples/tailwind/src/content/config.ts @@ -1,7 +1,6 @@ import { defineCollection } from 'astro:content'; -import { docsSchema, i18nSchema } from '@astrojs/starlight/schema'; +import { docsSchema } from '@astrojs/starlight/schema'; export const collections = { docs: defineCollection({ schema: docsSchema() }), - i18n: defineCollection({ type: 'data', schema: i18nSchema() }), }; diff --git a/packages/starlight/utils/translations.ts b/packages/starlight/utils/translations.ts index f1279abc..6c262938 100644 --- a/packages/starlight/utils/translations.ts +++ b/packages/starlight/utils/translations.ts @@ -3,16 +3,23 @@ import config from 'virtual:starlight/user-config'; import type { i18nSchemaOutput } from '../schemas/i18n'; import { createTranslationSystem } from './createTranslationSystem'; -/** All translation data from the i18n collection, keyed by `id`, which matches locale. */ -let userTranslations: Record = {}; -try { - // Load the user’s i18n collection and ignore the error if it doesn’t exist. - userTranslations = Object.fromEntries( - // @ts-ignore — may be an error in projects without an i18n collection - - (await getCollection('i18n')).map(({ id, data }) => [id, data] as const) - ); -} catch {} +/** Get all translation data from the i18n collection, keyed by `id`, which matches locale. */ +async function loadTranslations() { + let userTranslations: Record = {}; + // Briefly override `console.warn()` to silence logging when a project has no i18n collection. + const warn = console.warn; + console.warn = () => {}; + try { + // Load the user’s i18n collection and ignore the error if it doesn’t exist. + userTranslations = Object.fromEntries( + // @ts-ignore — may be an error in projects without an i18n collection + (await getCollection('i18n')).map(({ id, data }) => [id, data] as const) + ); + } catch {} + // Restore the original warn implementation. + console.warn = warn; + return userTranslations; +} /** * Generate a utility function that returns UI strings for the given `locale`. @@ -21,4 +28,4 @@ try { * const t = useTranslations('en'); * const label = t('search.label'); // => 'Search' */ -export const useTranslations = createTranslationSystem(userTranslations, config); +export const useTranslations = createTranslationSystem(await loadTranslations(), config); -- cgit