not-locale 0.0.21 → 0.0.24

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 CHANGED
@@ -1,20 +1,28 @@
1
1
  const {
2
- getMiddleware,
3
- fromJSON,
4
- fromDir,
5
- say,
6
- sayForModule,
7
- modulePhrase,
8
- vocabulary,
9
- OPTS
10
- } = require('./src/common/lib.js');
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
- name: 'not-locale',
14
- paths:{
15
- controllers: __dirname + '/src/controllers',
16
- logics: __dirname + '/src/logics',
17
- routes: __dirname + '/src/routes',
18
- },
19
- getMiddleware, fromJSON, fromDir, say, sayForModule, modulePhrase, vocabulary, OPTS
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.21",
3
+ "version": "0.0.24",
4
4
  "description": "not-* family module for localization in not- environment",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -37,4 +37,4 @@
37
37
  "mocha": "*",
38
38
  "nyc": "*"
39
39
  }
40
- }
40
+ }
@@ -12,7 +12,7 @@ const manifest = {
12
12
  sections:[
13
13
  {
14
14
  id: 'locale',
15
- title: '',
15
+ title: '',
16
16
  icon: {
17
17
  font: 'language',
18
18
  size: 'medium',
@@ -3,170 +3,166 @@
3
3
  *
4
4
  **/
5
5
 
6
- const SECTION_ID = 'locale';
6
+ const SECTION_ID = "locale";
7
7
 
8
- import {
9
- notCommon,
10
- notLocale,
11
- Frame
12
- } from 'not-bulma';
8
+ import { notCommon, notLocale, Frame } from "not-bulma";
13
9
 
14
- const {notTopMenu} = Frame;
10
+ const { notTopMenu } = Frame;
15
11
 
16
12
  class nsLocale {
17
- constructor(app) {
18
- this.app = app;
19
- this.locales = [];
20
- this.failures = 0;
21
- this.app.on('wsClient:main:connected', this.update.bind(this));
22
- notLocale.on('change', () => {
23
- this.app.emit('locale');
24
- });
25
- }
26
-
27
- /**
28
- * Creates network interface for this service
29
- */
30
- interface(data) {
31
- return this.app.getInterface('locale')(data);
32
- }
33
-
34
- /**
35
- * Retrieves dictionary for current locale
36
- * sets dictionary in notLocale object
37
- */
38
- async update() {
39
- try {
40
- await this.updateAvailable();
41
- let res = await this.interface({
42
- locale: this.getCurrentLocale()
43
- }).$get({});
44
- if (res.status === 'ok' && res.result) {
45
- notLocale.set(res.result);
46
- } else {
47
- this.scheduleUpdate();
48
- }
49
- } catch (e) {
50
- notCommon.error(e);
51
- this.scheduleUpdate();
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
+ });
52
21
  }
53
- }
54
-
55
- scheduleUpdate() {
56
- this.failures++;
57
- if (this.failures < 100) {
58
- setTimeout(this.update.bind(this), 1000 * this.failures);
59
- } else {
60
- notCommon.error('Too many failures of locale loading');
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
+ }
61
49
  }
62
- }
63
-
64
- async updateAvailable() {
65
- try {
66
- let res = await this.interface({}).$available({});
67
- if (res.status === 'ok' && res.result) {
68
- this.setAvailable(res.result);
69
- }
70
- } catch (e) {
71
- notCommon.error(e);
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
+ }
72
58
  }
73
- }
74
-
75
- updateUI(list) {
76
- let menuItems = this.createMenuItems(list);
77
- notTopMenu.updateSectionItems(SECTION_ID, () => {
78
- return menuItems;
79
- });
80
- setTimeout(() => {
81
- this.app.emit(`tag-${SECTION_ID}:update`, {
82
- title: this.getCurrentLocale()
83
- });
84
- }, 1000);
85
- }
86
-
87
- createMenuItems(list) {
88
- let items = list.map(this.createMenuItem.bind(this));
89
- return items;
90
- }
91
-
92
- createMenuItem(item) {
93
- return {
94
- id: `${SECTION_ID}.${item}`,
95
- section: SECTION_ID,
96
- title: item,
97
- classes: ' is-clickable ',
98
- action: this.changeLocale.bind(this, item)
99
- };
100
- }
101
-
102
- changeLocale(locale) {
103
- this.saveLocaleToStore(locale);
104
- this.update();
105
- }
106
-
107
- /**
108
- * @returns {string} code of current locale
109
- **/
110
- getCurrentLocale() {
111
- let stored = this.restoreLocaleFromStore();
112
- if (stored) {
113
- if (this.locales.includes(stored)) {
114
- return stored;
115
- }
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
+ }
116
69
  }
117
- return this.selectBest();
118
- }
119
-
120
- /**
121
- * @returns {Promise<Array>} of locales objects {code, title}
122
- **/
123
- getAvailable() {
124
- return this.interface().$available({});
125
- }
126
-
127
-
128
- setAvailable(list) {
129
- this.locales = list;
130
- this.updateUI(list);
131
- }
132
-
133
- restoreLocaleFromStore() {
134
- if (window.localStorage) {
135
- try {
136
- return window.localStorage.getItem('locale');
137
- } catch (e) {
138
- this.app.error(e);
139
- return false;
140
- }
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);
81
+ }
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
+ class: " 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({});
121
+ }
122
+
123
+ setAvailable(list) {
124
+ this.locales = list;
125
+ this.updateUI(list);
141
126
  }
142
- return false;
143
- }
144
-
145
- saveLocaleToStore(locale) {
146
- if (window.localStorage) {
147
- try {
148
- return window.localStorage.setItem('locale', locale);
149
- } catch (e) {
150
- this.app.error(e);
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
+ }
151
137
  return false;
152
- }
153
138
  }
154
- return false;
155
- }
156
-
157
- selectBest() {
158
- if (navigator.languages) {
159
- let locale = navigator.languages.find((itm) => {
160
- return this.locales.includes(itm);
161
- });
162
- if (locale) {
163
- return locale;
164
- }
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;
165
150
  }
166
- return this.app.getWorking('locale', this.app.getOptions('modules.locale.default', 'ru'));
167
- }
168
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
+ }
169
166
  }
170
167
 
171
-
172
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;
@@ -0,0 +1,4 @@
1
+ {
2
+ "exception_logic_available": "Exception while listing available locales",
3
+ "exception_logic_get": "Exception while retrieving locale library"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "exception_logic_available": "Исключение при листинге доступных локалей",
3
+ "exception_logic_get": "Исключение при получении локали"
4
+ }
@@ -1,63 +1,30 @@
1
- const notNode = require('not-node');
2
- const Log = require('not-log')(module, 'locale:logics');
1
+ const notLocale = require("../common/lib.js");
3
2
  const {
4
- notError
5
- } = require('not-error');
3
+ LocaleExceptionGetError,
4
+ LocaleExceptionAvailableError,
5
+ } = require("../exceptions");
6
6
 
7
- const notLocale = require('../common/lib.js');
8
-
9
- const MODEL_NAME = 'Locale';
7
+ const MODEL_NAME = "Locale";
10
8
  exports.thisLogicName = MODEL_NAME;
11
9
 
12
10
  class LocaleLogic {
13
- static async get({
14
- locale
15
- }){
16
- try{
17
- let result = notLocale.get(locale);
18
- return {
19
- status: 'ok',
20
- result
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
- }
11
+ static async get({ locale }) {
12
+ try {
13
+ let result = notLocale.get(locale);
14
+ return result;
15
+ } catch (err) {
16
+ throw new LocaleExceptionGetError({ locale }, err);
17
+ }
18
+ }
60
19
 
20
+ static async available() {
21
+ try {
22
+ let result = notLocale.available();
23
+ return result;
24
+ } catch (err) {
25
+ throw new LocaleExceptionAvailableError({}, err);
26
+ }
27
+ }
61
28
  }
62
29
 
63
30
  exports[MODEL_NAME] = LocaleLogic;
@@ -1,55 +1,58 @@
1
- const Log = require('not-log')(module, 'locale:route');
2
- const {notError} = require('not-error');
3
- const notNode = require('not-node');
1
+ const Log = require("not-log")(module, "locale:route");
2
+ const { notError } = require("not-error");
3
+ const notNode = require("not-node");
4
4
 
5
- async function get(req, res){
6
- try{
7
- let locale = req.params.locale;
8
- let result = await notNode.Application.getLogic('Locale').get({locale});
9
- res.status(200).json(result);
10
- }catch(err){
11
- Log.error(err);
12
- notNode.Application.report(
13
- new notError(
14
- `locale:route.get`, {
15
- locale: req.query.locale,
16
- owner: req.user?req.user._id:undefined,
17
- ownerModel: 'User'
18
- },
19
- err
20
- )
21
- );
22
- res.status(500).json({
23
- status: 'error'
24
- });
25
- }
5
+ async function get(req, res) {
6
+ try {
7
+ let locale = req.params.locale;
8
+ let result = await notNode.Application.getLogic("Locale").get({
9
+ locale,
10
+ });
11
+ res.status(200).json(result);
12
+ } catch (err) {
13
+ Log.error(err);
14
+ notNode.Application.report(
15
+ new notError(
16
+ `locale:route.get`,
17
+ {
18
+ locale: req.query.locale,
19
+ owner: req.user ? req.user._id : undefined,
20
+ ownerModel: "User",
21
+ },
22
+ err
23
+ )
24
+ );
25
+ res.status(500).json({
26
+ status: "error",
27
+ });
28
+ }
26
29
  }
27
30
 
28
- async function available(req, res){
29
- try{
30
- let result = await notNode.Application.getLogic('Locale').available();
31
- res.status(200).json(result);
32
- }catch(err){
33
- Log.error(err);
34
- notNode.Application.report(
35
- new notError(
36
- `locale:route.available`, {
37
- owner: req.user?req.user._id:undefined,
38
- ownerModel: 'User'
39
- },
40
- err
41
- )
42
- );
43
- res.status(500).json({
44
- status: 'error'
45
- });
46
- }
31
+ async function available(req, res) {
32
+ try {
33
+ let result = await notNode.Application.getLogic("Locale").available();
34
+ res.status(200).json(result);
35
+ } catch (err) {
36
+ Log.error(err);
37
+ notNode.Application.report(
38
+ new notError(
39
+ `locale:route.available`,
40
+ {
41
+ owner: req.user ? req.user._id : undefined,
42
+ ownerModel: "User",
43
+ },
44
+ err
45
+ )
46
+ );
47
+ res.status(500).json({
48
+ status: "error",
49
+ });
50
+ }
47
51
  }
48
52
 
49
-
50
53
  module.exports = {
51
- get,
52
- _get: get,
53
- available,
54
- _available: available,
54
+ get,
55
+ _get: get,
56
+ available,
57
+ _available: available,
55
58
  };
@@ -1,37 +1,43 @@
1
- const {error} = require('not-log')(module, 'locale:route');
1
+ const { error } = require("not-log")(module, "locale:route");
2
2
 
3
3
  try {
4
- module.exports = {
5
- model: 'locale',
6
- url: '/api/:modelName',
7
- fields: {},
8
- actions: {
9
- get: {
10
- ws: true,
11
- method: 'GET',
12
- isArray: false,
13
- postFix: '/:actionName',
14
- data: ['record'],
15
- rules: [{
16
- auth: true
17
- }, {
18
- auth: false
19
- }]
20
- },
21
- available:{
22
- ws: true,
23
- method: 'GET',
24
- isArray: false,
25
- postFix: '/:actionName',
26
- data: ['record'],
27
- rules: [{
28
- auth: true
29
- },{
30
- auth: false
31
- }]
32
- }
33
- }
34
- };
4
+ module.exports = {
5
+ model: "locale",
6
+ url: "/api/:modelName",
7
+ fields: {},
8
+ actions: {
9
+ get: {
10
+ ws: true,
11
+ method: "GET",
12
+ isArray: false,
13
+ postFix: "/:actionName",
14
+ data: ["record"],
15
+ rules: [
16
+ {
17
+ auth: true,
18
+ },
19
+ {
20
+ auth: false,
21
+ },
22
+ ],
23
+ },
24
+ available: {
25
+ ws: true,
26
+ method: "GET",
27
+ isArray: false,
28
+ postFix: "/:actionName",
29
+ data: ["record"],
30
+ rules: [
31
+ {
32
+ auth: true,
33
+ },
34
+ {
35
+ auth: false,
36
+ },
37
+ ],
38
+ },
39
+ },
40
+ };
35
41
  } catch (e) {
36
- error(e);
42
+ error(e);
37
43
  }
@@ -1,22 +1,25 @@
1
- const notNode = require('not-node');
1
+ const notNode = require("not-node");
2
2
 
3
- async function get({data}){
4
- let result = await notNode.Application.getLogic('Locale').get({locale: data.locale});
5
- return result;
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
- let result = await notNode.Application.getLogic('Locale').available();
10
- return result;
9
+ async function available() {
10
+ return await notNode.Application.getLogic("Locale").available();
11
11
  }
12
12
 
13
13
  module.exports = {
14
- servers:{ //collection type
15
- main:{ //collection name
16
- request: { //routes(end-points) type
17
- get, //end-points
18
- available
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
  };