0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

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

* fix(i18n): use import.meta.env to deliver config to virtual module

* add changeset

* prevent destructing i18n config unless enabled

* use defined variable instead

* Update packages/astro/src/i18n/vite-plugin-i18n.ts

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Arsh 2024-01-29 15:43:14 +00:00 committed by GitHub
parent 00ba9f1947
commit 0a06d87a1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 26 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes an issue where `astro:i18n` could not be used in framework components.

View file

@ -4,8 +4,6 @@ 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;
@ -13,8 +11,9 @@ type AstroInternationalization = {
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,
@ -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)};`;
}
},
}
};
}

View file

@ -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;