Skip to content

Commit

Permalink
feat: support get locale messages from global (#6898)
Browse files Browse the repository at this point in the history
* feat: support get locale messages from global

* fix: support option of useCDN
  • Loading branch information
ClarkXia authored May 29, 2024
1 parent b808156 commit d06826e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
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;

0 comments on commit d06826e

Please sign in to comment.