-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi18n.js
36 lines (34 loc) · 1.25 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
(function (global, factory) {
/* eslint-disable */
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.i18n = factory())
/* eslint-enable */
}(this, function () {
/**
* @param {String} options.locale
* @param {String[]} options.locales
* @return {Function} t
* e.g. const t = i18n({ locale: 'jp', locales: ['en', 'cn', 'jp'] })
*/
return function i18n (options) {
var locale = options.locale
var locales = options.locales
var localeIdx = locales.indexOf(locale)
if (localeIdx === -1) {
throw new Error(locale + ' is not included in ' + locales)
}
/**
* @param {...String[]} translations (must be consistent with `locales`)
* @return {String} (translation in `locale`)
* e.g. t('Hello', '你好', 'こんにちは') => 'こんにちは'
*/
return function t () {
var translations = [].slice.call(arguments)
if (translations.length !== locales.length) {
throw new Error(translations[0] + ': missing translation(s) in ' + locales.slice(translations.length))
}
return translations[localeIdx]
}
}
}))