├── src ├── declarations.d.ts └── index.ts ├── .gitignore ├── tsconfig.json ├── README.md ├── tslint.json └── package.json /src/declarations.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'gettext-parser'; 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE 2 | .vscode 3 | .idea 4 | 5 | # Logs and other files 6 | npm-debug.log* 7 | .DS_Store 8 | 9 | # Compiled files 10 | dist 11 | 12 | # Dependency directory 13 | node_modules 14 | 15 | # Source maps for JS builds 16 | *.js.map 17 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "noUnusedLocals": true, 5 | "noImplicitAny": true, 6 | "removeComments": true, 7 | "module": "commonjs", 8 | "moduleResolution": "node", 9 | "sourceMap": true, 10 | "outDir": "dist/", 11 | "declaration": true, 12 | "target": "es5", 13 | "lib": [ 14 | "dom", 15 | "es2015" 16 | ] 17 | }, 18 | "include": [ 19 | "src/**/*.ts" 20 | ], 21 | "exclude": [ 22 | "node_modules" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | Load po files for use with `ngx-translate` 3 | 4 | ## Installation: 5 | 6 | ``` 7 | npm i @ngx-translate/core --save 8 | npm i @biesbjerg/ngx-translate-po-http-loader --save 9 | ``` 10 | 11 | ## Usage: 12 | ```ts 13 | import { HttpClient, HttpClientModule } from '@angular/common/http'; 14 | 15 | import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; 16 | import { TranslatePoHttpLoader } from '@biesbjerg/ngx-translate-po-http-loader'; 17 | 18 | export function createTranslateLoader(http: HttpClient) { 19 | return new TranslatePoHttpLoader(http, 'assets/i18n', '.po'); 20 | } 21 | 22 | @NgModule({ 23 | imports: [ 24 | BrowserModule, 25 | HttpClientModule, 26 | TranslateModule.forRoot({ 27 | loader: { 28 | provide: TranslateLoader, 29 | useFactory: createTranslateLoader, 30 | deps: [HttpClient] 31 | } 32 | }) 33 | ], 34 | bootstrap: [AppComponent] 35 | }) 36 | export class AppModule { } 37 | ``` 38 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 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 | } 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@biesbjerg/ngx-translate-po-http-loader", 3 | "version": "3.1.0", 4 | "description": "Load po files for use with ngx-translate", 5 | "main": "dist/index.js", 6 | "typings": "dist/index.d.ts", 7 | "scripts": { 8 | "build": "npm run clean && tsc", 9 | "watch": "npm run clean && tsc --watch", 10 | "clean": "rm -rf ./dist", 11 | "lint": "tslint --force '*.ts'" 12 | }, 13 | "files": [ 14 | "dist/" 15 | ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/biesbjerg/ngx-translate-po-http-loader.git" 19 | }, 20 | "keywords": [], 21 | "author": "Kim Biesbjerg ", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/biesbjerg/ngx-translate-po-http-loader/issues" 25 | }, 26 | "homepage": "https://github.com/biesbjerg/ngx-translate-po-http-loader", 27 | "engines": { 28 | "node": ">=4.1.1" 29 | }, 30 | "config": {}, 31 | "peerDependencies": { 32 | "@angular/core": "^6.0.0", 33 | "@angular/common": "^6.0.0", 34 | "@ngx-translate/core": "^10.0.0" 35 | }, 36 | "dependencies": { 37 | "gettext-parser": "1.3.1" 38 | }, 39 | "devDependencies": { 40 | "@angular/core": "^6.0.0", 41 | "@angular/common": "^6.0.0", 42 | "@angular/platform-browser": "^6.0.0", 43 | "rxjs": "^6.0.0", 44 | "zone.js": "0.8.26", 45 | "typescript": "2.7.2", 46 | "@ngx-translate/core": "10.0.0", 47 | "tslint": "5.8.0", 48 | "tslint-eslint-rules": "4.1.1" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { TranslateLoader } from '@ngx-translate/core'; 3 | import * as gettext from 'gettext-parser'; 4 | import { Observable } from 'rxjs'; 5 | import { map } from 'rxjs/operators'; 6 | 7 | export class TranslatePoHttpLoader implements TranslateLoader { 8 | 9 | /** 10 | * Translation domain 11 | */ 12 | public domain = ''; 13 | 14 | constructor( 15 | protected _http: HttpClient, 16 | protected _prefix: string = 'i18n', 17 | protected _suffix: string = '.po' 18 | ) { 19 | } 20 | 21 | /** 22 | * Gets the translations from file 23 | * @param lang 24 | * @returns {any} 25 | */ 26 | public getTranslation(lang: string): Observable { 27 | return this._http 28 | .get(`${this._prefix}/${lang}${this._suffix}`, { responseType: 'text' }) 29 | .pipe(map((contents: string) => this.parse(contents))); 30 | } 31 | 32 | /** 33 | * Parse po file 34 | * @param contents 35 | * @returns {any} 36 | */ 37 | public parse(contents: string): any { 38 | let translations: { [key: string]: string } = {}; 39 | 40 | const po = gettext.po.parse(contents, 'utf-8'); 41 | if (!po.translations.hasOwnProperty(this.domain)) { 42 | return translations; 43 | } 44 | 45 | Object.keys(po.translations[this.domain]) 46 | .forEach(key => { 47 | const translation: string = po.translations[this.domain][key].msgstr.pop(); 48 | if (key.length > 0 && translation.length > 0) { 49 | translations[key] = translation; 50 | } 51 | }); 52 | 53 | return translations; 54 | } 55 | 56 | } 57 | --------------------------------------------------------------------------------