Skip to content

Commit

Permalink
fix(i18n): use define to deliver config to virtual module (#9838)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
lilnasy and bluwy committed Jan 29, 2024
1 parent 00ba9f1 commit 0a06d87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
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

0 comments on commit 0a06d87

Please sign in to comment.