@dcloudio/uni-i18n
Advanced tools
Comparing version 0.0.2 to 0.0.3
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const isObject = (val) => val !== null && typeof val === 'object'; | ||
@@ -101,6 +103,2 @@ class BaseFormatter { | ||
let curLocale = 'en'; | ||
let fallbackLocale = 'en'; | ||
let curMessages = {}; | ||
let messages = {}; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
@@ -115,5 +113,5 @@ const hasOwn = (val, key) => hasOwnProperty.call(val, key); | ||
} | ||
function normalizeLocale(locale) { | ||
function normalizeLocale(locale, messages) { | ||
if (!locale) { | ||
return fallbackLocale; | ||
return; | ||
} | ||
@@ -141,39 +139,141 @@ locale = locale.trim().replace(/_/g, '-'); | ||
} | ||
return fallbackLocale; | ||
} | ||
const watchers = []; | ||
var index = { | ||
init(options) { | ||
if (options.fallbackLocale) { | ||
fallbackLocale = options.fallbackLocale; | ||
class I18n { | ||
constructor({ locale, fallbackLocale, messages, watcher, formater, }) { | ||
this.locale = 'en'; | ||
this.fallbackLocale = 'en'; | ||
this.message = {}; | ||
this.messages = {}; | ||
this.watchers = []; | ||
if (fallbackLocale) { | ||
this.fallbackLocale = fallbackLocale; | ||
} | ||
messages = options.messages; | ||
this.setLocale(options.locale); | ||
}, | ||
this.formater = formater || defaultFormatter; | ||
this.messages = messages; | ||
this.setLocale(locale); | ||
if (watcher) { | ||
this.watchLocale(watcher); | ||
} | ||
} | ||
setLocale(locale) { | ||
const oldLocale = curLocale; | ||
curLocale = normalizeLocale(locale); | ||
curMessages = messages[curLocale]; | ||
watchers.forEach((watcher) => { | ||
watcher(curLocale, oldLocale); | ||
const oldLocale = this.locale; | ||
this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale; | ||
this.message = this.messages[this.locale]; | ||
this.watchers.forEach((watcher) => { | ||
watcher(this.locale, oldLocale); | ||
}); | ||
}, | ||
} | ||
getLocale() { | ||
return curLocale; | ||
}, | ||
return this.locale; | ||
} | ||
watchLocale(fn) { | ||
const index = watchers.push(fn) - 1; | ||
const index = this.watchers.push(fn) - 1; | ||
return () => { | ||
watchers.splice(index, 1); | ||
this.watchers.splice(index, 1); | ||
}; | ||
}, | ||
t(key, values) { | ||
if (!hasOwn(curMessages, key)) { | ||
} | ||
t(key, locale, values) { | ||
let message = this.message; | ||
if (typeof locale === 'string') { | ||
locale = normalizeLocale(locale, this.messages); | ||
locale && (message = this.messages[locale]); | ||
} | ||
else { | ||
values = locale; | ||
} | ||
if (!hasOwn(message, key)) { | ||
console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`); | ||
return key; | ||
} | ||
return defaultFormatter.interpolate(curMessages[key], values).join(''); | ||
}, | ||
}; | ||
return this.formater.interpolate(message[key], values).join(''); | ||
} | ||
} | ||
module.exports = index; | ||
function initLocaleWatcher(appVm, i18n) { | ||
appVm.$i18n && | ||
appVm.$i18n.vm.$watch('locale', (newLocale) => { | ||
i18n.setLocale(newLocale); | ||
}, { | ||
immediate: true, | ||
}); | ||
} | ||
function getDefaultLocale() { | ||
if (typeof navigator !== 'undefined') { | ||
return navigator.userLanguage || navigator.language; | ||
} | ||
if (typeof plus !== 'undefined') { | ||
// TODO 待调整为最新的获取语言代码 | ||
return plus.os.language; | ||
} | ||
return uni.getSystemInfoSync().language; | ||
} | ||
function initVueI18n(messages, fallbackLocale = 'en', locale) { | ||
const i18n = new I18n({ | ||
locale: locale || fallbackLocale, | ||
fallbackLocale, | ||
messages, | ||
}); | ||
let t = (key, values) => { | ||
if (typeof getApp !== 'function') { | ||
// app-plus view | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
return i18n.t(key, values); | ||
}; | ||
} | ||
else { | ||
const appVm = getApp().$vm; | ||
if (!appVm.$t || !appVm.$i18n) { | ||
if (!locale) { | ||
i18n.setLocale(getDefaultLocale()); | ||
} | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
return i18n.t(key, values); | ||
}; | ||
} | ||
else { | ||
initLocaleWatcher(appVm, i18n); | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
const $i18n = appVm.$i18n; | ||
const silentTranslationWarn = $i18n.silentTranslationWarn; | ||
$i18n.silentTranslationWarn = true; | ||
const msg = appVm.$t(key, values); | ||
$i18n.silentTranslationWarn = silentTranslationWarn; | ||
if (msg !== key) { | ||
return msg; | ||
} | ||
return i18n.t(key, $i18n.locale, values); | ||
}; | ||
} | ||
} | ||
return t(key, values); | ||
}; | ||
return { | ||
t(key, values) { | ||
return t(key, values); | ||
}, | ||
setLocale(newLocale) { | ||
return i18n.setLocale(newLocale); | ||
}, | ||
mixin: { | ||
beforeCreate() { | ||
const unwatch = i18n.watchLocale(() => { | ||
this.$forceUpdate(); | ||
}); | ||
this.$once('hook:beforeDestroy', function () { | ||
unwatch(); | ||
}); | ||
}, | ||
methods: { | ||
$$t(key, values) { | ||
return t(key, values); | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
exports.I18n = I18n; | ||
exports.initVueI18n = initVueI18n; |
export declare type BuiltInLocale = 'zh-Hans' | 'zh-Hant' | 'en' | 'fr' | 'es'; | ||
declare const _default: { | ||
init(options: I18nOptions): void; | ||
export declare interface Formatter { | ||
interpolate: (message: string, values?: Record<string, unknown> | Array<unknown>) => Array<unknown>; | ||
} | ||
export declare class I18n { | ||
private locale; | ||
private fallbackLocale; | ||
private message; | ||
private messages; | ||
private watchers; | ||
private formater; | ||
constructor({ locale, fallbackLocale, messages, watcher, formater, }: I18nOptions); | ||
setLocale(locale: string): void; | ||
getLocale(): BuiltInLocale; | ||
watchLocale(fn: LocaleWatcher): () => void; | ||
t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string; | ||
}; | ||
export default _default; | ||
t(key: string, values?: Record<string, unknown> | Array<unknown> | BuiltInLocale): string; | ||
t(key: string, locale?: BuiltInLocale, values?: Record<string, unknown> | Array<unknown>): string; | ||
} | ||
export declare interface I18nOptions { | ||
locale: BuiltInLocale; | ||
fallbackLocale: BuiltInLocale; | ||
fallbackLocale?: BuiltInLocale; | ||
messages: LocaleMessages; | ||
formater?: Formatter; | ||
watcher?: LocaleWatcher; | ||
} | ||
export declare function initVueI18n(messages: LocaleMessages, fallbackLocale?: BuiltInLocale, locale?: BuiltInLocale): { | ||
t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string; | ||
setLocale(newLocale: BuiltInLocale): void; | ||
mixin: { | ||
beforeCreate(): void; | ||
methods: { | ||
$$t(key: string, values?: any): string; | ||
}; | ||
}; | ||
}; | ||
export declare type LocaleMessages = { | ||
@@ -23,4 +46,4 @@ [name in BuiltInLocale]?: Record<string, string>; | ||
declare type LocaleWatcher = (newLocale: BuiltInLocale, oldLocale: BuiltInLocale) => void; | ||
export declare type LocaleWatcher = (newLocale: BuiltInLocale, oldLocale: BuiltInLocale) => void; | ||
export { } |
@@ -99,6 +99,2 @@ const isObject = (val) => val !== null && typeof val === 'object'; | ||
let curLocale = 'en'; | ||
let fallbackLocale = 'en'; | ||
let curMessages = {}; | ||
let messages = {}; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
@@ -113,5 +109,5 @@ const hasOwn = (val, key) => hasOwnProperty.call(val, key); | ||
} | ||
function normalizeLocale(locale) { | ||
function normalizeLocale(locale, messages) { | ||
if (!locale) { | ||
return fallbackLocale; | ||
return; | ||
} | ||
@@ -139,39 +135,140 @@ locale = locale.trim().replace(/_/g, '-'); | ||
} | ||
return fallbackLocale; | ||
} | ||
const watchers = []; | ||
var index = { | ||
init(options) { | ||
if (options.fallbackLocale) { | ||
fallbackLocale = options.fallbackLocale; | ||
class I18n { | ||
constructor({ locale, fallbackLocale, messages, watcher, formater, }) { | ||
this.locale = 'en'; | ||
this.fallbackLocale = 'en'; | ||
this.message = {}; | ||
this.messages = {}; | ||
this.watchers = []; | ||
if (fallbackLocale) { | ||
this.fallbackLocale = fallbackLocale; | ||
} | ||
messages = options.messages; | ||
this.setLocale(options.locale); | ||
}, | ||
this.formater = formater || defaultFormatter; | ||
this.messages = messages; | ||
this.setLocale(locale); | ||
if (watcher) { | ||
this.watchLocale(watcher); | ||
} | ||
} | ||
setLocale(locale) { | ||
const oldLocale = curLocale; | ||
curLocale = normalizeLocale(locale); | ||
curMessages = messages[curLocale]; | ||
watchers.forEach((watcher) => { | ||
watcher(curLocale, oldLocale); | ||
const oldLocale = this.locale; | ||
this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale; | ||
this.message = this.messages[this.locale]; | ||
this.watchers.forEach((watcher) => { | ||
watcher(this.locale, oldLocale); | ||
}); | ||
}, | ||
} | ||
getLocale() { | ||
return curLocale; | ||
}, | ||
return this.locale; | ||
} | ||
watchLocale(fn) { | ||
const index = watchers.push(fn) - 1; | ||
const index = this.watchers.push(fn) - 1; | ||
return () => { | ||
watchers.splice(index, 1); | ||
this.watchers.splice(index, 1); | ||
}; | ||
}, | ||
t(key, values) { | ||
if (!hasOwn(curMessages, key)) { | ||
} | ||
t(key, locale, values) { | ||
let message = this.message; | ||
if (typeof locale === 'string') { | ||
locale = normalizeLocale(locale, this.messages); | ||
locale && (message = this.messages[locale]); | ||
} | ||
else { | ||
values = locale; | ||
} | ||
if (!hasOwn(message, key)) { | ||
console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`); | ||
return key; | ||
} | ||
return defaultFormatter.interpolate(curMessages[key], values).join(''); | ||
}, | ||
}; | ||
return this.formater.interpolate(message[key], values).join(''); | ||
} | ||
} | ||
export default index; | ||
function initLocaleWatcher(appVm, i18n) { | ||
appVm.$i18n && | ||
appVm.$i18n.vm.$watch('locale', (newLocale) => { | ||
i18n.setLocale(newLocale); | ||
}, { | ||
immediate: true, | ||
}); | ||
} | ||
function getDefaultLocale() { | ||
if (typeof navigator !== 'undefined') { | ||
return navigator.userLanguage || navigator.language; | ||
} | ||
if (typeof plus !== 'undefined') { | ||
// TODO 待调整为最新的获取语言代码 | ||
return plus.os.language; | ||
} | ||
return uni.getSystemInfoSync().language; | ||
} | ||
function initVueI18n(messages, fallbackLocale = 'en', locale) { | ||
const i18n = new I18n({ | ||
locale: locale || fallbackLocale, | ||
fallbackLocale, | ||
messages, | ||
}); | ||
let t = (key, values) => { | ||
if (typeof getApp !== 'function') { | ||
// app-plus view | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
return i18n.t(key, values); | ||
}; | ||
} | ||
else { | ||
const appVm = getApp().$vm; | ||
if (!appVm.$t || !appVm.$i18n) { | ||
if (!locale) { | ||
i18n.setLocale(getDefaultLocale()); | ||
} | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
return i18n.t(key, values); | ||
}; | ||
} | ||
else { | ||
initLocaleWatcher(appVm, i18n); | ||
/* eslint-disable no-func-assign */ | ||
t = function (key, values) { | ||
const $i18n = appVm.$i18n; | ||
const silentTranslationWarn = $i18n.silentTranslationWarn; | ||
$i18n.silentTranslationWarn = true; | ||
const msg = appVm.$t(key, values); | ||
$i18n.silentTranslationWarn = silentTranslationWarn; | ||
if (msg !== key) { | ||
return msg; | ||
} | ||
return i18n.t(key, $i18n.locale, values); | ||
}; | ||
} | ||
} | ||
return t(key, values); | ||
}; | ||
return { | ||
t(key, values) { | ||
return t(key, values); | ||
}, | ||
setLocale(newLocale) { | ||
return i18n.setLocale(newLocale); | ||
}, | ||
mixin: { | ||
beforeCreate() { | ||
const unwatch = i18n.watchLocale(() => { | ||
this.$forceUpdate(); | ||
}); | ||
this.$once('hook:beforeDestroy', function () { | ||
unwatch(); | ||
}); | ||
}, | ||
methods: { | ||
$$t(key, values) { | ||
return t(key, values); | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
export { I18n, initVueI18n }; |
{ | ||
"name": "@dcloudio/uni-i18n", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "@dcloudio/uni-i18n", | ||
@@ -5,0 +5,0 @@ "main": "dist/uni-i18n.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30697
579