@sofit/view-locale 0.1.7 → 0.2.0

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.
Files changed (35) hide show
  1. package/.circleci/config.yml +12 -1
  2. package/coverage/jest/results.xml +12 -0
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.js +9 -1
  5. package/dist/joi/joi.interface.d.ts +12 -0
  6. package/dist/joi/joi.interface.js +2 -0
  7. package/dist/runner.d.ts +3 -2
  8. package/dist/runner.js +21 -13
  9. package/dist/translations/common/buttons.d.ts +2 -6
  10. package/dist/translations/common/database-errors.d.ts +2 -0
  11. package/dist/translations/common/database-errors.js +12 -0
  12. package/dist/translations/common/joi.d.ts +2 -0
  13. package/dist/translations/common/joi.js +16 -0
  14. package/dist/translations/index.d.ts +4 -2
  15. package/dist/translations/index.js +8 -5
  16. package/dist/translations/modules/gate/gate.d.ts +2 -0
  17. package/dist/translations/modules/gate/gate.js +8 -0
  18. package/dist/translations/translation.interface.d.ts +4 -0
  19. package/dist/translations/translation.interface.js +2 -0
  20. package/jest.config.json +10 -0
  21. package/lib/index.ts +4 -1
  22. package/lib/joi/joi.interface.ts +13 -0
  23. package/lib/runner.ts +19 -17
  24. package/lib/tests/basic.spec.ts +49 -0
  25. package/lib/translations/common/buttons.ts +3 -1
  26. package/lib/translations/common/database-errors.ts +12 -0
  27. package/lib/translations/common/joi.ts +16 -0
  28. package/lib/translations/index.ts +4 -2
  29. package/lib/translations/modules/gate/gate.ts +8 -0
  30. package/lib/translations/translation.interface.ts +5 -0
  31. package/package.json +9 -6
  32. package/dist/translations/modules/vehicle.d.ts +0 -18
  33. package/dist/translations/modules/vehicle.js +0 -20
  34. package/lib/tests/vehicle.spec.ts +0 -43
  35. package/lib/translations/modules/vehicle.ts +0 -20
@@ -22,13 +22,17 @@ jobs:
22
22
  # fallback to using the latest cache if no exact match is found
23
23
  - v1-dependencies-
24
24
 
25
- - run: npm install --production
25
+ - run: npm install
26
26
 
27
27
  - save_cache:
28
28
  paths:
29
29
  - node_modules
30
30
  key: v1-dependencies-{{ checksum "package.json" }}
31
31
 
32
+ - run:
33
+ name: Build from typescript to javascript
34
+ command: npm run build
35
+
32
36
  - run:
33
37
  name: Run code quality
34
38
  command: npm run lint
@@ -37,6 +41,13 @@ jobs:
37
41
  name: Run tests
38
42
  command: npm test
39
43
 
44
+ - store_artifacts:
45
+ path: ~/repo/coverage/lcov-report
46
+ destination: tests
47
+
48
+ - store_test_results:
49
+ path: ~/repo/coverage
50
+
40
51
  - persist_to_workspace:
41
52
  root: ~/repo
42
53
  paths: .
@@ -0,0 +1,12 @@
1
+ <testsuites name="Sofit view locale tests" tests="4" failures="0" time="3.04">
2
+ <testsuite name="Vehicle locale test" errors="0" failures="0" skipped="0" timestamp="2018-07-20T13:20:30" time="2.202" tests="4">
3
+ <testcase classname="Vehicle locale test Should get translations with reach" name="Vehicle locale test Should get translations with reach" time="0.006">
4
+ </testcase>
5
+ <testcase classname="Vehicle locale test Should replace correctly with reach" name="Vehicle locale test Should replace correctly with reach" time="0">
6
+ </testcase>
7
+ <testcase classname="Vehicle locale test Should get translations with raw" name="Vehicle locale test Should get translations with raw" time="0.001">
8
+ </testcase>
9
+ <testcase classname="Vehicle locale test Should replace correctly with raw" name="Vehicle locale test Should replace correctly with raw" time="0">
10
+ </testcase>
11
+ </testsuite>
12
+ </testsuites>
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
- export { locale, Locale } from './runner';
1
+ import * as i18n from './translations';
2
+ export { Locale } from './runner';
3
+ export { i18n };
package/dist/index.js CHANGED
@@ -1,5 +1,13 @@
1
1
  "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ const i18n = __importStar(require("./translations"));
11
+ exports.i18n = i18n;
3
12
  var runner_1 = require("./runner");
4
- exports.locale = runner_1.locale;
5
13
  exports.Locale = runner_1.Locale;
@@ -0,0 +1,12 @@
1
+ export interface IContext {
2
+ key?: string;
3
+ label?: string;
4
+ limit?: number;
5
+ value?: string;
6
+ }
7
+ export interface IValidationErrorItem {
8
+ message: string;
9
+ type: string;
10
+ path: string[];
11
+ context?: IContext;
12
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/runner.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { IValidationErrorItem } from './joi/joi.interface';
1
2
  import { IAccessKey, LANGUAGES } from './languages';
2
3
  export interface IReplaces {
3
4
  [key: string]: string;
@@ -6,6 +7,6 @@ export declare class Locale {
6
7
  private readonly language;
7
8
  constructor(language: LANGUAGES);
8
9
  reach(accessKey: IAccessKey, replaces?: IReplaces): string;
9
- raw(context: any, path: string, replaces?: IReplaces): string;
10
+ raw(path: string, replaces?: IReplaces): string;
11
+ joi(detail: IValidationErrorItem): string;
10
12
  }
11
- export declare function locale(language: LANGUAGES): Locale;
package/dist/runner.js CHANGED
@@ -1,5 +1,13 @@
1
1
  "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ const TRANSLATIONS = __importStar(require("./translations"));
3
11
  class Locale {
4
12
  constructor(language) {
5
13
  this.language = language;
@@ -19,9 +27,10 @@ class Locale {
19
27
  }
20
28
  return 'Translation not found';
21
29
  }
22
- raw(context, path, replaces) {
30
+ raw(path, replaces) {
23
31
  const paths = path.split('.');
24
- let target = context;
32
+ let target = TRANSLATIONS[paths[0]];
33
+ paths.shift();
25
34
  for (const element of paths) {
26
35
  target = target[element];
27
36
  if (!target) {
@@ -33,16 +42,15 @@ class Locale {
33
42
  }
34
43
  return 'Translation not found';
35
44
  }
36
- }
37
- exports.Locale = Locale;
38
- const cache = {
39
- ptBR: null,
40
- es: null
41
- };
42
- function locale(language) {
43
- if (cache[language] === null) {
44
- cache[language] = new Locale(language);
45
+ joi(detail) {
46
+ const accessKey = TRANSLATIONS.joi[detail.type];
47
+ if (accessKey) {
48
+ if (detail.context) {
49
+ detail.context.label = this.raw(detail.context.label);
50
+ }
51
+ return this.reach(TRANSLATIONS.joi[detail.type], detail.context);
52
+ }
53
+ return `There is no translation for joi type '${detail.type}'`;
45
54
  }
46
- return cache[language];
47
55
  }
48
- exports.locale = locale;
56
+ exports.Locale = Locale;
@@ -1,6 +1,2 @@
1
- export declare const button: {
2
- maria: {
3
- ptBR: string;
4
- es: string;
5
- };
6
- };
1
+ import { ITranslation } from '../translation.interface';
2
+ export declare const button: ITranslation;
@@ -0,0 +1,2 @@
1
+ import { ITranslation } from '../translation.interface';
2
+ export declare const databaseErrors: ITranslation;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.databaseErrors = {
4
+ generic: {
5
+ ptBR: 'Ocorreu um erro ao processar a requisição.',
6
+ es: 'Se produjo un error al procesar la solicitud.'
7
+ },
8
+ unique_constraint: {
9
+ ptBR: 'Já existe um {{label}} com valor "{{value}}".',
10
+ es: 'Ya existe un {{label}} con valor "{{value}}".'
11
+ }
12
+ };
@@ -0,0 +1,2 @@
1
+ import { ITranslation } from '../translation.interface';
2
+ export declare const joi: ITranslation;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.joi = {
4
+ 'string.base': {
5
+ ptBR: 'Campo "{{label}}" deve ser um texto.',
6
+ es: 'Campo "{{label}}" debe se uno texto.'
7
+ },
8
+ 'number.base': {
9
+ ptBR: 'Campo "{{label}}" precisa se um numero.',
10
+ es: ''
11
+ },
12
+ 'number.min': {
13
+ ptBR: 'Campo "{{label}}" precisa ter no minimo {{limit}}',
14
+ es: ''
15
+ }
16
+ };
@@ -1,2 +1,4 @@
1
- export * from './modules/vehicle';
2
- export * from './common/buttons';
1
+ export { gate } from './modules/gate/gate';
2
+ export { button } from './common/buttons';
3
+ export { joi } from './common/joi';
4
+ export { databaseErrors } from './common/database-errors';
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./modules/vehicle"));
7
- __export(require("./common/buttons"));
3
+ var gate_1 = require("./modules/gate/gate");
4
+ exports.gate = gate_1.gate;
5
+ var buttons_1 = require("./common/buttons");
6
+ exports.button = buttons_1.button;
7
+ var joi_1 = require("./common/joi");
8
+ exports.joi = joi_1.joi;
9
+ var database_errors_1 = require("./common/database-errors");
10
+ exports.databaseErrors = database_errors_1.databaseErrors;
@@ -0,0 +1,2 @@
1
+ import { ITranslation } from '../../translation.interface';
2
+ export declare const gate: ITranslation;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gate = {
4
+ name: {
5
+ ptBR: 'Nome',
6
+ es: 'Nombre'
7
+ }
8
+ };
@@ -0,0 +1,4 @@
1
+ import { IAccessKey } from '../languages';
2
+ export interface ITranslation {
3
+ [key: string]: IAccessKey;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/jest.config.json CHANGED
@@ -5,6 +5,16 @@
5
5
  "transform": {
6
6
  "^.+\\.ts$": "ts-jest"
7
7
  },
8
+ "reporters": [
9
+ "default",
10
+ [
11
+ "jest-junit",
12
+ {
13
+ "suiteName": "Sofit view locale tests",
14
+ "output":"./coverage/jest/results.xml"
15
+ }
16
+ ]
17
+ ],
8
18
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
9
19
  "moduleFileExtensions": [
10
20
  "ts",
package/lib/index.ts CHANGED
@@ -1 +1,4 @@
1
- export { locale, Locale } from './runner';
1
+ import * as i18n from './translations';
2
+
3
+ export { Locale } from './runner';
4
+ export { i18n };
@@ -0,0 +1,13 @@
1
+ export interface IContext {
2
+ key?: string;
3
+ label?: string;
4
+ limit?: number;
5
+ value?: string;
6
+ }
7
+
8
+ export interface IValidationErrorItem {
9
+ message: string;
10
+ type: string;
11
+ path: string[];
12
+ context?: IContext;
13
+ }
package/lib/runner.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { IAccessKey, ICreator, LANGUAGES } from './languages';
1
+ import { IValidationErrorItem } from './joi/joi.interface';
2
+ import { IAccessKey, LANGUAGES } from './languages';
3
+ import * as TRANSLATIONS from './translations';
2
4
 
3
5
  export interface IReplaces {
4
6
  [key: string]: string;
@@ -30,21 +32,20 @@ export class Locale {
30
32
  }
31
33
 
32
34
  return text;
33
-
34
35
  }
35
36
 
36
37
  return 'Translation not found';
37
38
  }
38
39
 
39
- // tslint:disable-next-line:no-any
40
- public raw(context: any, path: string, replaces?: IReplaces): string {
40
+ public raw(path: string, replaces?: IReplaces): string {
41
41
 
42
42
  const paths = path.split('.');
43
- let target = context;
43
+ let target = TRANSLATIONS[paths[0]];
44
+
45
+ paths.shift();
44
46
 
45
47
  for (const element of paths) {
46
48
 
47
- // tslint:disable-next-line
48
49
  target = target[element];
49
50
 
50
51
  if (!target) {
@@ -55,25 +56,26 @@ export class Locale {
55
56
 
56
57
  if (target) {
57
58
 
58
- // tslint:disable-next-line
59
59
  return this.reach(target, replaces);
60
60
  }
61
61
 
62
62
  return 'Translation not found';
63
63
  }
64
- }
65
64
 
66
- const cache: ICreator = {
67
- ptBR: null,
68
- es: null
69
- };
65
+ public joi(detail: IValidationErrorItem) {
70
66
 
71
- export function locale(language: LANGUAGES): Locale {
67
+ const accessKey = TRANSLATIONS.joi[detail.type];
72
68
 
73
- if (cache[language] === null) {
69
+ if (accessKey) {
74
70
 
75
- cache[language] = new Locale(language);
76
- }
71
+ if (detail.context) {
77
72
 
78
- return cache[language] as Locale;
73
+ detail.context.label = this.raw(detail.context.label as string);
74
+ }
75
+
76
+ return this.reach(TRANSLATIONS.joi[detail.type], detail.context as IReplaces);
77
+ }
78
+
79
+ return `There is no translation for joi type '${detail.type}'`;
80
+ }
79
81
  }
@@ -0,0 +1,49 @@
1
+ import { i18n, Locale } from '../index';
2
+
3
+ describe('Vehicle locale test', () => {
4
+
5
+ const localeptBR = new Locale('ptBR');
6
+ const localeptES = new Locale('es');
7
+
8
+ it('Should get translations with reach', () => {
9
+
10
+ expect(localeptBR.reach(i18n.gate.name))
11
+ .toEqual(i18n.gate.name.ptBR);
12
+ expect(localeptES.reach(i18n.gate.name))
13
+ .toEqual(i18n.gate.name.es);
14
+ });
15
+
16
+ it('Should replace correctly with reach', () => {
17
+
18
+ expect(localeptBR.reach(i18n.databaseErrors.unique_constraint, {
19
+ label: 'label',
20
+ value: 'value'
21
+ })).toEqual('Já existe um label com valor "value".');
22
+
23
+ expect(localeptES.reach(i18n.databaseErrors.unique_constraint, {
24
+ label: 'label2',
25
+ value: 'value2'
26
+ })).toEqual('Ya existe un label2 con valor "value2".');
27
+ });
28
+
29
+ it('Should get translations with raw', () => {
30
+
31
+ expect(localeptBR.raw('databaseErrors.generic'))
32
+ .toEqual(i18n.databaseErrors.generic.ptBR);
33
+ expect(localeptES.raw('databaseErrors.generic'))
34
+ .toEqual(i18n.databaseErrors.generic.es);
35
+ });
36
+
37
+ it('Should replace correctly with raw', () => {
38
+
39
+ expect(localeptBR.raw('databaseErrors.unique_constraint', {
40
+ label: 'label',
41
+ value: 'value'
42
+ })).toEqual('Já existe um label com valor "value".');
43
+
44
+ expect(localeptES.raw('databaseErrors.unique_constraint', {
45
+ label: 'label2',
46
+ value: 'value2'
47
+ })).toEqual('Ya existe un label2 con valor "value2".');
48
+ });
49
+ });
@@ -1,4 +1,6 @@
1
- export const button = {
1
+ import { ITranslation } from '../translation.interface';
2
+
3
+ export const button: ITranslation = {
2
4
  maria: {
3
5
  ptBR: 'maira',
4
6
  es: 'pedro'
@@ -0,0 +1,12 @@
1
+ import { ITranslation } from '../translation.interface';
2
+
3
+ export const databaseErrors: ITranslation = {
4
+ generic: {
5
+ ptBR: 'Ocorreu um erro ao processar a requisição.',
6
+ es: 'Se produjo un error al procesar la solicitud.'
7
+ },
8
+ unique_constraint: {
9
+ ptBR: 'Já existe um {{label}} com valor "{{value}}".',
10
+ es: 'Ya existe un {{label}} con valor "{{value}}".'
11
+ }
12
+ };
@@ -0,0 +1,16 @@
1
+ import { ITranslation } from '../translation.interface';
2
+
3
+ export const joi: ITranslation = {
4
+ 'string.base': {
5
+ ptBR: 'Campo "{{label}}" deve ser um texto.',
6
+ es: 'Campo "{{label}}" debe se uno texto.'
7
+ },
8
+ 'number.base': {
9
+ ptBR: 'Campo "{{label}}" precisa se um numero.',
10
+ es: ''
11
+ },
12
+ 'number.min': {
13
+ ptBR: 'Campo "{{label}}" precisa ter no minimo {{limit}}',
14
+ es: ''
15
+ }
16
+ };
@@ -1,2 +1,4 @@
1
- export * from './modules/vehicle';
2
- export * from './common/buttons';
1
+ export { gate } from './modules/gate/gate';
2
+ export { button } from './common/buttons';
3
+ export { joi } from './common/joi';
4
+ export { databaseErrors } from './common/database-errors';
@@ -0,0 +1,8 @@
1
+ import { ITranslation } from '../../translation.interface';
2
+
3
+ export const gate: ITranslation = {
4
+ name: {
5
+ ptBR: 'Nome',
6
+ es: 'Nombre'
7
+ }
8
+ };
@@ -0,0 +1,5 @@
1
+ import { IAccessKey } from '../languages';
2
+
3
+ export interface ITranslation {
4
+ [key: string]: IAccessKey;
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sofit/view-locale",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "description": "Traduções do Sofit View",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,8 @@
8
8
  "test": "jest --config ./jest.config.json",
9
9
  "dev": "nodemon",
10
10
  "lint": "tslint --project ./",
11
- "build": "tsc --p ./"
11
+ "build": "tsc --p ./",
12
+ "postinstall": "node .vscode/commit-msg.js"
12
13
  },
13
14
  "author": "desenvolvimento@sofit4.com.br",
14
15
  "contributors": [
@@ -21,14 +22,16 @@
21
22
  },
22
23
  "dependencies": {
23
24
  "typescript": "^2.8.3",
24
- "@types/node": "^9.6.6",
25
+ "@types/node": "^9.6.6"
26
+ },
27
+ "devDependencies": {
28
+ "@sofit/tslint-rules": "0.1.11",
25
29
  "@types/jest": "^22.2.3",
30
+ "jest-junit": "^5.1.0",
26
31
  "jest": "^22.4.3",
27
32
  "ts-jest": "^22.4.4",
28
33
  "tslint": "^5.9.1",
29
- "tslint-eslint-rules": "^5.1.0"
30
- },
31
- "devDependencies": {
34
+ "tslint-eslint-rules": "^5.1.0",
32
35
  "nodemon": "^1.17.3",
33
36
  "ts-node": "^6.0.0"
34
37
  }
@@ -1,18 +0,0 @@
1
- export declare const vehicle: {
2
- label: {
3
- name: {
4
- ptBR: string;
5
- es: string;
6
- };
7
- };
8
- messages: {
9
- not_found: {
10
- ptBR: string;
11
- es: string;
12
- };
13
- test: {
14
- ptBR: string;
15
- es: string;
16
- };
17
- };
18
- };
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.vehicle = {
4
- label: {
5
- name: {
6
- ptBR: 'Nome',
7
- es: 'Nuombre'
8
- }
9
- },
10
- messages: {
11
- not_found: {
12
- ptBR: 'Não encontrado',
13
- es: 'nao coisado'
14
- },
15
- test: {
16
- ptBR: 'Teste de coisa pra {{variable}}',
17
- es: 'Carai {{variable}}'
18
- }
19
- }
20
- };
@@ -1,43 +0,0 @@
1
- import { locale } from '../index';
2
- import * as translations from '../translations';
3
-
4
- describe('Vehicle locale test', () => {
5
-
6
- it('Should get translations with reach', () => {
7
-
8
- expect(locale('ptBR').reach(translations.vehicle.messages.not_found))
9
- .toEqual(translations.vehicle.messages.not_found.ptBR);
10
- expect(locale('es').reach(translations.vehicle.messages.not_found))
11
- .toEqual(translations.vehicle.messages.not_found.es);
12
- });
13
-
14
- it('Should replace correctly with reach', () => {
15
-
16
- expect(locale('ptBR').reach(translations.vehicle.messages.test, {
17
- variable: 'varai'
18
- })).toEqual('Teste de coisa pra varai');
19
-
20
- expect(locale('es').reach(translations.vehicle.messages.test, {
21
- variable: 'varai'
22
- })).toEqual('Carai varai');
23
- });
24
-
25
- it('Should get translations with raw', () => {
26
-
27
- expect(locale('ptBR').raw(translations.vehicle, 'messages.not_found'))
28
- .toEqual(translations.vehicle.messages.not_found.ptBR);
29
- expect(locale('es').raw(translations.vehicle, 'messages.not_found'))
30
- .toEqual(translations.vehicle.messages.not_found.es);
31
- });
32
-
33
- it('Should replace correctly with raw', () => {
34
-
35
- expect(locale('ptBR').raw(translations.vehicle, 'messages.test', {
36
- variable: 'varai'
37
- })).toEqual('Teste de coisa pra varai');
38
-
39
- expect(locale('es').raw(translations.vehicle, 'messages.test', {
40
- variable: 'varai'
41
- })).toEqual('Carai varai');
42
- });
43
- });
@@ -1,20 +0,0 @@
1
-
2
- export const vehicle = {
3
- label: {
4
- name: {
5
- ptBR: 'Nome',
6
- es: 'Nuombre'
7
- }
8
- },
9
- messages: {
10
- /** Não encontrado */
11
- not_found: {
12
- ptBR: 'Não encontrado',
13
- es: 'nao coisado'
14
- },
15
- test: {
16
- ptBR: 'Teste de coisa pra {{variable}}',
17
- es: 'Carai {{variable}}'
18
- }
19
- }
20
- };