@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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/languages.d.ts +10 -0
- package/dist/languages.js +2 -0
- package/dist/runner.d.ts +11 -0
- package/dist/runner.js +48 -0
- package/dist/translations/common/buttons.d.ts +6 -0
- package/dist/translations/common/buttons.js +8 -0
- package/dist/translations/index.d.ts +2 -0
- package/dist/translations/index.js +7 -0
- package/dist/translations/modules/vehicle.d.ts +18 -0
- package/dist/translations/modules/vehicle.js +20 -0
- package/jest.config.json +17 -0
- package/lib/index.ts +1 -0
- package/lib/languages.ts +13 -0
- package/lib/runner.ts +79 -0
- package/lib/tests/vehicle.spec.ts +43 -0
- package/lib/translations/common/buttons.ts +6 -0
- package/lib/translations/index.ts +2 -0
- package/lib/translations/modules/vehicle.ts +20 -0
- package/package.json +36 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { locale, Locale } from './runner';
|
package/dist/index.js
ADDED
package/dist/runner.d.ts
ADDED
|
@@ -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,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
|
+
};
|
package/jest.config.json
ADDED
package/lib/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { locale, Locale } from './runner';
|
package/lib/languages.ts
ADDED
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,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
|
+
}
|