UNPKG

1.04 kBJavaScriptView Raw
1/* global __KOOT_SSR__:false, __KOOT_SSR_STATE__:false */
2
3/**
4 * 根据当前环境,返回语言包对象的引用
5 * - 客户端: 当前语种的语言包对象 (仅当多语言类型为 `redux` 时)
6 * - 服务器端: 所有语种语言包合集对象
7 * @returns {Object}
8 */
9const getLocalesObject = () => {
10 if (__SERVER__) {
11 if (typeof __KOOT_SSR__ === 'object')
12 return __KOOT_SSR__.locales
13 }
14 if (__CLIENT__) {
15 if (typeof __KOOT_SSR_STATE__ === 'object') {
16 return __KOOT_SSR_STATE__.locales
17 }
18 }
19 return false
20}
21
22/**
23 * @type {Object}
24 * 语言包对象
25 * - 客户端: 当前语种的语言包对象 (仅当多语言类型为 `redux` 时)
26 * - 服务器端: 所有语种语言包合集对象
27 */
28export const locales = (() => getLocalesObject() || {})()
29
30export const setLocales = (newLocales = {}) => {
31 const obj = getLocalesObject()
32 if (obj) Object.assign(obj, newLocales)
33 return locales
34}
35
36export default locales