not-locale 0.0.19 → 0.0.22
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/index.js +24 -16
- package/package.json +4 -3
- package/src/common/lib.js +80 -73
- package/src/controllers/common/nsLocale.js +155 -151
- package/src/exceptions.js +17 -0
- package/src/locales/en.json +4 -0
- package/src/locales/ru.json +4 -0
- package/src/logics/locale.js +21 -55
- package/src/routes/locale.manifest.js +2 -2
- package/src/routes/locale.ws.js +18 -15
- package/test/index.js +1 -1
package/index.js
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
const {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} = require(
|
|
2
|
+
getMiddleware,
|
|
3
|
+
fromJSON,
|
|
4
|
+
fromDir,
|
|
5
|
+
say,
|
|
6
|
+
sayForModule,
|
|
7
|
+
modulePhrase,
|
|
8
|
+
vocabulary,
|
|
9
|
+
OPTS,
|
|
10
|
+
} = require("./src/common/lib.js");
|
|
11
11
|
|
|
12
12
|
module.exports = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
name: "not-locale",
|
|
14
|
+
paths: {
|
|
15
|
+
controllers: __dirname + "/src/controllers",
|
|
16
|
+
logics: __dirname + "/src/logics",
|
|
17
|
+
locales: __dirname + "/src/locales",
|
|
18
|
+
routes: __dirname + "/src/routes",
|
|
19
|
+
},
|
|
20
|
+
getMiddleware,
|
|
21
|
+
fromJSON,
|
|
22
|
+
fromDir,
|
|
23
|
+
say,
|
|
24
|
+
sayForModule,
|
|
25
|
+
modulePhrase,
|
|
26
|
+
vocabulary,
|
|
27
|
+
OPTS,
|
|
20
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-locale",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "not-* family module for localization in not- environment",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"url": "git+https://github.com/interrupter/not-locale.git"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
|
+
"not-bulma",
|
|
17
18
|
"not-node",
|
|
18
19
|
"locale"
|
|
19
20
|
],
|
|
@@ -24,13 +25,13 @@
|
|
|
24
25
|
},
|
|
25
26
|
"homepage": "https://github.com/interrupter/not-locale#readme",
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"not-log": "^0.0.
|
|
28
|
+
"not-log": "^0.0.20",
|
|
28
29
|
"not-path": "^1.0.1"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"chai": "*",
|
|
32
33
|
"chai-as-promised": "*",
|
|
33
|
-
"eslint": "^8.0
|
|
34
|
+
"eslint": "^8.3.0",
|
|
34
35
|
"ink-docstrap": "^1.3.2",
|
|
35
36
|
"jsdoc": "^3.6.7",
|
|
36
37
|
"mocha": "*",
|
package/src/common/lib.js
CHANGED
|
@@ -9,43 +9,43 @@ const //loadJsonFile = require('load-json-file'),
|
|
|
9
9
|
notPath = require('not-path'),
|
|
10
10
|
path = require('path');
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
let store = {},
|
|
13
13
|
OPTS = {
|
|
14
14
|
default: 'en',
|
|
15
15
|
getter: null
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Express middleware, to determine in which locale should we process response
|
|
20
|
+
*/
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
next();
|
|
22
|
+
function detect(req, res, next) {
|
|
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;
|
|
35
33
|
}
|
|
34
|
+
next();
|
|
35
|
+
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Express middleware initializer, to determine in which locale should we process response
|
|
39
|
+
* @param {object} options - object with `deafult` {string}, `getter` {function} redefined
|
|
40
|
+
* @return {function} function wich will accept three params (req, res, next) and run as express middleware
|
|
41
|
+
*/
|
|
42
42
|
|
|
43
|
-
exports.getMiddleware = (options)=>{
|
|
44
|
-
if (options){
|
|
45
|
-
if (options.default && options.default.length > 1){
|
|
43
|
+
exports.getMiddleware = (options) => {
|
|
44
|
+
if (options) {
|
|
45
|
+
if (options.default && options.default.length > 1) {
|
|
46
46
|
OPTS.default = options.default;
|
|
47
47
|
}
|
|
48
|
-
if (options.getter && typeof options.getter === 'function'){
|
|
48
|
+
if (options.getter && typeof options.getter === 'function') {
|
|
49
49
|
OPTS.getter = options.getter;
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -54,12 +54,12 @@ exports.getMiddleware = (options)=>{
|
|
|
54
54
|
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
exports.fromJSON = (locale, json, prefix = '')=>{
|
|
62
|
-
if(prefix){
|
|
57
|
+
* Add locale by json object
|
|
58
|
+
* @param {string} locale - name of locale
|
|
59
|
+
* @param {json} json - json object
|
|
60
|
+
*/
|
|
61
|
+
exports.fromJSON = (locale, json, prefix = '') => {
|
|
62
|
+
if (prefix) {
|
|
63
63
|
let tmp = {};
|
|
64
64
|
const keys = Object.keys(json);
|
|
65
65
|
keys.forEach((key) => {
|
|
@@ -67,36 +67,36 @@ exports.fromJSON = (locale, json, prefix = '')=>{
|
|
|
67
67
|
});
|
|
68
68
|
json = tmp;
|
|
69
69
|
}
|
|
70
|
-
if (typeof store[locale] !== 'undefined'){
|
|
70
|
+
if (typeof store[locale] !== 'undefined') {
|
|
71
71
|
store[locale] = Object.assign(store[locale], json);
|
|
72
|
-
}else{
|
|
72
|
+
} else {
|
|
73
73
|
store[locale] = Object.assign({}, json);
|
|
74
74
|
}
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
exports.fromDir = (pathToLocales, prefix = '')=>{
|
|
83
|
-
return new Promise((resolve, reject)=>{
|
|
78
|
+
* Load locales from directory, with json files, names as [locale_name].json
|
|
79
|
+
* @param {number} pathToLocales - absolute path to directory
|
|
80
|
+
* @return {Promise}
|
|
81
|
+
*/
|
|
82
|
+
exports.fromDir = (pathToLocales, prefix = '') => {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
84
|
fs.readdir(pathToLocales, (err, items) => {
|
|
85
|
-
if (err){
|
|
85
|
+
if (err) {
|
|
86
86
|
reject(err);
|
|
87
|
-
}else{
|
|
88
|
-
for(let i = 0; i < items.length; i++) {
|
|
87
|
+
} else {
|
|
88
|
+
for (let i = 0; i < items.length; i++) {
|
|
89
89
|
let filename = path.join(pathToLocales, items[i]),
|
|
90
90
|
stats = fs.lstatSync(filename);
|
|
91
|
-
if (stats.isFile()){
|
|
92
|
-
try{
|
|
91
|
+
if (stats.isFile()) {
|
|
92
|
+
try {
|
|
93
93
|
let file = require(filename),
|
|
94
94
|
[localeName] = items[i].split('.');
|
|
95
95
|
exports.fromJSON(localeName, file, prefix);
|
|
96
|
-
}catch(e){
|
|
96
|
+
} catch (e) {
|
|
97
97
|
Log.error(e);
|
|
98
98
|
}
|
|
99
|
-
}else{
|
|
99
|
+
} else {
|
|
100
100
|
continue;
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -107,56 +107,63 @@ exports.fromDir = (pathToLocales, prefix = '')=>{
|
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
function say(phrase, params = {}, locale = OPTS.default){
|
|
116
|
-
try{
|
|
110
|
+
* Returns localized variant of code phrase
|
|
111
|
+
* @param {string} phrase - code phrase
|
|
112
|
+
* @param {array|object} params - array or hash with params for template parser
|
|
113
|
+
* @return {string} localized variant
|
|
114
|
+
*/
|
|
115
|
+
function say(phrase, params = {}, locale = OPTS.default) {
|
|
116
|
+
try {
|
|
117
117
|
let tmpl = store[locale][phrase],
|
|
118
118
|
result = '';
|
|
119
|
-
if (
|
|
119
|
+
if (typeof tmpl === 'undefined') {
|
|
120
|
+
return phrase;
|
|
121
|
+
}
|
|
122
|
+
if (params) {
|
|
120
123
|
return notPath.parseSubs(tmpl, params, {});
|
|
121
|
-
}else{
|
|
124
|
+
} else {
|
|
122
125
|
result = tmpl;
|
|
123
126
|
}
|
|
124
127
|
return result;
|
|
125
|
-
}catch(e){
|
|
128
|
+
} catch (e) {
|
|
126
129
|
Log.error(e);
|
|
127
130
|
}
|
|
128
|
-
}
|
|
131
|
+
}
|
|
129
132
|
|
|
130
|
-
exports.say = say
|
|
133
|
+
exports.say = say;
|
|
131
134
|
|
|
132
135
|
/**
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
exports.vocabulary = () => {
|
|
136
|
+
* Getter for stores of all locales
|
|
137
|
+
* @return {objects} all locales
|
|
138
|
+
*/
|
|
139
|
+
exports.vocabulary = () => {
|
|
140
|
+
return store;
|
|
141
|
+
};
|
|
137
142
|
|
|
138
143
|
/**
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
exports.OPTS = () => {
|
|
144
|
+
* Getter for OPTS variable
|
|
145
|
+
* @return {object} copy of OPTS object
|
|
146
|
+
*/
|
|
147
|
+
exports.OPTS = () => {
|
|
148
|
+
return Object.assign({}, OPTS);
|
|
149
|
+
};
|
|
143
150
|
|
|
144
151
|
|
|
145
|
-
exports.get = (locale)=>{
|
|
146
|
-
if(Object.prototype.hasOwnProperty.call(store, locale)){
|
|
152
|
+
exports.get = (locale) => {
|
|
153
|
+
if (Object.prototype.hasOwnProperty.call(store, locale)) {
|
|
147
154
|
return store[locale];
|
|
148
|
-
}else{
|
|
155
|
+
} else {
|
|
149
156
|
return {};
|
|
150
157
|
}
|
|
151
158
|
};
|
|
152
159
|
|
|
153
160
|
|
|
154
|
-
exports.available = ()=>{
|
|
161
|
+
exports.available = () => {
|
|
155
162
|
return Object.keys(store);
|
|
156
163
|
};
|
|
157
164
|
|
|
158
165
|
|
|
159
|
-
function modulePhrase(moduleName = ''){
|
|
166
|
+
function modulePhrase(moduleName = '') {
|
|
160
167
|
return (phrase) => [moduleName, phrase].join(':');
|
|
161
168
|
}
|
|
162
169
|
|
|
@@ -169,5 +176,5 @@ exports.sayForModule = (moduleName = '') => {
|
|
|
169
176
|
params,
|
|
170
177
|
locale
|
|
171
178
|
);
|
|
172
|
-
}
|
|
179
|
+
};
|
|
173
180
|
};
|
|
@@ -1,164 +1,168 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* detects current locale, loads dictionary from server
|
|
3
|
-
*
|
|
4
|
-
**/
|
|
5
|
-
|
|
6
|
-
const SECTION_ID =
|
|
7
|
-
|
|
8
|
-
import {notCommon, notLocale,
|
|
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
|
-
|
|
2
|
+
* detects current locale, loads dictionary from server
|
|
3
|
+
*
|
|
4
|
+
**/
|
|
5
|
+
|
|
6
|
+
const SECTION_ID = "locale";
|
|
7
|
+
|
|
8
|
+
import { notCommon, notLocale, Frame } from "not-bulma";
|
|
9
|
+
|
|
10
|
+
const { notTopMenu } = Frame;
|
|
11
|
+
|
|
12
|
+
class nsLocale {
|
|
13
|
+
constructor(app) {
|
|
14
|
+
this.app = app;
|
|
15
|
+
this.locales = [];
|
|
16
|
+
this.failures = 0;
|
|
17
|
+
this.app.on("wsClient:main:connected", this.update.bind(this));
|
|
18
|
+
notLocale.on("change", () => {
|
|
19
|
+
this.app.emit("locale");
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates network interface for this service
|
|
25
|
+
*/
|
|
26
|
+
interface(data) {
|
|
27
|
+
return this.app.getInterface("locale")(data);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves dictionary for current locale
|
|
32
|
+
* sets dictionary in notLocale object
|
|
33
|
+
*/
|
|
34
|
+
async update() {
|
|
35
|
+
try {
|
|
36
|
+
await this.updateAvailable();
|
|
37
|
+
let res = await this.interface({
|
|
38
|
+
locale: this.getCurrentLocale(),
|
|
39
|
+
}).$get({});
|
|
40
|
+
if (res.status === "ok" && res.result) {
|
|
41
|
+
notLocale.set(res.result);
|
|
42
|
+
} else {
|
|
43
|
+
this.scheduleUpdate();
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
notCommon.error(e);
|
|
47
|
+
this.scheduleUpdate();
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
|
|
51
|
+
scheduleUpdate() {
|
|
52
|
+
this.failures++;
|
|
53
|
+
if (this.failures < 100) {
|
|
54
|
+
setTimeout(this.update.bind(this), 1000 * this.failures);
|
|
55
|
+
} else {
|
|
56
|
+
notCommon.error("Too many failures of locale loading");
|
|
57
|
+
}
|
|
53
58
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
|
|
60
|
+
async updateAvailable() {
|
|
61
|
+
try {
|
|
62
|
+
let res = await this.interface({}).$available({});
|
|
63
|
+
if (res.status === "ok" && res.result) {
|
|
64
|
+
this.setAvailable(res.result);
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
notCommon.error(e);
|
|
68
|
+
}
|
|
64
69
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}, 1000);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
createMenuItems(list) {
|
|
80
|
-
let items = list.map(this.createMenuItem.bind(this));
|
|
81
|
-
return items;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
createMenuItem(item) {
|
|
85
|
-
return {
|
|
86
|
-
id: `${SECTION_ID}.${item}`,
|
|
87
|
-
section: SECTION_ID,
|
|
88
|
-
title: item,
|
|
89
|
-
classes: ' is-clickable ',
|
|
90
|
-
action: this.changeLocale.bind(this, item)
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
changeLocale(locale){
|
|
95
|
-
this.saveLocaleToStore(locale);
|
|
96
|
-
this.update();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* @returns {string} code of current locale
|
|
101
|
-
**/
|
|
102
|
-
getCurrentLocale(){
|
|
103
|
-
let stored = this.restoreLocaleFromStore();
|
|
104
|
-
if (stored){
|
|
105
|
-
if(this.locales.includes(stored)){
|
|
106
|
-
return stored;
|
|
107
|
-
}
|
|
70
|
+
|
|
71
|
+
updateUI(list) {
|
|
72
|
+
let menuItems = this.createMenuItems(list);
|
|
73
|
+
notTopMenu.updateSectionItems(SECTION_ID, () => {
|
|
74
|
+
return menuItems;
|
|
75
|
+
});
|
|
76
|
+
setTimeout(() => {
|
|
77
|
+
this.app.emit(`tag-${SECTION_ID}:update`, {
|
|
78
|
+
title: this.getCurrentLocale(),
|
|
79
|
+
});
|
|
80
|
+
}, 1000);
|
|
108
81
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
82
|
+
|
|
83
|
+
createMenuItems(list) {
|
|
84
|
+
let items = list.map(this.createMenuItem.bind(this));
|
|
85
|
+
return items;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
createMenuItem(item) {
|
|
89
|
+
return {
|
|
90
|
+
id: `${SECTION_ID}.${item}`,
|
|
91
|
+
section: SECTION_ID,
|
|
92
|
+
title: item,
|
|
93
|
+
classes: " is-clickable ",
|
|
94
|
+
action: this.changeLocale.bind(this, item),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
changeLocale(locale) {
|
|
99
|
+
this.saveLocaleToStore(locale);
|
|
100
|
+
this.update();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @returns {string} code of current locale
|
|
105
|
+
**/
|
|
106
|
+
getCurrentLocale() {
|
|
107
|
+
let stored = this.restoreLocaleFromStore();
|
|
108
|
+
if (stored) {
|
|
109
|
+
if (this.locales.includes(stored)) {
|
|
110
|
+
return stored;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return this.selectBest();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @returns {Promise<Array>} of locales objects {code, title}
|
|
118
|
+
**/
|
|
119
|
+
getAvailable() {
|
|
120
|
+
return this.interface().$available({});
|
|
133
121
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
122
|
+
|
|
123
|
+
setAvailable(list) {
|
|
124
|
+
this.locales = list;
|
|
125
|
+
this.updateUI(list);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
restoreLocaleFromStore() {
|
|
129
|
+
if (window.localStorage) {
|
|
130
|
+
try {
|
|
131
|
+
return window.localStorage.getItem("locale");
|
|
132
|
+
} catch (e) {
|
|
133
|
+
this.app.error(e);
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
143
137
|
return false;
|
|
144
|
-
}
|
|
145
138
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
139
|
+
|
|
140
|
+
saveLocaleToStore(locale) {
|
|
141
|
+
if (window.localStorage) {
|
|
142
|
+
try {
|
|
143
|
+
return window.localStorage.setItem("locale", locale);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
this.app.error(e);
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
157
150
|
}
|
|
158
|
-
return this.app.getWorking('locale', this.app.getOptions('modules.locale.default', 'ru'));
|
|
159
|
-
}
|
|
160
151
|
|
|
152
|
+
selectBest() {
|
|
153
|
+
if (navigator.languages) {
|
|
154
|
+
let locale = navigator.languages.find((itm) => {
|
|
155
|
+
return this.locales.includes(itm);
|
|
156
|
+
});
|
|
157
|
+
if (locale) {
|
|
158
|
+
return locale;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return this.app.getWorking(
|
|
162
|
+
"locale",
|
|
163
|
+
this.app.getOptions("modules.locale.default", "ru")
|
|
164
|
+
);
|
|
165
|
+
}
|
|
161
166
|
}
|
|
162
167
|
|
|
163
|
-
|
|
164
168
|
export default nsLocale;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const { notError } = require("not-error");
|
|
2
|
+
|
|
3
|
+
class LocaleExceptionGetError extends notError {
|
|
4
|
+
constructor(params = {}, cause) {
|
|
5
|
+
super(`not-locale:exception_logic_get`, params, cause);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports.LocaleExceptionGetError = LocaleExceptionGetError;
|
|
10
|
+
|
|
11
|
+
class LocaleExceptionAvailableError extends notError {
|
|
12
|
+
constructor(params = {}, cause) {
|
|
13
|
+
super(`not-locale:exception_logic_available`, params, cause);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports.LocaleExceptionAvailableError = LocaleExceptionAvailableError;
|
package/src/logics/locale.js
CHANGED
|
@@ -1,63 +1,29 @@
|
|
|
1
|
-
const
|
|
2
|
-
const Log = require('not-log')(module, 'locale:logics');
|
|
1
|
+
const notLocale = require("../common/lib.js");
|
|
3
2
|
const {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const MODEL_NAME = 'Locale';
|
|
3
|
+
LocaleExceptionGetError,
|
|
4
|
+
LocaleExceptionAvailableError,
|
|
5
|
+
} = require("../exceptions");
|
|
6
|
+
const MODEL_NAME = "Locale";
|
|
10
7
|
exports.thisLogicName = MODEL_NAME;
|
|
11
8
|
|
|
12
9
|
class LocaleLogic {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
}catch(err){
|
|
23
|
-
Log.error(err);
|
|
24
|
-
notNode.Application.report(
|
|
25
|
-
new notError(
|
|
26
|
-
`locale:logic.get`,
|
|
27
|
-
{locale},
|
|
28
|
-
err
|
|
29
|
-
)
|
|
30
|
-
);
|
|
31
|
-
return {
|
|
32
|
-
status: 'error',
|
|
33
|
-
error: err.message
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
static async available() {
|
|
39
|
-
try{
|
|
40
|
-
let result = notLocale.available();
|
|
41
|
-
return {
|
|
42
|
-
status: 'ok',
|
|
43
|
-
result
|
|
44
|
-
};
|
|
45
|
-
}catch(err){
|
|
46
|
-
Log.error(err);
|
|
47
|
-
notNode.Application.report(
|
|
48
|
-
new notError(
|
|
49
|
-
`locale:logic.available`,
|
|
50
|
-
{},
|
|
51
|
-
err
|
|
52
|
-
)
|
|
53
|
-
);
|
|
54
|
-
return {
|
|
55
|
-
status: 'error',
|
|
56
|
-
error: err.message
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
10
|
+
static async get({ locale }) {
|
|
11
|
+
try {
|
|
12
|
+
let result = notLocale.get(locale);
|
|
13
|
+
return result;
|
|
14
|
+
} catch (err) {
|
|
15
|
+
throw new LocaleExceptionGetError({ locale }, err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
60
18
|
|
|
19
|
+
static async available() {
|
|
20
|
+
try {
|
|
21
|
+
let result = notLocale.available();
|
|
22
|
+
return result;
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw new LocaleExceptionAvailableError({}, err);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
61
27
|
}
|
|
62
28
|
|
|
63
29
|
exports[MODEL_NAME] = LocaleLogic;
|
package/src/routes/locale.ws.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
const notNode = require(
|
|
1
|
+
const notNode = require("not-node");
|
|
2
2
|
|
|
3
|
-
async function get({data}){
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
async function get({ data }) {
|
|
4
|
+
return await notNode.Application.getLogic("Locale").get({
|
|
5
|
+
locale: data.locale,
|
|
6
|
+
});
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
async function available(){
|
|
9
|
-
|
|
10
|
-
return result;
|
|
9
|
+
async function available() {
|
|
10
|
+
return await notNode.Application.getLogic("Locale").available();
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
module.exports = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
servers: {
|
|
15
|
+
//collection type
|
|
16
|
+
main: {
|
|
17
|
+
//collection name
|
|
18
|
+
request: {
|
|
19
|
+
//routes(end-points) type
|
|
20
|
+
get, //end-points
|
|
21
|
+
available,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
22
25
|
};
|
package/test/index.js
CHANGED