Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): use define to deliver config to virtual module #9838

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-buckets-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where `astro:i18n` could not be used in framework components.
36 changes: 13 additions & 23 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';

const virtualModuleId = 'astro:i18n';
const configId = 'astro-internal:i18n-config';
const resolvedConfigId = `\0${configId}`;

type AstroInternationalization = {
settings: AstroSettings;
};

export interface I18nInternalConfig
extends Pick<AstroConfig, 'base' | 'site' | 'trailingSlash'>,
NonNullable<AstroConfig['i18n']>,
Pick<AstroConfig['build'], 'format'> {}
Pick<AstroConfig['build'], 'format'> {
i18n: AstroConfig['i18n'];
}

export default function astroInternationalization({
settings,
Expand All @@ -29,28 +28,19 @@ export default function astroInternationalization({
return {
name: 'astro:i18n',
enforce: 'pre',
async resolveId(id) {
config(config) {
const i18nConfig: I18nInternalConfig = { base, format, site, trailingSlash, i18n };
return {
define: {
__ASTRO_INTERNAL_I18N_CONFIG__: JSON.stringify(i18nConfig)
}
}
},
resolveId(id) {
if (id === virtualModuleId) {
if (i18n === undefined) throw new AstroError(AstroErrorData.i18nNotEnabled);
return this.resolve('astro/virtual-modules/i18n.js');
}
if (id === configId) return resolvedConfigId;
},
load(id) {
if (id === resolvedConfigId) {
const { defaultLocale, locales, routing, fallback } = i18n!;
const config: I18nInternalConfig = {
base,
format,
site,
trailingSlash,
defaultLocale,
locales,
routing,
fallback,
};
return `export default ${JSON.stringify(config)};`;
}
},
}
};
}
6 changes: 3 additions & 3 deletions packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as I18nInternals from '../i18n/index.js';
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';

// @ts-expect-error
import config from 'astro-internal:i18n-config';
const { trailingSlash, format, site, defaultLocale, locales, routing } =
config as I18nInternalConfig;
const { trailingSlash, format, site, i18n } = __ASTRO_INTERNAL_I18N_CONFIG__ as I18nInternalConfig;
const { defaultLocale, locales, routing } = i18n!;
const base = import.meta.env.BASE_URL;

export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
Expand Down
Loading