inula/packages/max/src/plugins/plugin-intl/templates/localeExports.tpl

129 lines
3.3 KiB
Smarty
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import EventEmitter from '{{{EventEmitterPkg}}}';
const useLocalStorage = {{{UseLocalStorage}}};
// @ts-ignore
export const event = new EventEmitter();
export const LANG_CHANGE_EVENT = Symbol('LANG_CHANGE');
{{#LocaleList}}
{{#paths}}
import lang_{{lang}}{{country}}{{index}} from "{{{path}}}";
{{/paths}}
{{/LocaleList}}
const flattenMessages=(
nestedMessages: Record<string, any>,
prefix = '',
) => {
return Object.keys(nestedMessages).reduce(
(messages: Record<string, any>, key) => {
const value = nestedMessages[key];
const prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
},
{},
);
}
export const localeInfo: {[key: string]: any} = {
{{#LocaleList}}
'{{name}}': {
{{#paths}}...flattenMessages(lang_{{lang}}{{country}}{{index}}),{{/paths}}
},
{{/LocaleList}}
};
/**
* 增加一个新的国际化语言
* @param name 语言的 key
* @param messages 对应的枚举对象
*/
export const addLocale = (
name: string,
messages: Object,
) => {
if (!name) {
return;
}
//
const mergeMessages = localeInfo[name]
? Object.assign({}, localeInfo[name], messages)
: messages;
localeInfo[name] = mergeMessages;
// name locale
if (name === getLocale()) {
event.emit(LANG_CHANGE_EVENT, name);
}
};
/**
* 获取当前选择的语言
* @returns string
*/
export const getLocale = () => {
// because changing will break the app
const lang =
navigator.cookieEnabled && typeof localStorage !== 'undefined' && useLocalStorage
? window.localStorage.getItem('inula_locale')
: '';
// support baseNavigator, default true
let browserLang;
{{#BaseNavigator}}
const isNavigatorLanguageValid =
typeof navigator !== 'undefined' && typeof navigator.language === 'string';
browserLang = isNavigatorLanguageValid
? navigator.language
: '';
{{/BaseNavigator}}
return lang || browserLang || {{{DefaultLocale}}};
};
/**
* 切换语言
* @param lang 语言的 key
* @param realReload 是否刷新页面,默认刷新
* @returns string
*/
export const setLocale = (lang: string, realReload: boolean = true) => {
//const { pluginManager } = useAppContext();
//const runtimeLocale = pluginManagerapplyPlugins({
// key: 'locale',
// workaround: 使 ApplyPluginsType.modify fast-refresh
// type: 'modify',
// initialValue: {},
//});
const updater = () => {
if (getLocale() !== lang) {
if (navigator.cookieEnabled && typeof window.localStorage !== 'undefined' && useLocalStorage) {
window.localStorage.setItem('inula_locale', lang || '');
}
if (realReload) {
window.location.reload();
} else {
event.emit(LANG_CHANGE_EVENT, lang);
// chrome
if (window.dispatchEvent) {
const event = new Event('languagechange');
window.dispatchEvent(event);
}
}
}
}
updater();
};
/**
* 获取语言列表
* @returns string[]
*/
export const getAllLocales = () => Object.keys(localeInfo);