-
Notifications
You must be signed in to change notification settings - Fork 380
/
i18n.js
210 lines (169 loc) · 4.87 KB
/
i18n.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* @flow */
import { interpolate } from "./context"
import { isString, isFunction, isEmpty } from "./essentials"
import t from "./t"
import { select, plural, selectOrdinal } from "./select"
import * as dev from "./dev"
type MessageOptions = {|
defaults?: string,
formats?: Object
|}
type LanguageData = {
plurals?: Function
}
type Messages = { [key: string]: string | Function }
type Catalog = {
messages: Messages,
languageData?: LanguageData
}
type Catalogs = { [key: string]: Catalog }
type setupI18nProps = {
language?: string,
catalogs?: Catalogs,
development?: Object
}
function getLanguageData(catalog) {
return (catalog || {}).languageData || {}
}
function getMessages(catalog) {
return (catalog || {}).messages || {}
}
class I18n {
_language: string
// Message catalogs
_catalogs: Catalogs
// Messages/language data in active language.
// This is optimization, so we don't perform object lookup
// _catalogs[language] for each translation.
_activeMessages: Messages
_activeLanguageData: LanguageData
_dev: Object
t: Function
plural: Function
select: Function
selectOrdinal: Function
constructor() {
// Messages and languageData are merged on load,
// so we must initialize it manually
this._activeMessages = {}
this._catalogs = {}
if (process.env.NODE_ENV !== "production") {
this.t = t
this.select = select
this.plural = plural(this)
this.selectOrdinal = selectOrdinal(this)
}
}
get availableLanguages(): Array<string> {
return Object.keys(this._catalogs)
}
get language(): string {
return this._language
}
get messages(): Messages {
return this._activeMessages
}
get languageData(): LanguageData {
return this._activeLanguageData
}
_cacheActiveLanguage() {
const activeCatalog = this._catalogs[this.language]
let languageData = getLanguageData(activeCatalog)
if (process.env.NODE_ENV !== "production") {
// Allow overriding data in development, useful for testing
if (
isEmpty(languageData) &&
this._dev &&
isFunction(this._dev.loadLanguageData)
) {
languageData = this._dev.loadLanguageData(this.language)
}
}
this._activeMessages = getMessages(activeCatalog)
this._activeLanguageData = languageData
}
load(catalogs: Catalogs) {
if (typeof catalogs !== "object") return
// deeply merge Catalogs
Object.keys({ ...this._catalogs, ...catalogs }).forEach(language => {
let compiledMessages = getMessages(catalogs[language])
if (process.env.NODE_ENV !== "production") {
if (this._dev && isFunction(this._dev.compile)) {
compiledMessages = Object.keys(compiledMessages).reduce(
(dict, id) => {
const msg = compiledMessages[id]
dict[id] = isString(msg) ? this._dev.compile(msg) : msg
return dict
},
{}
)
}
}
this._catalogs[language] = {
messages: {
...getMessages(this._catalogs[language]),
...compiledMessages
},
languageData: {
...getLanguageData(this._catalogs[language]),
...getLanguageData(catalogs[language])
}
}
})
this._cacheActiveLanguage()
}
activate(language: string) {
if (!language) return
if (process.env.NODE_ENV !== "production") {
if (this.availableLanguages.indexOf(language) === -1) {
console.warn(`Message catalog for locale "${language}" not loaded.`)
}
}
this._language = language
this._cacheActiveLanguage()
}
use(language: string) {
return setupI18n({
language,
catalogs: this._catalogs,
development: this._dev
})
}
// default translate method
_(
id: string,
values: Object = {},
{ defaults, formats = {} }: MessageOptions = {}
) {
let translation = this.messages[id] || defaults || id
if (process.env.NODE_ENV !== "production") {
if (isString(translation) && this._dev && isFunction(this._dev.compile)) {
translation = this._dev.compile(translation)
}
}
if (typeof translation !== "function") return translation
return interpolate(translation, this.language, this.languageData)(
values,
formats
)
}
pluralForm(
n: number,
pluralType?: "cardinal" | "ordinal" = "cardinal"
): string {
if (!this.languageData.plurals) return "other"
return this.languageData.plurals(n, pluralType === "ordinal")
}
}
function setupI18n(params?: setupI18nProps = {}): I18n {
const i18n = new I18n()
if (process.env.NODE_ENV !== "production") {
i18n._dev = dev
}
if (params.catalogs) i18n.load(params.catalogs)
if (params.language) i18n.activate(params.language)
return i18n
}
const i18n = setupI18n()
export { setupI18n, i18n }
export type { MessageOptions, Catalog, Catalogs, LanguageData, I18n }