dfx-translate
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -30,5 +30,7 @@ (function (global, factory) { | ||
var TranslatorService = /** @class */ (function () { | ||
function TranslatorService(config, http) { | ||
var _a, _b; | ||
var TRANSLATE_CONFIG = new i0.InjectionToken('TRANSLATE_CONFIG'); | ||
// noinspection JSUnusedLocalSymbols | ||
var TranslateService = /** @class */ (function () { | ||
function TranslateService(config, http) { | ||
this.config = config; | ||
@@ -39,39 +41,41 @@ this.http = http; | ||
this.selectedTranslation = ''; | ||
this.defaultLanguage = 'de'; | ||
this.autoGeneratedDefaultLanguage = 'en'; | ||
this.defaultLanguage = ((_a = config === null || config === void 0 ? void 0 : config.defaultLanguage) === null || _a === void 0 ? void 0 : _a.length) > 0 ? config.defaultLanguage : this.defaultLanguage; | ||
this.autoGeneratedDefaultLanguage = ((_b = config === null || config === void 0 ? void 0 : config.autoGeneratedDefaultLanguage) === null || _b === void 0 ? void 0 : _b.length) > 0 ? config.autoGeneratedDefaultLanguage : this.autoGeneratedDefaultLanguage; | ||
this.defaultLanguage = 'en'; | ||
this.useLocalStorage = true; | ||
this.defaultLanguage = (config === null || config === void 0 ? void 0 : config.defaultLanguage) != null ? config.defaultLanguage : this.defaultLanguage; | ||
this.useLocalStorage = (config === null || config === void 0 ? void 0 : config.useLocalStorage) != null ? config.useLocalStorage : this.useLocalStorage; | ||
} | ||
TranslatorService.prototype.translate = function (key) { | ||
if (this.translations[key]) { | ||
return this.translations[key]; | ||
TranslateService.prototype.translate = function (key) { | ||
var translation = this.translations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else if (this.autoGeneratedTranslations[key]) { | ||
return this.autoGeneratedTranslations[key]; | ||
translation = this.autoGeneratedTranslations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else { | ||
return key; | ||
} | ||
return key; | ||
}; | ||
TranslatorService.prototype.use = function (lang) { | ||
TranslateService.prototype.use = function (lang) { | ||
var _this = this; | ||
if (lang === 'DEFAULT') { | ||
var language = localStorage.getItem('language'); | ||
if (lang === void 0) { lang = null; } | ||
if (lang === null) { | ||
var language = this.useLocalStorage ? localStorage.getItem('language') : null; | ||
if (language != null) { | ||
lang = language; | ||
console.log('Language cookie found! Using ' + lang); | ||
console.log('Language cookie found! Using "' + lang + '"'); | ||
} | ||
else { | ||
lang = this.defaultLanguage; | ||
console.log('No language cookie found! Using de as default'); | ||
console.log('No language cookie found! Using "' + this.defaultLanguage + '" as default'); | ||
} | ||
} | ||
else { | ||
console.log('Language changed to ' + lang); | ||
console.log('Language changed to "' + lang + "'"); | ||
} | ||
this.selectedTranslation = lang; | ||
localStorage.setItem('language', lang); | ||
if (this.useLocalStorage) { | ||
localStorage.setItem('language', lang); | ||
} | ||
return new Promise(function (resolve) { | ||
var langPath = "assets/i18n/" + (lang || _this.defaultLanguage) + ".json"; | ||
// noinspection JSUnusedLocalSymbols | ||
_this.http.get(langPath).subscribe(function (translation) { | ||
@@ -84,18 +88,20 @@ _this.translations = Object.assign({}, translation || {}); | ||
}); | ||
var autoLangPath = "assets/i18n/" + (lang || _this.autoGeneratedDefaultLanguage) + "_auto.json"; | ||
// noinspection JSUnusedLocalSymbols | ||
_this.http.get(autoLangPath).subscribe(function (translation) { | ||
_this.autoGeneratedTranslations = Object.assign({}, translation || {}); | ||
resolve(_this.autoGeneratedTranslations); | ||
}, function (error) { | ||
_this.autoGeneratedTranslations = {}; | ||
resolve(_this.autoGeneratedTranslations); | ||
}); | ||
// Only fetch auto generated translation if it is not the primary language (which contains all translations anyway) | ||
if (_this.defaultLanguage !== lang) { | ||
var autoLangPath = "assets/i18n/" + lang + "_auto.json"; | ||
_this.http.get(autoLangPath).subscribe(function (translation) { | ||
_this.autoGeneratedTranslations = Object.assign({}, translation || {}); | ||
resolve(_this.autoGeneratedTranslations); | ||
}, function (error) { | ||
_this.autoGeneratedTranslations = {}; | ||
resolve(_this.autoGeneratedTranslations); | ||
}); | ||
} | ||
}); | ||
}; | ||
return TranslatorService; | ||
return TranslateService; | ||
}()); | ||
TranslatorService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatorService, deps: [{ token: 'config' }, { token: i1__namespace.HttpClient }], target: i0__namespace.ɵɵFactoryTarget.Injectable }); | ||
TranslatorService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatorService, providedIn: 'root' }); | ||
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatorService, decorators: [{ | ||
TranslateService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslateService, deps: [{ token: TRANSLATE_CONFIG }, { token: i1__namespace.HttpClient }], target: i0__namespace.ɵɵFactoryTarget.Injectable }); | ||
TranslateService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslateService, providedIn: 'root' }); | ||
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslateService, decorators: [{ | ||
type: i0.Injectable, | ||
@@ -108,3 +114,3 @@ args: [{ | ||
type: i0.Inject, | ||
args: ['config'] | ||
args: [TRANSLATE_CONFIG] | ||
}] }, { type: i1__namespace.HttpClient }]; | ||
@@ -118,15 +124,15 @@ } }); | ||
TranslatePipe.prototype.transform = function (key) { | ||
if (this.translator.translations[key]) { | ||
return this.translator.translations[key]; | ||
var translation = this.translator.translations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else if (this.translator.autoGeneratedTranslations[key]) { | ||
return this.translator.autoGeneratedTranslations[key]; | ||
translation = this.translator.autoGeneratedTranslations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else { | ||
return key; | ||
} | ||
return key; | ||
}; | ||
return TranslatePipe; | ||
}()); | ||
TranslatePipe.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatePipe, deps: [{ token: TranslatorService }], target: i0__namespace.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatePipe, deps: [{ token: TranslateService }], target: i0__namespace.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵpipe = i0__namespace.ɵɵngDeclarePipe({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatePipe, name: "tr", pure: false }); | ||
@@ -139,3 +145,3 @@ i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0__namespace, type: TranslatePipe, decorators: [{ | ||
}] | ||
}], ctorParameters: function () { return [{ type: TranslatorService }]; } }); | ||
}], ctorParameters: function () { return [{ type: TranslateService }]; } }); | ||
@@ -149,3 +155,3 @@ var DfxTranslateModule = /** @class */ (function () { | ||
ngModule: DfxTranslateModule, | ||
providers: [{ provide: 'config', useValue: configuration }] | ||
providers: [{ provide: TRANSLATE_CONFIG, useValue: configuration }] | ||
}; | ||
@@ -161,3 +167,3 @@ }; | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -175,3 +181,3 @@ }, | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -183,3 +189,3 @@ }, | ||
function setupTranslateFactory(service) { | ||
return function () { return service.use('DEFAULT'); }; | ||
return function () { return service.use(); }; | ||
} | ||
@@ -196,4 +202,5 @@ | ||
exports.DfxTranslateModule = DfxTranslateModule; | ||
exports.TRANSLATE_CONFIG = TRANSLATE_CONFIG; | ||
exports.TranslatePipe = TranslatePipe; | ||
exports.TranslatorService = TranslatorService; | ||
exports.TranslateService = TranslateService; | ||
exports.setupTranslateFactory = setupTranslateFactory; | ||
@@ -200,0 +207,0 @@ |
import { APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { TranslatePipe } from './translate.pipe'; | ||
import { TranslatorService } from "./translator.service"; | ||
import { TranslateService } from "./translate.service"; | ||
import { TRANSLATE_CONFIG } from "./translate.config"; | ||
import * as i0 from "@angular/core"; | ||
@@ -10,3 +11,3 @@ export class DfxTranslateModule { | ||
ngModule: DfxTranslateModule, | ||
providers: [{ provide: 'config', useValue: configuration }] | ||
providers: [{ provide: TRANSLATE_CONFIG, useValue: configuration }] | ||
}; | ||
@@ -21,3 +22,3 @@ } | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -35,3 +36,3 @@ }, | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -43,4 +44,4 @@ }, | ||
export function setupTranslateFactory(service) { | ||
return () => service.use('DEFAULT'); | ||
return () => service.use(); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGZ4LXRyYW5zbGF0ZS5tb2R1bGUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9wcm9qZWN0cy9kZngtdHJhbnNsYXRlL3NyYy9saWIvZGZ4LXRyYW5zbGF0ZS5tb2R1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFDLGVBQWUsRUFBdUIsUUFBUSxFQUFDLE1BQU0sZUFBZSxDQUFDO0FBRTdFLE9BQU8sRUFBQyxhQUFhLEVBQUMsTUFBTSxrQkFBa0IsQ0FBQztBQUMvQyxPQUFPLEVBQUMsaUJBQWlCLEVBQUMsTUFBTSxzQkFBc0IsQ0FBQzs7QUFjdkQsTUFBTSxPQUFPLGtCQUFrQjtJQUM3QixNQUFNLENBQUMsTUFBTSxD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VHJhbnNsYXRlU2VydmljZV0sXG4gICAgICBtdWx0aTogdHJ1ZVxuICAgIH0sXG4gIF1cbn0pXG5leHBvcnQgY2xhc3MgRGZ4VHJhbnNsYXRlTW9kdWxlIHtcbiAgc3RhdGljIGNvbmZpZyhjb25maWd1cmF0aW9uOiBUcmFuc2xhdGVDb25maWcpOiBNb2R1bGVXaXRoUHJvdmlkZXJzPERmeFRyYW5zbGF0ZU1vZHVsZT4ge1xuICAgIGNvbnNvbGUubG9nKGNvbmZpZ3VyYXRpb24pO1xuICAgIHJldHVybiB7XG4gICAgICBuZ01vZHVsZTogRGZ4VHJhbnNsYXRlTW9kdWxlLFxuICAgICAgcHJvdmlkZXJzOiBbe3Byb3ZpZGU6IFRSQU5TTEFURV9DT05GSUcsIHVzZVZhbHVlOiBjb25maWd1cmF0aW9ufV1cbiAgICB9O1xuICB9XG59XG5cbmV4cG9ydCBmdW5jdGlvbiBzZXR1cFRyYW5zbGF0ZUZhY3Rvcnkoc2VydmljZTogVHJhbnNsYXRlU2VydmljZSk6IEZ1bmN0aW9uIHtcbiAgcmV0dXJuICgpID0+IHNlcnZpY2UudXNlKCk7XG59XG4iXX0= |
import { Pipe } from '@angular/core'; | ||
import * as i0 from "@angular/core"; | ||
import * as i1 from "./translator.service"; | ||
import * as i1 from "./translate.service"; | ||
export class TranslatePipe { | ||
@@ -9,14 +9,14 @@ constructor(translator) { | ||
transform(key) { | ||
if (this.translator.translations[key]) { | ||
return this.translator.translations[key]; | ||
let translation = this.translator.translations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else if (this.translator.autoGeneratedTranslations[key]) { | ||
return this.translator.autoGeneratedTranslations[key]; | ||
translation = this.translator.autoGeneratedTranslations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else { | ||
return key; | ||
} | ||
return key; | ||
} | ||
} | ||
TranslatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, deps: [{ token: i1.TranslatorService }], target: i0.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, deps: [{ token: i1.TranslateService }], target: i0.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, name: "tr", pure: false }); | ||
@@ -29,3 +29,3 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, decorators: [{ | ||
}] | ||
}], ctorParameters: function () { return [{ type: i1.TranslatorService }]; } }); | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhbnNsYXRlLnBpcGUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9wcm9qZWN0cy9kZngtdHJhbnNsYXRlL3NyYy9saWIvdHJhbnNsYXRlLnBpcGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFDLElBQUksRUFBZ0IsTUFBTSxlQUFlLENBQUM7OztBQU9sRCxNQUFNLE9BQU8sYUFBYTtJQUN4QixZQUFvQixVQUE2QjtRQUE3QixlQUFVLEdBQVYsVUFBVSxDQUFtQjtJQUFHLENBQUM7SUFFckQsU0FBUyxDQUFDLEdBQVE7UUFDaEIsSUFBSSxJQUFJLENBQUMsVUFBVSxDQUFDLFlBQVksQ0FBQyxHQUFHLENBQUMsRUFBRTtZQUNyQyxPQUFPLElBQUksQ0FBQyxVQUFVLENBQUMsWUFBWSxDQUFDLEdBQUcsQ0FBQyxDQUFDO1NBQzFDO2FBQU0sSUFBSSxJQUFJLENBQUMsVUFBVSxDQUFDLHlCQUF5QixDQUFDLEdBQUcsQ0FBQyxFQUFFO1lBQ3pELE9BQU8sSUFBSSxDQUFDLFVBQVUsQ0FBQyx5QkFBeUIsQ0FBQyxHQUFHLENBQUMsQ0FBQztTQUN2RDthQUFNO1lBQ0wsT0FBTyxHQUFHLENBQUM7U0FDWjtJQUNILENBQUM7OzBHQVhVLGFBQWE7d0dBQWIsYUFBYTsyRkFBYixhQUFhO2tCQUp6QixJQUFJO21CQUFDO29CQUNKLElBQUksRUFBRSxJQUFJO29CQUNWLElBQUksRUFBRSxLQUFLO2lCQUNaIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHtQaXBlLCBQaXBlVHJhbnNmb3JtfSBmcm9tICdAYW5ndWxhci9jb3JlJztcbmltcG9ydCB7VHJhbnNsYXRvclNlcnZpY2V9IGZyb20gJy4vdHJhbnNsYXRvci5zZXJ2aWNlJztcblxuQFBpcGUoe1xuICBuYW1lOiAndHInLFxuICBwdXJlOiBmYWxzZSxcbn0pXG5leHBvcnQgY2xhc3MgVHJhbnNsYXRlUGlwZSBpbXBsZW1lbnRzIFBpcGVUcmFuc2Zvcm0ge1xuICBjb25zdHJ1Y3Rvcihwcml2YXRlIHRyYW5zbGF0b3I6IFRyYW5zbGF0b3JTZXJ2aWNlKSB7fVxuXG4gIHRyYW5zZm9ybShrZXk6IGFueSk6IGFueSB7XG4gICAgaWYgKHRoaXMudHJhbnNsYXRvci50cmFuc2xhdGlvbnNba2V5XSkge1xuICAgICAgcmV0dXJuIHRoaXMudHJhbnNsYXRvci50cmFuc2xhdGlvbnNba2V5XTtcbiAgICB9IGVsc2UgaWYgKHRoaXMudHJhbnNsYXRvci5hdXRvR2VuZXJhdGVkVHJhbnNsYXRpb25zW2tleV0pIHtcbiAgICAgIHJldHVybiB0aGlzLnRyYW5zbGF0b3IuYXV0b0dlbmVyYXRlZFRyYW5zbGF0aW9uc1trZXldO1xuICAgIH0gZWxzZSB7XG4gICAgICByZXR1cm4ga2V5O1xuICAgIH1cbiAgfVxufVxuIl19 | ||
}], ctorParameters: function () { return [{ type: i1.TranslateService }]; } }); | ||
//# sour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AgbmFtZTogJ3RyJyxcbiAgcHVyZTogZmFsc2UsXG59KVxuZXhwb3J0IGNsYXNzIFRyYW5zbGF0ZVBpcGUgaW1wbGVtZW50cyBQaXBlVHJhbnNmb3JtIHtcbiAgY29uc3RydWN0b3IocHJpdmF0ZSB0cmFuc2xhdG9yOiBUcmFuc2xhdGVTZXJ2aWNlKSB7XG4gIH1cblxuICB0cmFuc2Zvcm0oa2V5OiBhbnkpOiBhbnkge1xuICAgIGxldCB0cmFuc2xhdGlvbiA9IHRoaXMudHJhbnNsYXRvci50cmFuc2xhdGlvbnNba2V5XTtcbiAgICBpZiAodHJhbnNsYXRpb24pIHtcbiAgICAgIHJldHVybiB0cmFuc2xhdGlvbjtcbiAgICB9XG5cbiAgICB0cmFuc2xhdGlvbiA9IHRoaXMudHJhbnNsYXRvci5hdXRvR2VuZXJhdGVkVHJhbnNsYXRpb25zW2tleV07XG4gICAgaWYgKHRyYW5zbGF0aW9uKSB7XG4gICAgICByZXR1cm4gdHJhbnNsYXRpb247XG4gICAgfVxuXG4gICAgcmV0dXJuIGtleTtcbiAgfVxufVxuIl19 |
@@ -5,4 +5,5 @@ /* | ||
export * from './lib/dfx-translate.module'; | ||
export * from './lib/translator.service'; | ||
export * from './lib/translate.service'; | ||
export * from './lib/translate.pipe'; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL2RmeC10cmFuc2xhdGUvc3JjL3B1YmxpYy1hcGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7O0dBRUc7QUFFSCxjQUFjLDRCQUE0QixDQUFDO0FBQzNDLGNBQWMsMEJBQTBCLENBQUM7QUFDekMsY0FBYyxzQkFBc0IsQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbIi8qXG4gKiBQdWJsaWMgQVBJIFN1cmZhY2Ugb2YgZGZ4LXRyYW5zbGF0ZVxuICovXG5cbmV4cG9ydCAqIGZyb20gJy4vbGliL2RmeC10cmFuc2xhdGUubW9kdWxlJztcbmV4cG9ydCAqIGZyb20gJy4vbGliL3RyYW5zbGF0b3Iuc2VydmljZSc7XG5leHBvcnQgKiBmcm9tICcuL2xpYi90cmFuc2xhdGUucGlwZSc7XG4iXX0= | ||
export * from './lib/translate.config'; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL2RmeC10cmFuc2xhdGUvc3JjL3B1YmxpYy1hcGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7O0dBRUc7QUFFSCxjQUFjLDRCQUE0QixDQUFDO0FBQzNDLGNBQWMseUJBQXlCLENBQUM7QUFDeEMsY0FBYyxzQkFBc0IsQ0FBQztBQUNyQyxjQUFjLHdCQUF3QixDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiLypcbiAqIFB1YmxpYyBBUEkgU3VyZmFjZSBvZiBkZngtdHJhbnNsYXRlXG4gKi9cblxuZXhwb3J0ICogZnJvbSAnLi9saWIvZGZ4LXRyYW5zbGF0ZS5tb2R1bGUnO1xuZXhwb3J0ICogZnJvbSAnLi9saWIvdHJhbnNsYXRlLnNlcnZpY2UnO1xuZXhwb3J0ICogZnJvbSAnLi9saWIvdHJhbnNsYXRlLnBpcGUnO1xuZXhwb3J0ICogZnJvbSAnLi9saWIvdHJhbnNsYXRlLmNvbmZpZyc7XG4iXX0= |
import * as i0 from '@angular/core'; | ||
import { Injectable, Inject, Pipe, APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { InjectionToken, Injectable, Inject, Pipe, APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import * as i1 from '@angular/common/http'; | ||
class TranslatorService { | ||
const TRANSLATE_CONFIG = new InjectionToken('TRANSLATE_CONFIG'); | ||
// noinspection JSUnusedLocalSymbols | ||
class TranslateService { | ||
constructor(config, http) { | ||
var _a, _b; | ||
this.config = config; | ||
@@ -13,38 +15,39 @@ this.http = http; | ||
this.selectedTranslation = ''; | ||
this.defaultLanguage = 'de'; | ||
this.autoGeneratedDefaultLanguage = 'en'; | ||
this.defaultLanguage = ((_a = config === null || config === void 0 ? void 0 : config.defaultLanguage) === null || _a === void 0 ? void 0 : _a.length) > 0 ? config.defaultLanguage : this.defaultLanguage; | ||
this.autoGeneratedDefaultLanguage = ((_b = config === null || config === void 0 ? void 0 : config.autoGeneratedDefaultLanguage) === null || _b === void 0 ? void 0 : _b.length) > 0 ? config.autoGeneratedDefaultLanguage : this.autoGeneratedDefaultLanguage; | ||
this.defaultLanguage = 'en'; | ||
this.useLocalStorage = true; | ||
this.defaultLanguage = (config === null || config === void 0 ? void 0 : config.defaultLanguage) != null ? config.defaultLanguage : this.defaultLanguage; | ||
this.useLocalStorage = (config === null || config === void 0 ? void 0 : config.useLocalStorage) != null ? config.useLocalStorage : this.useLocalStorage; | ||
} | ||
translate(key) { | ||
if (this.translations[key]) { | ||
return this.translations[key]; | ||
let translation = this.translations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else if (this.autoGeneratedTranslations[key]) { | ||
return this.autoGeneratedTranslations[key]; | ||
translation = this.autoGeneratedTranslations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else { | ||
return key; | ||
} | ||
return key; | ||
} | ||
use(lang) { | ||
if (lang === 'DEFAULT') { | ||
const language = localStorage.getItem('language'); | ||
use(lang = null) { | ||
if (lang === null) { | ||
const language = this.useLocalStorage ? localStorage.getItem('language') : null; | ||
if (language != null) { | ||
lang = language; | ||
console.log('Language cookie found! Using ' + lang); | ||
console.log('Language cookie found! Using "' + lang + '"'); | ||
} | ||
else { | ||
lang = this.defaultLanguage; | ||
console.log('No language cookie found! Using de as default'); | ||
console.log('No language cookie found! Using "' + this.defaultLanguage + '" as default'); | ||
} | ||
} | ||
else { | ||
console.log('Language changed to ' + lang); | ||
console.log('Language changed to "' + lang + "'"); | ||
} | ||
this.selectedTranslation = lang; | ||
localStorage.setItem('language', lang); | ||
if (this.useLocalStorage) { | ||
localStorage.setItem('language', lang); | ||
} | ||
return new Promise((resolve) => { | ||
const langPath = `assets/i18n/${lang || this.defaultLanguage}.json`; | ||
// noinspection JSUnusedLocalSymbols | ||
this.http.get(langPath).subscribe((translation) => { | ||
@@ -57,17 +60,19 @@ this.translations = Object.assign({}, translation || {}); | ||
}); | ||
const autoLangPath = `assets/i18n/${lang || this.autoGeneratedDefaultLanguage}_auto.json`; | ||
// noinspection JSUnusedLocalSymbols | ||
this.http.get(autoLangPath).subscribe((translation) => { | ||
this.autoGeneratedTranslations = Object.assign({}, translation || {}); | ||
resolve(this.autoGeneratedTranslations); | ||
}, (error) => { | ||
this.autoGeneratedTranslations = {}; | ||
resolve(this.autoGeneratedTranslations); | ||
}); | ||
// Only fetch auto generated translation if it is not the primary language (which contains all translations anyway) | ||
if (this.defaultLanguage !== lang) { | ||
const autoLangPath = `assets/i18n/${lang}_auto.json`; | ||
this.http.get(autoLangPath).subscribe((translation) => { | ||
this.autoGeneratedTranslations = Object.assign({}, translation || {}); | ||
resolve(this.autoGeneratedTranslations); | ||
}, (error) => { | ||
this.autoGeneratedTranslations = {}; | ||
resolve(this.autoGeneratedTranslations); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
TranslatorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatorService, deps: [{ token: 'config' }, { token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); | ||
TranslatorService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatorService, providedIn: 'root' }); | ||
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatorService, decorators: [{ | ||
TranslateService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslateService, deps: [{ token: TRANSLATE_CONFIG }, { token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); | ||
TranslateService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslateService, providedIn: 'root' }); | ||
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslateService, decorators: [{ | ||
type: Injectable, | ||
@@ -79,3 +84,3 @@ args: [{ | ||
type: Inject, | ||
args: ['config'] | ||
args: [TRANSLATE_CONFIG] | ||
}] }, { type: i1.HttpClient }]; } }); | ||
@@ -88,14 +93,14 @@ | ||
transform(key) { | ||
if (this.translator.translations[key]) { | ||
return this.translator.translations[key]; | ||
let translation = this.translator.translations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else if (this.translator.autoGeneratedTranslations[key]) { | ||
return this.translator.autoGeneratedTranslations[key]; | ||
translation = this.translator.autoGeneratedTranslations[key]; | ||
if (translation) { | ||
return translation; | ||
} | ||
else { | ||
return key; | ||
} | ||
return key; | ||
} | ||
} | ||
TranslatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, deps: [{ token: TranslatorService }], target: i0.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, deps: [{ token: TranslateService }], target: i0.ɵɵFactoryTarget.Pipe }); | ||
TranslatePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, name: "tr", pure: false }); | ||
@@ -108,3 +113,3 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.0", ngImport: i0, type: TranslatePipe, decorators: [{ | ||
}] | ||
}], ctorParameters: function () { return [{ type: TranslatorService }]; } }); | ||
}], ctorParameters: function () { return [{ type: TranslateService }]; } }); | ||
@@ -116,3 +121,3 @@ class DfxTranslateModule { | ||
ngModule: DfxTranslateModule, | ||
providers: [{ provide: 'config', useValue: configuration }] | ||
providers: [{ provide: TRANSLATE_CONFIG, useValue: configuration }] | ||
}; | ||
@@ -127,3 +132,3 @@ } | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -141,3 +146,3 @@ }, | ||
useFactory: setupTranslateFactory, | ||
deps: [TranslatorService], | ||
deps: [TranslateService], | ||
multi: true | ||
@@ -149,3 +154,3 @@ }, | ||
function setupTranslateFactory(service) { | ||
return () => service.use('DEFAULT'); | ||
return () => service.use(); | ||
} | ||
@@ -161,3 +166,3 @@ | ||
export { DfxTranslateModule, TranslatePipe, TranslatorService, setupTranslateFactory }; | ||
export { DfxTranslateModule, TRANSLATE_CONFIG, TranslatePipe, TranslateService, setupTranslateFactory }; | ||
//# sourceMappingURL=dfx-translate.js.map |
import { ModuleWithProviders } from '@angular/core'; | ||
import { TranslatorService } from "./translator.service"; | ||
import { TranslateService } from "./translate.service"; | ||
import { TranslateConfig } from "./translate.config"; | ||
import * as i0 from "@angular/core"; | ||
import * as i1 from "./translate.pipe"; | ||
export declare class DfxTranslateModule { | ||
static config(configuration: any): ModuleWithProviders<DfxTranslateModule>; | ||
static config(configuration: TranslateConfig): ModuleWithProviders<DfxTranslateModule>; | ||
static ɵfac: i0.ɵɵFactoryDeclaration<DfxTranslateModule, never>; | ||
@@ -11,2 +12,2 @@ static ɵmod: i0.ɵɵNgModuleDeclaration<DfxTranslateModule, [typeof i1.TranslatePipe], never, [typeof i1.TranslatePipe]>; | ||
} | ||
export declare function setupTranslateFactory(service: TranslatorService): Function; | ||
export declare function setupTranslateFactory(service: TranslateService): Function; |
import { PipeTransform } from '@angular/core'; | ||
import { TranslatorService } from './translator.service'; | ||
import { TranslateService } from './translate.service'; | ||
import * as i0 from "@angular/core"; | ||
export declare class TranslatePipe implements PipeTransform { | ||
private translator; | ||
constructor(translator: TranslatorService); | ||
constructor(translator: TranslateService); | ||
transform(key: any): any; | ||
@@ -8,0 +8,0 @@ static ɵfac: i0.ɵɵFactoryDeclaration<TranslatePipe, never>; |
{ | ||
"name": "dfx-translate", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"private": false, | ||
@@ -13,3 +13,10 @@ "license": "MIT", | ||
"translating", | ||
"json" | ||
"i18n", | ||
"internationalization", | ||
"json", | ||
"simple", | ||
"ngx", | ||
"ng", | ||
"autotranslation", | ||
"automation" | ||
], | ||
@@ -16,0 +23,0 @@ "author": "Dafnik", |
export * from './lib/dfx-translate.module'; | ||
export * from './lib/translator.service'; | ||
export * from './lib/translate.service'; | ||
export * from './lib/translate.pipe'; | ||
export * from './lib/translate.config'; |
103
README.md
# dfx-translate | ||
A simple translate package for Angular 4 - 12. | ||
@@ -7,2 +8,3 @@ | ||
### Installation | ||
```bash | ||
@@ -13,10 +15,13 @@ npm install dfx-translate@latest | ||
### Language file setup | ||
All of your language files are going to be stored in `src/assets/i18n/` folder. If there is no one in your project, create one. | ||
Depending on which primary language you want to write your application you can pick from **"Supported languages"**. | ||
All of your language files are going to be stored in `src/assets/i18n/` folder. If there is no one in your project, | ||
create one. Depending on which primary language you want to write your application you can pick from **"Supported | ||
languages"**. | ||
Lets say you've picked **english** as your primary language, create an `en.json` file with following content: | ||
```json | ||
{ | ||
"WELCOME": "Welcome", | ||
"WELCOME_SUBTEXT": "strange." | ||
"WELCOME_SUBTEXT": "stranger." | ||
} | ||
@@ -26,2 +31,3 @@ ``` | ||
Now you want to translate `Welcome` into german. Create a `de.json` file with the content: | ||
```json | ||
@@ -32,15 +38,25 @@ { | ||
``` | ||
Not translated properties can be auto translated with an Libre Translation Deep Learning API. More at **Auto deep learning translation** | ||
#### Auto translate not translated strings | ||
dfx-translate will take partially manual translated files into account, meaning it will only translate strings which | ||
weren't translated. Sadly you will need a LibreTranlsate instance or access to one for this feature to work. More at **Auto deep | ||
learning translation** | ||
### Registration in root (app) module | ||
This has to be done only once. | ||
This has to be done only once in your project. Please keep following infromation in mind. | ||
* The `defaultLanguage` property should be the same as your primary language. | ||
* There should not be an auto generated language file for your default / primary language. | ||
Use code values for the following properties from **"Supported languages"** | ||
property | description | ||
--- | --- | ||
defaultLanguage | Short name of default language | ||
autoGeneratedDefaultLanguage | Short name of default language for auto generated languages | ||
property | description | default value | ||
--- | --- | --- | ||
defaultLanguage | Short name of default language | `en` | ||
useLocalStorage | Save selected languages as the preference into local storage | `true` | ||
```typescript | ||
import {DfxTranslateModule} from "dfx-translate"; | ||
@NgModule({ | ||
@@ -50,3 +66,8 @@ declarations: [...], | ||
... | ||
DfxTranslateModule.config({configuration: { defaultLanguage: 'de', autoGeneratedDefaultLanguage: 'en' }}) | ||
DfxTranslateModule.config({ | ||
configuration: { | ||
defaultLanguage: 'de', | ||
useLocalStorage: false | ||
} | ||
}) | ||
], | ||
@@ -56,9 +77,12 @@ providers: [...], | ||
}) | ||
export class AppModule { } | ||
export class AppModule { | ||
} | ||
``` | ||
### Switch language | ||
Language switching is as easy as one function call. Just pass a code from **"Supported languages"** | ||
```typescript | ||
import {TranslatorService} from "dfx-translate"; | ||
import {TranslateService} from "dfx-translate"; | ||
@@ -71,4 +95,5 @@ @Component({ | ||
export class ExampleComponent { | ||
constructor(private translator: TranslatorService) { } | ||
constructor(private translator: TranslateService) { | ||
} | ||
setLang(lang: string) { | ||
@@ -81,4 +106,8 @@ this.translator.use(lang); | ||
### Pipeline usage | ||
If you want to use the tranlsation pipe you have to import the `DfxTranslateModule` into your used module | ||
```typescript | ||
import {DfxTranslateModule} from "dfx-translate"; | ||
@NgModule({ | ||
@@ -88,3 +117,3 @@ declarations: [...], | ||
... | ||
DfxTranslateModule, | ||
DfxTranslateModule, | ||
... | ||
@@ -94,5 +123,8 @@ ], | ||
}) | ||
export class ExampleModule {} | ||
export class ExampleModule { | ||
} | ||
``` | ||
After that you can simple use the `tr` pipe in html | ||
```angular2html | ||
@@ -104,6 +136,7 @@ <h1>{{'WELCOME' | tr}}</h1> | ||
### Service usage | ||
It's also easily possible to access translations via code with the `TranslatorService`: | ||
```typescript | ||
import {TranslatorService} from "dfx-translate"; | ||
import {TranslateService} from "dfx-translate"; | ||
@@ -116,4 +149,5 @@ @Component({ | ||
export class ExampleComponent { | ||
constructor(private translator: TranslatorService) { } | ||
constructor(private translator: TranslateService) { | ||
} | ||
save() { | ||
@@ -126,21 +160,25 @@ window.alert(this.translator.translate('WELCOME')); | ||
### Auto deep learning translation | ||
It's possible to translate not manual translated strings via LibreTranslates Deep Learning API. dfx-translate will take | ||
partially manual translated files into account. | ||
The command syntax is | ||
It's possible to translate not manual translated strings via LibreTranslate API. LibreTranslate is an open source selfhostable machine-translation | ||
service. Visit https://github.com/LibreTranslate/LibreTranslate for more information. | ||
dfx-translate will take partially manual translated files into account. The command syntax is | ||
```bash | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js {API_URL} {PRIMARY_LANGUAGE_FILE} {SOURCE_LANGUAGE_CODE} {TARGET_LANGUAGE_CODE} | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js {API_URL} {SOURCE_LANGUAGE_CODE} {TARGET_LANGUAGE_CODE} | ||
``` | ||
For simplicity purposes I wrote a little shell script. Put this at the top / root level of your project in `languages.sh` | ||
For simplicity purposes I wrote a little shell script. Put this at the top / root level of your project | ||
in `languages.sh` | ||
```bash | ||
#!/usr/bin/env bash | ||
main () { | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/en.json en de | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/en.json en fr | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/en.json en es | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/en.json en it | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/en.json en pt | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate en de | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate en fr | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate en es | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate en it | ||
node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate en pt | ||
# If you have prettier installed uncomment this line | ||
# If you have any prettier library intstalled, execute it here | ||
# prettier --config ./.prettierrc --write ./src/assets/i18n | ||
@@ -152,7 +190,10 @@ | ||
``` | ||
dfx-translate will put all auto generated properties in `src/assets/i18n/`. The file names will be something like `de_auto.json` or `en_auto.json`. | ||
dfx-translate will put all auto generated properties in `src/assets/i18n/`. The file names will be something | ||
like `de_auto.json` or `en_auto.json`. | ||
Before an production build run | ||
## Supported languages | ||
code | name | ||
@@ -159,0 +200,0 @@ --- | --- |
@@ -1,2 +0,2 @@ | ||
// noinspection JSCheckFunctionSignatures NodeCoreCodingAssistance JSUnresolvedVariable | ||
// noinspection JSCheckFunctionSignatures, NodeCoreCodingAssistance, JSUnresolvedVariable, NpmUsedModulesInstalled | ||
@@ -10,8 +10,8 @@ /** | ||
* Command syntax: | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js {API_URL} ./src/path/file.json {INPUT_LANG} {TARGET_LANG} | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js {API_URL} {INPUT_LANG} {TARGET_LANG} | ||
* | ||
* Command examples: | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/de.json de en | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/de.json de fr | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate ./src/assets/i18n/de.json de es | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate de en | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate de fr | ||
* node ./node_modules/dfx-translate/translation/generateTranslation.js https://translate.abc.abc/translate de es | ||
*/ | ||
@@ -23,5 +23,4 @@ | ||
const URL = process.argv[2] || null; | ||
const INPUT_FILE = process.argv[3] || './de.json'; | ||
const INPUT_LANG = process.argv[4] || 'de'; | ||
const TARGET_LANG = process.argv[5] || 'en'; | ||
const INPUT_LANG = process.argv[3] || 'de'; | ||
const TARGET_LANG = process.argv[4] || 'en'; | ||
@@ -202,3 +201,3 @@ let localCache = null; | ||
* @param {Array} book output variable array. Translated terms. | ||
* @param {Integer} currentIndex of proccesing queue | ||
* @param {Number} currentIndex of proccesing queue | ||
*/ | ||
@@ -235,4 +234,3 @@ const convert = (arr, book = [], currentIndex = 0) => | ||
// noinspection JSCheckFunctionSignatures | ||
getJSON(INPUT_FILE) // get data from input file | ||
getJSON('./src/assets/i18n/' + INPUT_LANG + '.json') // get data from input file | ||
.then((input_vars) => convertToArray(input_vars)) // convert data from object to array | ||
@@ -239,0 +237,0 @@ .then((input_arr) => processTranslation(input_arr, TARGET_LANG, process.env.GKEY)) // process the whole array |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73201
21
779
200