-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.ts
230 lines (192 loc) · 6.17 KB
/
index.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
221
222
223
224
225
226
227
228
229
230
export interface Y18NOpts {
directory?: string;
updateFiles?: boolean;
locale?: string;
fallbackToLanguage?: boolean;
}
interface Work {
directory: string;
locale: string;
cb: Function
}
export interface Locale {
[key: string]: string
}
interface CacheEntry {
[key: string]: string;
}
export interface PlatformShim {
fs: {
readFileSync: Function,
writeFile: Function
},
exists: Function,
format: Function,
resolve: Function
}
let shim: PlatformShim
class Y18N {
directory: string;
updateFiles: boolean;
locale: string;
fallbackToLanguage: boolean;
writeQueue: Work[];
cache: {[key: string]: {[key: string]: CacheEntry|string}};
constructor (opts: Y18NOpts) {
// configurable options.
opts = opts || {}
this.directory = opts.directory || './locales'
this.updateFiles = typeof opts.updateFiles === 'boolean' ? opts.updateFiles : true
this.locale = opts.locale || 'en'
this.fallbackToLanguage = typeof opts.fallbackToLanguage === 'boolean' ? opts.fallbackToLanguage : true
// internal stuff.
this.cache = Object.create(null)
this.writeQueue = []
}
__ (...args: (string|Function)[]): string {
if (typeof arguments[0] !== 'string') {
return this._taggedLiteral(arguments[0] as string[], ...arguments)
}
const str: string = args.shift() as string
let cb: Function = function () {} // start with noop.
if (typeof args[args.length - 1] === 'function') cb = (args.pop() as Function)
cb = cb || function () {} // noop.
if (!this.cache[this.locale]) this._readLocaleFile()
// we've observed a new string, update the language file.
if (!this.cache[this.locale][str] && this.updateFiles) {
this.cache[this.locale][str] = str
// include the current directory and locale,
// since these values could change before the
// write is performed.
this._enqueueWrite({
directory: this.directory,
locale: this.locale,
cb
})
} else {
cb()
}
return shim.format.apply(shim.format, [this.cache[this.locale][str] || str].concat(args as string[]))
}
__n () {
const args = Array.prototype.slice.call(arguments)
const singular: string = args.shift()
const plural: string = args.shift()
const quantity: number = args.shift()
let cb = function () {} // start with noop.
if (typeof args[args.length - 1] === 'function') cb = args.pop()
if (!this.cache[this.locale]) this._readLocaleFile()
let str = quantity === 1 ? singular : plural
if (this.cache[this.locale][singular]) {
const entry = this.cache[this.locale][singular] as CacheEntry
str = entry[quantity === 1 ? 'one' : 'other']
}
// we've observed a new string, update the language file.
if (!this.cache[this.locale][singular] && this.updateFiles) {
this.cache[this.locale][singular] = {
one: singular,
other: plural
}
// include the current directory and locale,
// since these values could change before the
// write is performed.
this._enqueueWrite({
directory: this.directory,
locale: this.locale,
cb
})
} else {
cb()
}
// if a %d placeholder is provided, add quantity
// to the arguments expanded by util.format.
const values: (string|number)[] = [str]
if (~str.indexOf('%d')) values.push(quantity)
return shim.format.apply(shim.format, values.concat(args))
}
setLocale (locale: string) {
this.locale = locale
}
getLocale () {
return this.locale
}
updateLocale (obj: Locale) {
if (!this.cache[this.locale]) this._readLocaleFile()
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
this.cache[this.locale][key] = obj[key]
}
}
}
_taggedLiteral (parts: string[], ...args: string[]) {
let str = ''
parts.forEach(function (part, i) {
const arg = args[i + 1]
str += part
if (typeof arg !== 'undefined') {
str += '%s'
}
})
return this.__.apply(this, [str].concat([].slice.call(args, 1)))
}
_enqueueWrite (work: Work) {
this.writeQueue.push(work)
if (this.writeQueue.length === 1) this._processWriteQueue()
}
_processWriteQueue () {
const _this = this
const work = this.writeQueue[0]
// destructure the enqueued work.
const directory = work.directory
const locale = work.locale
const cb = work.cb
const languageFile = this._resolveLocaleFile(directory, locale)
const serializedLocale = JSON.stringify(this.cache[locale], null, 2)
shim.fs.writeFile(languageFile, serializedLocale, 'utf-8', function (err: Error) {
_this.writeQueue.shift()
if (_this.writeQueue.length > 0) _this._processWriteQueue()
cb(err)
})
}
_readLocaleFile () {
let localeLookup = {}
const languageFile = this._resolveLocaleFile(this.directory, this.locale)
try {
// When using a bundler such as webpack, readFileSync may not be defined:
if (shim.fs.readFileSync) {
localeLookup = JSON.parse(shim.fs.readFileSync(languageFile, 'utf-8'))
}
} catch (err) {
if (err instanceof SyntaxError) {
err.message = 'syntax error in ' + languageFile
}
if ((err as { code?: string }).code === 'ENOENT') localeLookup = {}
else throw err
}
this.cache[this.locale] = localeLookup
}
_resolveLocaleFile (directory: string, locale: string) {
let file = shim.resolve(directory, './', locale + '.json')
if (this.fallbackToLanguage && !this._fileExistsSync(file) && ~locale.lastIndexOf('_')) {
// attempt fallback to language only
const languageFile = shim.resolve(directory, './', locale.split('_')[0] + '.json')
if (this._fileExistsSync(languageFile)) file = languageFile
}
return file
}
_fileExistsSync (file: string) {
return shim.exists(file)
}
}
export function y18n (opts: Y18NOpts, _shim: PlatformShim) {
shim = _shim
const y18n = new Y18N(opts)
return {
__: y18n.__.bind(y18n),
__n: y18n.__n.bind(y18n),
setLocale: y18n.setLocale.bind(y18n),
getLocale: y18n.getLocale.bind(y18n),
updateLocale: y18n.updateLocale.bind(y18n),
locale: y18n.locale
}
}