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

feat: support get locale messages from global #6898

Merged
merged 2 commits into from
May 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/thirty-islands-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/plugin-intl': patch
---

feat: support get locale messages for global
26 changes: 23 additions & 3 deletions packages/plugin-intl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ import type { Plugin } from '@ice/app/types';

const _dirname = path.dirname(fileURLToPath(import.meta.url));

const plugin: Plugin = () => ({
interface PluginOptions {
// The key of locale content read from the window object.
localeMessagesKey?: string;
defaultLocaleKey?: string;
useCDN?: boolean;
}

const plugin: Plugin<PluginOptions> = ({
localeMessagesKey = '__LOCALE_MESSAGES__',
defaultLocaleKey = '__DEFAULT_LOCALE__',
useCDN = false,
} = {}) => ({
name: 'plugin-intl',
setup: ({ generator, context, createLogger, watch }) => {
const { rootDir } = context;
Expand All @@ -26,6 +37,8 @@ const plugin: Plugin = () => ({
path.join(_dirname, '../templates/locales.ts.ejs'),
'locales.ts',
{
localeMessagesKey,
defaultLocaleKey,
localeImport: locales.join('\n'),
localeExport: localeExport.join('\n '),
},
Expand All @@ -34,7 +47,7 @@ const plugin: Plugin = () => ({
const globRule = 'src/locales/*.{ts,js,json}';
// Glob all locale files, and generate runtime options.
const localeFiles = fg.sync(globRule, { cwd: rootDir });
if (localeFiles.length > 0) {
if (localeFiles.length > 0 && !useCDN) {
// Filter the entry of locale files.
const mainEntry = localeFiles.find((file) => file.match(/index\.(ts|js|json)$/));
let runtimeSource = '';
Expand All @@ -59,7 +72,14 @@ const plugin: Plugin = () => ({
}, 'both');
}
} else {
logger.warn('No locale files found, please check the `src/locales` folder.');
renderLocaleEntry([]);
generator.addEntryImportAhead({
source: './locales',
// @ts-ignore
}, 'both');
if (!useCDN) {
logger.warn('No locale files found, please check the `src/locales` folder.');
}
}

// Add intl export from ice.
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-intl/src/runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const EXPORT_NAME = 'locale';
const cache = createIntlCache();

const getDefaultLocale = () => {
return (typeof navigator !== 'undefined' && navigator.language) || 'zh-CN';
// @ts-ignore
return (typeof window !== 'undefined' && window.__ICE_DEFAULT_LOCALE__) ||
(typeof navigator !== 'undefined' && navigator.language) ||
'zh-CN';
};

const getLocaleMessages = () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-intl/templates/locales.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
const localeMessages = {
<%- localeExport %>
};
const LOCALE_KEY = '__ICE_LOCALE_MESSAGES__';
const LOCALE_MESSAGES_KEY = '__ICE_LOCALE_MESSAGES__';
const DEFAULT_LOCALE_KEY = '__ICE_DEFAULT_LOCALE__';
if (typeof window !== 'undefined') {
window[LOCALE_KEY] = localeMessages;
window[LOCALE_MESSAGES_KEY] = window['<%- localeMessagesKey %>'] || localeMessages;
window[DEFAULT_LOCALE_KEY] = window['<%- defaultLocaleKey %>'];
} else {
global[LOCALE_KEY]= localeMessages;
global[LOCALE_MESSAGES_KEY]= localeMessages;
}

export default localeMessages;
Loading