Skip to content

Commit

Permalink
Silence i18n content collection warnings (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored Feb 6, 2024
1 parent 57bb14a commit 8c88642
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-moons-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Silences i18n content collection warnings for projects without custom translations.
3 changes: 1 addition & 2 deletions examples/basics/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -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() }),
};
3 changes: 1 addition & 2 deletions examples/tailwind/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -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() }),
};
29 changes: 18 additions & 11 deletions packages/starlight/utils/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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);

0 comments on commit 8c88642

Please sign in to comment.