not-locale 0.0.24 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/common/lib.js +97 -105
- package/src/const.js +1 -0
- package/src/logics/locale.js +21 -4
package/package.json
CHANGED
package/src/common/lib.js
CHANGED
|
@@ -4,34 +4,34 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const //loadJsonFile = require('load-json-file'),
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
Log = require("not-log")(module, "not-locale"),
|
|
8
|
+
fs = require("fs"),
|
|
9
|
+
notPath = require("not-path"),
|
|
10
|
+
path = require("path");
|
|
11
11
|
|
|
12
12
|
let store = {},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
OPTS = {
|
|
14
|
+
default: "en",
|
|
15
|
+
getter: null,
|
|
16
|
+
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Express middleware, to determine in which locale should we process response
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
function detect(req, res, next) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
let reqLang;
|
|
24
|
+
if (OPTS.getter) {
|
|
25
|
+
reqLang = OPTS.getter(req);
|
|
26
|
+
} else {
|
|
27
|
+
reqLang = req.get("Accept-Language");
|
|
28
|
+
}
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(store, reqLang)) {
|
|
30
|
+
res.locals.locale = reqLang;
|
|
31
|
+
} else {
|
|
32
|
+
res.locals.locale = OPTS.default;
|
|
33
|
+
}
|
|
34
|
+
next();
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -41,37 +41,36 @@ function detect(req, res, next) {
|
|
|
41
41
|
*/
|
|
42
42
|
|
|
43
43
|
exports.getMiddleware = (options) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
if (options) {
|
|
45
|
+
if (options.default && options.default.length > 1) {
|
|
46
|
+
OPTS.default = options.default;
|
|
47
|
+
}
|
|
48
|
+
if (options.getter && typeof options.getter === "function") {
|
|
49
|
+
OPTS.getter = options.getter;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return detect;
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
|
|
56
55
|
/**
|
|
57
56
|
* Add locale by json object
|
|
58
57
|
* @param {string} locale - name of locale
|
|
59
58
|
* @param {json} json - json object
|
|
60
59
|
*/
|
|
61
|
-
exports.fromJSON = (locale, json, prefix =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
60
|
+
exports.fromJSON = (locale, json, prefix = "") => {
|
|
61
|
+
if (prefix) {
|
|
62
|
+
let tmp = {};
|
|
63
|
+
const keys = Object.keys(json);
|
|
64
|
+
keys.forEach((key) => {
|
|
65
|
+
tmp[`${prefix}:${key}`] = json[key];
|
|
66
|
+
});
|
|
67
|
+
json = tmp;
|
|
68
|
+
}
|
|
69
|
+
if (typeof store[locale] !== "undefined") {
|
|
70
|
+
store[locale] = Object.assign(store[locale], json);
|
|
71
|
+
} else {
|
|
72
|
+
store[locale] = Object.assign({}, json);
|
|
73
|
+
}
|
|
75
74
|
};
|
|
76
75
|
|
|
77
76
|
/**
|
|
@@ -79,31 +78,31 @@ exports.fromJSON = (locale, json, prefix = '') => {
|
|
|
79
78
|
* @param {number} pathToLocales - absolute path to directory
|
|
80
79
|
* @return {Promise}
|
|
81
80
|
*/
|
|
82
|
-
exports.fromDir = (pathToLocales, prefix =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
81
|
+
exports.fromDir = (pathToLocales, prefix = "") => {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
fs.readdir(pathToLocales, (err, items) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
reject(err);
|
|
86
|
+
} else {
|
|
87
|
+
for (let i = 0; i < items.length; i++) {
|
|
88
|
+
let filename = path.join(pathToLocales, items[i]),
|
|
89
|
+
stats = fs.lstatSync(filename);
|
|
90
|
+
if (stats.isFile()) {
|
|
91
|
+
try {
|
|
92
|
+
let file = require(filename),
|
|
93
|
+
[localeName] = items[i].split(".");
|
|
94
|
+
exports.fromJSON(localeName, file, prefix);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
Log.error(e);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
resolve();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
107
106
|
};
|
|
108
107
|
|
|
109
108
|
/**
|
|
@@ -113,21 +112,21 @@ exports.fromDir = (pathToLocales, prefix = '') => {
|
|
|
113
112
|
* @return {string} localized variant
|
|
114
113
|
*/
|
|
115
114
|
function say(phrase, params = {}, locale = OPTS.default) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
115
|
+
try {
|
|
116
|
+
let tmpl = store[locale] ? store[locale][phrase] : undefined,
|
|
117
|
+
result = "";
|
|
118
|
+
if (typeof tmpl === "undefined") {
|
|
119
|
+
return phrase;
|
|
120
|
+
}
|
|
121
|
+
if (params) {
|
|
122
|
+
return notPath.parseSubs(tmpl, params, {});
|
|
123
|
+
} else {
|
|
124
|
+
result = tmpl;
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
} catch (e) {
|
|
128
|
+
Log.error(e);
|
|
129
|
+
}
|
|
131
130
|
}
|
|
132
131
|
|
|
133
132
|
exports.say = say;
|
|
@@ -137,7 +136,7 @@ exports.say = say;
|
|
|
137
136
|
* @return {objects} all locales
|
|
138
137
|
*/
|
|
139
138
|
exports.vocabulary = () => {
|
|
140
|
-
|
|
139
|
+
return store;
|
|
141
140
|
};
|
|
142
141
|
|
|
143
142
|
/**
|
|
@@ -145,36 +144,29 @@ exports.vocabulary = () => {
|
|
|
145
144
|
* @return {object} copy of OPTS object
|
|
146
145
|
*/
|
|
147
146
|
exports.OPTS = () => {
|
|
148
|
-
|
|
147
|
+
return Object.assign({}, OPTS);
|
|
149
148
|
};
|
|
150
149
|
|
|
151
|
-
|
|
152
150
|
exports.get = (locale) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
151
|
+
if (Object.prototype.hasOwnProperty.call(store, locale)) {
|
|
152
|
+
return store[locale];
|
|
153
|
+
} else {
|
|
154
|
+
return {};
|
|
155
|
+
}
|
|
158
156
|
};
|
|
159
157
|
|
|
160
|
-
|
|
161
158
|
exports.available = () => {
|
|
162
|
-
|
|
159
|
+
return Object.keys(store);
|
|
163
160
|
};
|
|
164
161
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return (phrase) => [moduleName, phrase].join(':');
|
|
162
|
+
function modulePhrase(moduleName = "") {
|
|
163
|
+
return (phrase) => [moduleName, phrase].join(":");
|
|
168
164
|
}
|
|
169
165
|
|
|
170
166
|
exports.modulePhrase = modulePhrase;
|
|
171
167
|
|
|
172
|
-
exports.sayForModule = (moduleName =
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
params,
|
|
177
|
-
locale
|
|
178
|
-
);
|
|
179
|
-
};
|
|
168
|
+
exports.sayForModule = (moduleName = "") => {
|
|
169
|
+
return (phrase, params = {}, locale = OPTS.default) => {
|
|
170
|
+
return say([moduleName, phrase].join(":"), params, locale);
|
|
171
|
+
};
|
|
180
172
|
};
|
package/src/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports.MODULE_NAME = "not-locale";
|
package/src/logics/locale.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
const notLocale = require("../common/lib.js");
|
|
2
|
+
const { MODULE_NAME } = require("../const");
|
|
3
|
+
const config = require("not-config").readerForModule(MODULE_NAME);
|
|
4
|
+
|
|
2
5
|
const {
|
|
3
6
|
LocaleExceptionGetError,
|
|
4
7
|
LocaleExceptionAvailableError,
|
|
@@ -10,8 +13,11 @@ exports.thisLogicName = MODEL_NAME;
|
|
|
10
13
|
class LocaleLogic {
|
|
11
14
|
static async get({ locale }) {
|
|
12
15
|
try {
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
const available = await this.available();
|
|
17
|
+
if (available.includes(locale)) {
|
|
18
|
+
return notLocale.get(locale);
|
|
19
|
+
}
|
|
20
|
+
throw new Error("Locale not available");
|
|
15
21
|
} catch (err) {
|
|
16
22
|
throw new LocaleExceptionGetError({ locale }, err);
|
|
17
23
|
}
|
|
@@ -19,8 +25,19 @@ class LocaleLogic {
|
|
|
19
25
|
|
|
20
26
|
static async available() {
|
|
21
27
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
const foundedLocales = notLocale.available();
|
|
29
|
+
const configPermitedLocales = config.get("available");
|
|
30
|
+
if (
|
|
31
|
+
configPermitedLocales &&
|
|
32
|
+
Array.isArray(configPermitedLocales) &&
|
|
33
|
+
configPermitedLocales.length
|
|
34
|
+
) {
|
|
35
|
+
return config.permits.filter((name) =>
|
|
36
|
+
foundedLocales.includes(name)
|
|
37
|
+
);
|
|
38
|
+
} else {
|
|
39
|
+
return foundedLocales;
|
|
40
|
+
}
|
|
24
41
|
} catch (err) {
|
|
25
42
|
throw new LocaleExceptionAvailableError({}, err);
|
|
26
43
|
}
|