-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
134 lines (114 loc) · 3.42 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { getValueByPath, deepMerge } from '@pluginjs/utils'
import template from '@pluginjs/template'
class I18N {
constructor(defaults, translations) {
this.defaults = deepMerge(I18N.defaults, defaults)
this.translations = translations ? translations : {}
}
hasTranslation(locale) {
return locale in this.translations
}
addTranslation(locale, translation) {
if (this.translations[locale]) {
Object.assign(this.translations[locale], translation)
} else {
this.translations[locale] = translation
}
}
getTranslation(locale) {
if (this.translations[locale]) {
return this.translations[locale]
}
return {}
}
instance(options = {}) {
const that = this
const _options = deepMerge(that.defaults, options)
let _locale = _options.locale
function getMessage(key, locale) {
const translation = that.getTranslation(locale)
const message = getValueByPath(translation, key)
return message
}
return {
translate(key, args = {}, locale = _locale) {
let message = getMessage(key, locale)
if (typeof message === 'undefined' && _options.fallbacks) {
const locales = locale.split('-')
if (locales.length > 1 && that.hasTranslation(locales[0])) {
message = getMessage(key, locales[0])
}
if (typeof message === 'undefined') {
let fallbackLocale
if (
_options.fallbacks !== true &&
that.hasTranslation(_options.fallbacks)
) {
fallbackLocale = _options.fallbacks
} else {
fallbackLocale = that.defaults.locale
}
message = getMessage(key, fallbackLocale)
}
}
if (
Object.prototype.toString.call(message) === '[object Array]' &&
message.length >= 2
) {
if (typeof args._number === 'string') {
if (typeof args[args._number] !== 'undefined') {
const _number = parseInt(args[args._number], 10)
if (_number === 1) {
message = message[0]
} else if (_number > 1) {
message = message[1]
} else if (_number === 0 && message.length >= 3) {
message = message[2]
}
}
}
}
if (typeof message === 'string') {
const parsed = template.parse(message)
if (!parsed) {
return message
}
let key
for (let i = 0; i < parsed.length; i++) {
key = parsed[i]
if (typeof args[key] === 'undefined') {
args[key] = _options.missingPlaceholder(key)
} else if (args[key] === null) {
args[key] = _options.nullPlaceholder(key)
}
}
return template.render(message, args)
}
if (Object(message) === message) {
return message
}
return `[missing "${locale}.${key}" translation]`
},
setLocale(locale) {
_locale = locale
},
getLocale() {
return _locale
}
}
}
setTranslations(translations) {
this.translations = translations
}
}
I18N.defaults = {
locale: 'en',
fallbacks: true,
nullPlaceholder(key) {
return `[missing {{${key}}} value]`
},
missingPlaceholder(key) {
return `[missing {{${key}}} value]`
}
}
export default I18N