@dcloudio/uni-i18n
Advanced tools
Comparing version 2.0.0 to 3.0.0-alpha-3000020210521003
@@ -103,2 +103,7 @@ 'use strict'; | ||
const LOCALE_ZH_HANS = 'zh-Hans'; | ||
const LOCALE_ZH_HANT = 'zh-Hant'; | ||
const LOCALE_EN = 'en'; | ||
const LOCALE_FR = 'fr'; | ||
const LOCALE_ES = 'es'; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
@@ -124,13 +129,13 @@ const hasOwn = (val, key) => hasOwnProperty.call(val, key); | ||
if (locale.indexOf('-hans') !== -1) { | ||
return 'zh-Hans'; | ||
return LOCALE_ZH_HANS; | ||
} | ||
if (locale.indexOf('-hant') !== -1) { | ||
return 'zh-Hant'; | ||
return LOCALE_ZH_HANT; | ||
} | ||
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) { | ||
return 'zh-Hant'; | ||
return LOCALE_ZH_HANT; | ||
} | ||
return 'zh-Hans'; | ||
return LOCALE_ZH_HANS; | ||
} | ||
const lang = startsWith(locale, ['en', 'fr', 'es']); | ||
const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]); | ||
if (lang) { | ||
@@ -142,4 +147,4 @@ return lang; | ||
constructor({ locale, fallbackLocale, messages, watcher, formater, }) { | ||
this.locale = 'en'; | ||
this.fallbackLocale = 'en'; | ||
this.locale = LOCALE_EN; | ||
this.fallbackLocale = LOCALE_EN; | ||
this.message = {}; | ||
@@ -152,3 +157,3 @@ this.messages = {}; | ||
this.formater = formater || defaultFormatter; | ||
this.messages = messages; | ||
this.messages = messages || {}; | ||
this.setLocale(locale); | ||
@@ -162,2 +167,6 @@ if (watcher) { | ||
this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale; | ||
if (!this.messages[this.locale]) { | ||
// 可能初始化时不存在 | ||
this.messages[this.locale] = {}; | ||
} | ||
this.message = this.messages[this.locale]; | ||
@@ -177,2 +186,10 @@ this.watchers.forEach((watcher) => { | ||
} | ||
add(locale, message) { | ||
if (this.messages[locale]) { | ||
Object.assign(this.messages[locale], message); | ||
} | ||
else { | ||
this.messages[locale] = message; | ||
} | ||
} | ||
t(key, locale, values) { | ||
@@ -203,13 +220,20 @@ let message = this.message; | ||
} | ||
function getDefaultLocale() { | ||
if (typeof navigator !== 'undefined') { | ||
return navigator.userLanguage || navigator.language; | ||
// function getDefaultLocale() { | ||
// if (typeof navigator !== 'undefined') { | ||
// return (navigator as any).userLanguage || navigator.language | ||
// } | ||
// if (typeof plus !== 'undefined') { | ||
// // TODO 待调整为最新的获取语言代码 | ||
// return plus.os.language | ||
// } | ||
// return uni.getSystemInfoSync().language | ||
// } | ||
function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN) { | ||
// 兼容旧版本入参 | ||
if (typeof locale !== 'string') { | ||
[locale, messages] = [messages, locale]; | ||
} | ||
if (typeof plus !== 'undefined') { | ||
// TODO 待调整为最新的获取语言代码 | ||
return plus.os.language; | ||
if (typeof locale !== 'string') { | ||
locale = fallbackLocale; | ||
} | ||
return uni.getSystemInfoSync().language; | ||
} | ||
function initVueI18n(messages, fallbackLocale = 'en', locale) { | ||
const i18n = new I18n({ | ||
@@ -222,3 +246,3 @@ locale: locale || fallbackLocale, | ||
if (typeof getApp !== 'function') { | ||
// app-plus view | ||
// app view | ||
/* eslint-disable no-func-assign */ | ||
@@ -232,5 +256,5 @@ t = function (key, values) { | ||
if (!appVm.$t || !appVm.$i18n) { | ||
if (!locale) { | ||
i18n.setLocale(getDefaultLocale()); | ||
} | ||
// if (!locale) { | ||
// i18n.setLocale(getDefaultLocale()) | ||
// } | ||
/* eslint-disable no-func-assign */ | ||
@@ -260,5 +284,9 @@ t = function (key, values) { | ||
return { | ||
i18n, | ||
t(key, values) { | ||
return t(key, values); | ||
}, | ||
add(locale, message) { | ||
return i18n.add(locale, message); | ||
}, | ||
getLocale() { | ||
@@ -270,17 +298,2 @@ return i18n.getLocale(); | ||
}, | ||
mixin: { | ||
beforeCreate() { | ||
const unwatch = i18n.watchLocale(() => { | ||
this.$forceUpdate(); | ||
}); | ||
this.$once('hook:beforeDestroy', function () { | ||
unwatch(); | ||
}); | ||
}, | ||
methods: { | ||
$$t(key, values) { | ||
return t(key, values); | ||
}, | ||
}, | ||
}, | ||
}; | ||
@@ -290,2 +303,7 @@ } | ||
exports.I18n = I18n; | ||
exports.LOCALE_EN = LOCALE_EN; | ||
exports.LOCALE_ES = LOCALE_ES; | ||
exports.LOCALE_FR = LOCALE_FR; | ||
exports.LOCALE_ZH_HANS = LOCALE_ZH_HANS; | ||
exports.LOCALE_ZH_HANT = LOCALE_ZH_HANT; | ||
exports.initVueI18n = initVueI18n; |
export declare type BuiltInLocale = 'zh-Hans' | 'zh-Hant' | 'en' | 'fr' | 'es'; | ||
export declare type BuiltInLocale = typeof LOCALE_ZH_HANS | typeof LOCALE_ZH_HANT | typeof LOCALE_EN | typeof LOCALE_FR | typeof LOCALE_ES; | ||
@@ -19,2 +19,3 @@ export declare interface Formatter { | ||
watchLocale(fn: LocaleWatcher): () => void; | ||
add(locale: BuiltInLocale, message: Record<string, string>): void; | ||
t(key: string, values?: Record<string, unknown> | Array<unknown> | BuiltInLocale): string; | ||
@@ -27,3 +28,3 @@ t(key: string, locale?: BuiltInLocale, values?: Record<string, unknown> | Array<unknown>): string; | ||
fallbackLocale?: BuiltInLocale; | ||
messages: LocaleMessages; | ||
messages?: LocaleMessages; | ||
formater?: Formatter; | ||
@@ -33,14 +34,20 @@ watcher?: LocaleWatcher; | ||
export declare function initVueI18n(messages: LocaleMessages, fallbackLocale?: BuiltInLocale, locale?: BuiltInLocale): { | ||
export declare function initVueI18n(locale?: BuiltInLocale, messages?: LocaleMessages, fallbackLocale?: BuiltInLocale): { | ||
i18n: I18n; | ||
t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string; | ||
add(locale: BuiltInLocale, message: Record<string, string>): void; | ||
getLocale(): BuiltInLocale; | ||
setLocale(newLocale: BuiltInLocale): void; | ||
mixin: { | ||
beforeCreate(): void; | ||
methods: { | ||
$$t(key: string, values?: any): string; | ||
}; | ||
}; | ||
}; | ||
export declare const LOCALE_EN = "en"; | ||
export declare const LOCALE_ES = "es"; | ||
export declare const LOCALE_FR = "fr"; | ||
export declare const LOCALE_ZH_HANS = "zh-Hans"; | ||
export declare const LOCALE_ZH_HANT = "zh-Hant"; | ||
export declare type LocaleMessages = { | ||
@@ -47,0 +54,0 @@ [name in BuiltInLocale]?: Record<string, string>; |
{ | ||
"name": "@dcloudio/uni-i18n", | ||
"version": "2.0.0", | ||
"version": "3.0.0-alpha-3000020210521003", | ||
"description": "@dcloudio/uni-i18n", | ||
"main": "dist/uni-i18n.cjs.js", | ||
"module": "dist/uni-i18n.esm.js", | ||
"types": "dist/uni-i18n.d.ts", | ||
"main": "./dist/uni-i18n.cjs.js", | ||
"module": "./dist/uni-i18n.es.js", | ||
"types": "./dist/uni-i18n.d.ts", | ||
"files": [ | ||
@@ -21,3 +21,3 @@ "dist" | ||
}, | ||
"gitHead": "b1bdfb4de3f518c1c0fbab242403ea05feced4e3" | ||
"gitHead": "e2dc110527f42462d0a7afa2f81108ef899a4fda" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
32401
619
0
2
1