-
Notifications
You must be signed in to change notification settings - Fork 379
/
i18n.ts
220 lines (179 loc) · 5.53 KB
/
i18n.ts
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
211
212
213
214
215
216
217
218
219
220
import { interpolate } from "./context"
import { isString, isFunction } from "./essentials"
import { date, number } from "./formats"
import * as icu from "./dev"
import { EventEmitter } from "./eventEmitter"
export type MessageOptions = {
message?: string
formats?: Object
}
export type Locale = string
export type Locales = Locale | Locale[]
export type LocaleData = {
plurals?: Function
}
export type AllLocaleData = Record<Locale, LocaleData>
export type CompiledMessage =
| string
| Array<
string | Array<string | (string | undefined) | Record<string, unknown>>
>
export type Messages = Record<string, CompiledMessage>
export type AllMessages = Record<Locale, Messages>
export type MessageDescriptor = {
id?: string
comment?: string
message?: string
values?: Record<string, unknown>
}
export type MissingMessageEvent = {
locale: Locale
id: string
}
type setupI18nProps = {
locale?: Locale
locales?: Locales
messages?: AllMessages
localeData?: AllLocaleData
missing?: string | ((message, id) => string)
}
type Events = {
change: () => void
missing: (event: MissingMessageEvent) => void
}
export class I18n extends EventEmitter<Events> {
_locale: Locale
_locales: Locales
_localeData: AllLocaleData
_messages: AllMessages
_missing: string | ((message, id) => string)
constructor(params: setupI18nProps) {
super()
this._messages = {}
this._localeData = {}
if (params.missing != null) this._missing = params.missing
if (params.messages != null) this.load(params.messages)
if (params.localeData != null) this.loadLocaleData(params.localeData)
if (params.locale != null || params.locales != null) {
this.activate(params.locale, params.locales)
}
}
get locale() {
return this._locale
}
get locales() {
return this._locales
}
get messages(): Messages {
return this._messages[this._locale] ?? {}
}
get localeData(): LocaleData {
return this._localeData[this._locale] ?? {}
}
_loadLocaleData(locale: Locale, localeData: LocaleData) {
if (this._localeData[locale] == null) {
this._localeData[locale] = localeData
} else {
Object.assign(this._localeData[locale], localeData)
}
}
public loadLocaleData(allLocaleData: AllLocaleData): void
public loadLocaleData(locale: Locale, localeData: LocaleData): void
loadLocaleData(localeOrAllData, localeData?) {
if (localeData != null) {
// loadLocaleData('en', enLocaleData)
// Loading locale data for a single locale.
this._loadLocaleData(localeOrAllData, localeData)
} else {
// loadLocaleData(allLocaleData)
// Loading all locale data at once.
Object.keys(localeOrAllData).forEach((locale) =>
this._loadLocaleData(locale, localeOrAllData[locale])
)
}
this.emit("change")
}
_load(locale: Locale, messages: Messages) {
if (this._messages[locale] == null) {
this._messages[locale] = messages
} else {
Object.assign(this._messages[locale], messages)
}
}
public load(allMessages: AllMessages): void
public load(locale: Locale, messages: Messages): void
load(localeOrMessages, messages?) {
if (messages != null) {
// load('en', catalog)
// Loading a catalog for a single locale.
this._load(localeOrMessages, messages)
} else {
// load(catalogs)
// Loading several locales at once.
Object.keys(localeOrMessages).forEach((locale) =>
this._load(locale, localeOrMessages[locale])
)
}
this.emit("change")
}
activate(locale: Locale, locales?: Locales) {
if (process.env.NODE_ENV !== "production") {
if (!this._messages[locale]) {
console.warn(`Messages for locale "${locale}" not loaded.`)
}
if (!this._localeData[locale]) {
console.warn(
`Locale data for locale "${locale}" not loaded. Plurals won't work correctly.`
)
}
}
this._locale = locale
this._locales = locales
this.emit("change")
}
// method for translation and formatting
_(
id: MessageDescriptor | string,
values: Object | undefined = {},
{ message, formats }: MessageOptions | undefined = {}
) {
if (!isString(id)) {
values = id.values || values
message = id.message
id = id.id
}
let translation = this.messages[id] || message || id
// replace missing messages with custom message for debugging
const missing = this._missing
if (missing && !this.messages[id]) {
return isFunction(missing) ? missing(this.locale, id) : missing
}
if (!this.messages[id]) {
this.emit("missing", { id, locale: this._locale })
}
if (process.env.NODE_ENV !== "production") {
translation = isString(translation)
? icu.compile(translation)
: translation
}
// hack for parsing unicode values inside a string to get parsed in react native environments
if (isString(translation) && /\\u[a-fA-F0-9]{4}/g.test(translation)) return JSON.parse(`"${translation}"`)
if (isString(translation)) return translation
return interpolate(
translation,
this.locale,
this.locales,
this.localeData
)(values, formats)
}
date(value: string | Date, format?: Intl.DateTimeFormatOptions): string {
return date(this.locales || this.locale, format)(value)
}
number(value: number, format?: Intl.NumberFormatOptions): string {
return number(this.locales || this.locale, format)(value)
}
}
function setupI18n(params: setupI18nProps = {}): I18n {
return new I18n(params)
}
export { setupI18n }