UNPKG

1.54 kBJavaScriptView Raw
1import {
2 setLocaleId,
3 setLocales
4} from '../index'
5
6/**
7 * 初始化
8 *
9 * @param {string} [args.localeId] 当前语言ID。如过未提供,会尝试从初始 redux store 中查询
10 * @param {Object} [args.locales] 当前语言包内容对象。如过未提供,会尝试从初始 redux store 中查询
11 */
12export default ({
13 localeId,
14 locales
15}) => {
16 if (!__CLIENT__) return
17
18 if (typeof localeId === 'undefined' &&
19 typeof __REDUX_STATE__ === 'object' &&
20 typeof __REDUX_STATE__.localeId !== 'undefined'
21 )
22 localeId = __REDUX_STATE__.localeId
23 if (typeof locales === 'undefined' &&
24 typeof __REDUX_STATE__ === 'object' &&
25 typeof __REDUX_STATE__.locales !== 'undefined'
26 )
27 locales = __REDUX_STATE__.locales
28
29 if (typeof localeId === 'undefined' || typeof locales === 'undefined')
30 return
31
32 setLocaleId(localeId)
33 setLocales(localeId, locales)
34
35 if (localeId && typeof document !== 'undefined' && typeof document.cookie !== 'undefined') {
36 const Cookies = require('js-cookie')
37 const cookieOptions = {
38 expires: 365
39 }
40 if (typeof process.env.KOOT_I18N_COOKIE_DOMAIN === 'string' &&
41 process.env.KOOT_I18N_COOKIE_DOMAIN) {
42 cookieOptions.domain = process.env.KOOT_I18N_COOKIE_DOMAIN
43 }
44 Cookies.set(
45 process.env.KOOT_I18N_COOKIE_KEY,
46 localeId,
47 cookieOptions
48 )
49 }
50}