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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-locale",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "description": "not-* family module for localization in not- environment",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/common/lib.js CHANGED
@@ -4,34 +4,34 @@
4
4
  */
5
5
 
6
6
  const //loadJsonFile = require('load-json-file'),
7
- Log = require('not-log')(module, 'not-locale'),
8
- fs = require('fs'),
9
- notPath = require('not-path'),
10
- path = require('path');
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
- OPTS = {
14
- default: 'en',
15
- getter: null
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
- 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();
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
- 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;
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
- if (prefix) {
63
- let tmp = {};
64
- const keys = Object.keys(json);
65
- keys.forEach((key) => {
66
- tmp[`${prefix}:${key}`] = json[key];
67
- });
68
- json = tmp;
69
- }
70
- if (typeof store[locale] !== 'undefined') {
71
- store[locale] = Object.assign(store[locale], json);
72
- } else {
73
- store[locale] = Object.assign({}, json);
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
- return new Promise((resolve, reject) => {
84
- fs.readdir(pathToLocales, (err, items) => {
85
- if (err) {
86
- reject(err);
87
- } else {
88
- for (let i = 0; i < items.length; i++) {
89
- let filename = path.join(pathToLocales, items[i]),
90
- stats = fs.lstatSync(filename);
91
- if (stats.isFile()) {
92
- try {
93
- let file = require(filename),
94
- [localeName] = items[i].split('.');
95
- exports.fromJSON(localeName, file, prefix);
96
- } catch (e) {
97
- Log.error(e);
98
- }
99
- } else {
100
- continue;
101
- }
102
- }
103
- resolve();
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
- try {
117
- let tmpl = store[locale][phrase],
118
- result = '';
119
- if (typeof tmpl === 'undefined') {
120
- return phrase;
121
- }
122
- if (params) {
123
- return notPath.parseSubs(tmpl, params, {});
124
- } else {
125
- result = tmpl;
126
- }
127
- return result;
128
- } catch (e) {
129
- Log.error(e);
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
- return store;
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
- return Object.assign({}, OPTS);
147
+ return Object.assign({}, OPTS);
149
148
  };
150
149
 
151
-
152
150
  exports.get = (locale) => {
153
- if (Object.prototype.hasOwnProperty.call(store, locale)) {
154
- return store[locale];
155
- } else {
156
- return {};
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
- return Object.keys(store);
159
+ return Object.keys(store);
163
160
  };
164
161
 
165
-
166
- function modulePhrase(moduleName = '') {
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
- return (phrase, params = {}, locale = OPTS.default) => {
174
- return say(
175
- [moduleName, phrase].join(':'),
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";
@@ -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
- let result = notLocale.get(locale);
14
- return result;
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
- let result = notLocale.available();
23
- return result;
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
  }