@sofit/view-locale 0.1.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.
@@ -0,0 +1 @@
1
+ export { locale, Locale } from './runner';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var runner_1 = require("./runner");
4
+ exports.locale = runner_1.locale;
5
+ exports.Locale = runner_1.Locale;
@@ -0,0 +1,10 @@
1
+ import { Locale } from './runner';
2
+ export declare type LANGUAGES = 'ptBR' | 'es';
3
+ export interface IAccessKey {
4
+ ptBR: string;
5
+ es: string;
6
+ }
7
+ export interface ICreator {
8
+ ptBR: Locale | null;
9
+ es: Locale | null;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ import { IAccessKey, LANGUAGES } from './languages';
2
+ export interface IReplaces {
3
+ [key: string]: string;
4
+ }
5
+ export declare class Locale {
6
+ private readonly language;
7
+ constructor(language: LANGUAGES);
8
+ reach(accessKey: IAccessKey, replaces?: IReplaces): string;
9
+ raw(context: any, path: string, replaces?: IReplaces): string;
10
+ }
11
+ export declare function locale(language: LANGUAGES): Locale;
package/dist/runner.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Locale {
4
+ constructor(language) {
5
+ this.language = language;
6
+ }
7
+ reach(accessKey, replaces) {
8
+ let text = accessKey[this.language];
9
+ if (Boolean(text)) {
10
+ if (replaces !== undefined) {
11
+ for (const key in replaces) {
12
+ if (replaces.hasOwnProperty(key)) {
13
+ const value = replaces[key];
14
+ text = text.replace(`{{${key}}}`, value);
15
+ }
16
+ }
17
+ }
18
+ return text;
19
+ }
20
+ return 'Translation not found';
21
+ }
22
+ raw(context, path, replaces) {
23
+ const paths = path.split('.');
24
+ let target = context;
25
+ for (const element of paths) {
26
+ target = target[element];
27
+ if (!target) {
28
+ break;
29
+ }
30
+ }
31
+ if (target) {
32
+ return this.reach(target, replaces);
33
+ }
34
+ return 'Translation not found';
35
+ }
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
+ }
46
+ return cache[language];
47
+ }
48
+ exports.locale = locale;
@@ -0,0 +1,6 @@
1
+ export declare const button: {
2
+ maria: {
3
+ ptBR: string;
4
+ es: string;
5
+ };
6
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.button = {
4
+ maria: {
5
+ ptBR: 'maira',
6
+ es: 'pedro'
7
+ }
8
+ };
@@ -0,0 +1,2 @@
1
+ export * from './modules/vehicle';
2
+ export * from './common/buttons';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ function __export(m) {
3
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
+ }
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ __export(require("./modules/vehicle"));
7
+ __export(require("./common/buttons"));
@@ -0,0 +1,18 @@
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
+ };
@@ -0,0 +1,20 @@
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
+ };
@@ -0,0 +1,17 @@
1
+ {
2
+ "roots": [
3
+ "<rootDir>/lib"
4
+ ],
5
+ "transform": {
6
+ "^.+\\.ts$": "ts-jest"
7
+ },
8
+ "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
9
+ "moduleFileExtensions": [
10
+ "ts",
11
+ "tsx",
12
+ "js",
13
+ "jsx",
14
+ "json",
15
+ "node"
16
+ ]
17
+ }
package/lib/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { locale, Locale } from './runner';
@@ -0,0 +1,13 @@
1
+ import { Locale } from './runner';
2
+
3
+ export type LANGUAGES = 'ptBR' | 'es';
4
+
5
+ export interface IAccessKey {
6
+ ptBR: string;
7
+ es: string;
8
+ }
9
+
10
+ export interface ICreator {
11
+ ptBR: Locale | null;
12
+ es: Locale | null;
13
+ }
package/lib/runner.ts ADDED
@@ -0,0 +1,79 @@
1
+ import { IAccessKey, ICreator, LANGUAGES } from './languages';
2
+
3
+ export interface IReplaces {
4
+ [key: string]: string;
5
+ }
6
+
7
+ export class Locale {
8
+
9
+ constructor(
10
+ private readonly language: LANGUAGES
11
+ ) { }
12
+
13
+ public reach(accessKey: IAccessKey, replaces?: IReplaces): string {
14
+
15
+ let text = accessKey[this.language];
16
+
17
+ if (Boolean(text)) {
18
+
19
+ if (replaces !== undefined) {
20
+
21
+ for (const key in replaces) {
22
+
23
+ if (replaces.hasOwnProperty(key)) {
24
+
25
+ const value = replaces[key];
26
+
27
+ text = text.replace(`{{${key}}}`, value);
28
+ }
29
+ }
30
+ }
31
+
32
+ return text;
33
+
34
+ }
35
+
36
+ return 'Translation not found';
37
+ }
38
+
39
+ // tslint:disable-next-line:no-any
40
+ public raw(context: any, path: string, replaces?: IReplaces): string {
41
+
42
+ const paths = path.split('.');
43
+ let target = context;
44
+
45
+ for (const element of paths) {
46
+
47
+ // tslint:disable-next-line
48
+ target = target[element];
49
+
50
+ if (!target) {
51
+
52
+ break;
53
+ }
54
+ }
55
+
56
+ if (target) {
57
+
58
+ // tslint:disable-next-line
59
+ return this.reach(target, replaces);
60
+ }
61
+
62
+ return 'Translation not found';
63
+ }
64
+ }
65
+
66
+ const cache: ICreator = {
67
+ ptBR: null,
68
+ es: null
69
+ };
70
+
71
+ export function locale(language: LANGUAGES): Locale {
72
+
73
+ if (cache[language] === null) {
74
+
75
+ cache[language] = new Locale(language);
76
+ }
77
+
78
+ return cache[language] as Locale;
79
+ }
@@ -0,0 +1,43 @@
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
+ });
@@ -0,0 +1,6 @@
1
+ export const button = {
2
+ maria: {
3
+ ptBR: 'maira',
4
+ es: 'pedro'
5
+ }
6
+ };
@@ -0,0 +1,2 @@
1
+ export * from './modules/vehicle';
2
+ export * from './common/buttons';
@@ -0,0 +1,20 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@sofit/view-locale",
3
+ "version": "0.1.0",
4
+ "description": "Traduções do Sofit View",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest --config ./jest.config.json",
9
+ "dev": "nodemon",
10
+ "build": "tslint --project ./ && tsc --p ./",
11
+ "preversion": "npm t && tslint --project",
12
+ "prepublish": "npm t && npm run build"
13
+ },
14
+ "author": "desenvolvimento@sofit4.com.br",
15
+ "contributors": [
16
+ "lucassedrez@hotmail.com",
17
+ "rhogeranacleto@gmail.com"
18
+ ],
19
+ "license": "ISC",
20
+ "engines": {
21
+ "node": "8.11.1"
22
+ },
23
+ "dependencies": {
24
+ "typescript": "^2.8.3",
25
+ "@types/node": "^9.6.6"
26
+ },
27
+ "devDependencies": {
28
+ "@types/jest": "^22.2.3",
29
+ "jest": "^22.4.3",
30
+ "nodemon": "^1.17.3",
31
+ "ts-jest": "^22.4.4",
32
+ "ts-node": "^6.0.0",
33
+ "tslint": "^5.9.1",
34
+ "tslint-eslint-rules": "^5.1.0"
35
+ }
36
+ }