1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _fs = require('fs');
|
8 |
|
9 | var _path = require('path');
|
10 |
|
11 | class I18n {
|
12 | constructor(messages) {
|
13 | this._path = (0, _path.join)(__dirname, '../i18n');
|
14 | this._userMessages = messages || {};
|
15 | this.setLocale('en');
|
16 | }
|
17 |
|
18 | setLocale(locale) {
|
19 | if (!locale || this._locale === locale) return;
|
20 |
|
21 | let temp = this._userMessages[locale] || this._userMessages['*'] || {};
|
22 | let file = (0, _path.join)(this._path, locale + '.json');
|
23 |
|
24 | if ((0, _fs.existsSync)(file)) {
|
25 | this._locale = locale;
|
26 | this._messages = Object.assign(require(file), temp);
|
27 | } else {
|
28 | this._locale = 'en';
|
29 | this._messages = Object.assign(require((0, _path.join)(this._path, './en.json')), temp);
|
30 | }
|
31 | }
|
32 |
|
33 | message(key) {
|
34 | return this._messages[key] || key;
|
35 | }
|
36 | }
|
37 | exports.default = I18n;
|
38 | module.exports = exports['default']; |
\ | No newline at end of file |