Skip to content

Commit b2df710

Browse files
author
Kim Biesbjerg
committed
Move to gettext-parser package
1 parent 8c947a0 commit b2df710

File tree

5 files changed

+92
-26
lines changed

5 files changed

+92
-26
lines changed

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"scripts": {
88
"build": "npm run clean && tsc",
99
"watch": "npm run clean && tsc --watch",
10-
"clean": "rm -rf ./dist"
10+
"clean": "rm -rf ./dist",
11+
"lint": "tslint --force '*.ts'"
1112
},
1213
"files": [
1314
"dist/"
@@ -33,7 +34,7 @@
3334
"ng2-translate": "^4.0.0"
3435
},
3536
"dependencies": {
36-
"pofile": "^1.0.2"
37+
"gettext-parser": "^1.2.1"
3738
},
3839
"devDependencies": {
3940
"@angular/core": "2.2.1",
@@ -43,6 +44,8 @@
4344
"rxjs": "5.0.0-beta.12",
4445
"zone.js": "0.6.21",
4546
"typescript": "2.0.10",
46-
"ng2-translate": "4.0.1"
47+
"ng2-translate": "4.0.1",
48+
"tslint": "^4.0.2",
49+
"tslint-eslint-rules": "^3.1.0"
4750
}
4851
}

src/declarations.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
declare module 'pofile';
1+
declare module 'gettext-parser';

src/index.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { Http, Response } from '@angular/http';
22

33
import { Observable } from 'rxjs/Observable';
44
import { TranslateLoader } from 'ng2-translate';
5-
import * as pofile from 'pofile';
5+
import * as gettext from 'gettext-parser';
66

77
export class TranslatePoLoader implements TranslateLoader {
88

9+
/**
10+
* Translation domain
11+
*/
12+
public domain = '';
13+
914
constructor(
1015
protected _http: Http,
1116
protected _prefix: string = 'i18n',
@@ -22,25 +27,29 @@ export class TranslatePoLoader implements TranslateLoader {
2227
return this._http
2328
.get(`${this._prefix}/${lang}${this._suffix}`)
2429
.map((response: Response) => response.text())
25-
.map((contents: string) => this._parse(contents));
30+
.map((contents: string) => this.parse(contents));
2631
}
2732

2833
/**
2934
* Parse po file
3035
* @param contents
3136
* @returns {any}
3237
*/
33-
protected _parse(contents: string): any {
34-
let translations = {};
35-
36-
const data = pofile.parse(contents);
37-
data.items.forEach(item => {
38-
const id: string = item.msgid;
39-
const translation: string = item.msgstr.pop();
40-
if (id && translation) {
41-
translations[id] = translation;
42-
}
43-
});
38+
public parse(contents: string): any {
39+
let translations: { [key: string]: string } = {};
40+
41+
const po = gettext.po.parse(contents, 'utf-8');
42+
if (!po.translations.hasOwnProperty(this.domain)) {
43+
return translations;
44+
}
45+
46+
Object.keys(po.translations[this.domain])
47+
.forEach(key => {
48+
const translation: string = po.translations[this.domain][key].msgstr.pop();
49+
if (key.length > 0 && translation.length > 0) {
50+
translations[key] = translation;
51+
}
52+
});
4453

4554
return translations;
4655
}

tsconfig.json

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
22
"compilerOptions": {
3-
"emitDecoratorMetadata": true,
4-
"experimentalDecorators": true,
5-
"declaration": true,
6-
"lib": [
7-
"dom",
8-
"es2015"
9-
],
3+
"allowSyntheticDefaultImports": true,
4+
"noUnusedLocals": true,
5+
"noImplicitAny": true,
6+
"removeComments": true,
107
"module": "commonjs",
118
"moduleResolution": "node",
12-
"removeComments": true,
139
"sourceMap": true,
10+
"outDir": "dist/",
11+
"declaration": true,
1412
"target": "es5",
15-
"outDir": "dist/"
13+
"lib": [
14+
"dom",
15+
"es2015"
16+
]
1617
},
1718
"include": [
1819
"src/**/*.ts"

tslint.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"rulesDirectory": [
3+
"node_modules/tslint-eslint-rules/dist/rules"
4+
],
5+
"rules": {
6+
"indent": [true, "tabs"],
7+
"semicolon": [true, "always", "ignore-interfaces"],
8+
"quotemark": [true, "single", "avoid-escape"],
9+
"only-arrow-functions": true,
10+
"no-duplicate-variable": true,
11+
"member-access": true,
12+
"member-ordering": [
13+
true,
14+
{
15+
"order": [
16+
"public-static-field",
17+
"public-static-method",
18+
"protected-static-field",
19+
"protected-static-method",
20+
"private-static-field",
21+
"private-static-method",
22+
"public-instance-field",
23+
"protected-instance-field",
24+
"private-instance-field",
25+
"constructor",
26+
"public-instance-method",
27+
"protected-instance-method",
28+
"private-instance-method"
29+
]
30+
}
31+
],
32+
"curly": true,
33+
"eofline": true,
34+
"no-trailing-whitespace": true,
35+
"trailing-comma": [
36+
true,
37+
{
38+
"multiline": "never",
39+
"singleline": "never"
40+
}
41+
],
42+
"whitespace": [
43+
true,
44+
"check-branch",
45+
"check-decl",
46+
"check-operator",
47+
"check-module",
48+
"check-separator",
49+
"check-type",
50+
"check-typecast"
51+
]
52+
}
53+
}

0 commit comments

Comments
 (0)