summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Swithinbank2024-02-06 16:08:41 +0100
committerGitHub2024-02-06 16:08:41 +0100
commit8c88642875e8344396074a780e28fb0860b249f8 (patch)
tree33e6493abbef5713a1273048c2bc888998daa51e
parent57bb14afe4e3be058766adb85d56ec17e93ebae3 (diff)
downloadIT.starlight-8c88642875e8344396074a780e28fb0860b249f8.tar.gz
IT.starlight-8c88642875e8344396074a780e28fb0860b249f8.tar.bz2
IT.starlight-8c88642875e8344396074a780e28fb0860b249f8.zip
Silence i18n content collection warnings (#1458)
-rw-r--r--.changeset/tricky-moons-admire.md5
-rw-r--r--examples/basics/src/content/config.ts3
-rw-r--r--examples/tailwind/src/content/config.ts3
-rw-r--r--packages/starlight/utils/translations.ts29
4 files changed, 25 insertions, 15 deletions
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<string, i18nSchemaOutput> = {};
-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<string, i18nSchemaOutput> = {};
+ // 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);