profile + i18n
This commit is contained in:
@@ -1,35 +1,18 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { isEmpty } from 'rxjs/operators';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class I18nService {
|
||||
|
||||
locale: String;
|
||||
locales = ["de-informal"];
|
||||
locale: string;
|
||||
locales : any = ["de-informal"];
|
||||
i18n: any;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
let browserLocale = navigator.language;
|
||||
|
||||
if (browserLocale.indexOf("-") != -1) {
|
||||
browserLocale = browserLocale.split("-")[0];
|
||||
}
|
||||
|
||||
let locale = localStorage.getItem("bstly.locale") || browserLocale || this.locales[0];
|
||||
|
||||
if (locale == 'de') {
|
||||
locale = 'de-informal';
|
||||
}
|
||||
|
||||
if (this.locales.indexOf(locale) == -1) {
|
||||
locale = this.locales[0];
|
||||
}
|
||||
|
||||
|
||||
this.setLocale(locale);
|
||||
|
||||
}
|
||||
|
||||
getLocales() {
|
||||
@@ -44,25 +27,44 @@ export class I18nService {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
async fetch(locale) {
|
||||
this.i18n = await this.http.get("./assets/i18n/" + locale + ".json").toPromise();
|
||||
async fetch() {let browserLocale = navigator.language;
|
||||
|
||||
if(browserLocale.indexOf("-") != -1) {
|
||||
browserLocale = browserLocale.split("-")[0];
|
||||
}
|
||||
|
||||
let locale = localStorage.getItem("bstly.locale") || browserLocale || this.locales[0];
|
||||
|
||||
if(locale == 'de') {
|
||||
locale = 'de-informal';
|
||||
}
|
||||
|
||||
this.locales = await this.http.get(environment.apiUrl + "/i18n").toPromise();
|
||||
|
||||
if(this.locales.indexOf(locale) == -1) {
|
||||
locale = this.locales[0];
|
||||
}
|
||||
|
||||
this.i18n = await this.http.get(environment.apiUrl + "/i18n/" + locale).toPromise();
|
||||
|
||||
this.setLocale(locale);
|
||||
}
|
||||
|
||||
get(key, args: any[]): String {
|
||||
get(key, args: string[]): string {
|
||||
return this.getInternal(key, args, this.i18n);
|
||||
}
|
||||
|
||||
getInternal(key, args: any[], from): String {
|
||||
if (!from) {
|
||||
getInternal(key, args: string[], from): string {
|
||||
if(!from) {
|
||||
return key;
|
||||
} else if (from[key]) {
|
||||
if (from[key]["."]) {
|
||||
} else if(from[key]) {
|
||||
if(from[key]["."]) {
|
||||
return this.insertArguments(from[key]["."], args);
|
||||
}
|
||||
return this.insertArguments(from[key], args);
|
||||
} else {
|
||||
let keys = key.split(".");
|
||||
if (from[keys[0]]) {
|
||||
if(from[keys[0]]) {
|
||||
key = keys.slice(1, keys.length).join(".");
|
||||
return this.getInternal(key, args, from[keys[0]])
|
||||
}
|
||||
@@ -71,10 +73,10 @@ export class I18nService {
|
||||
return key;
|
||||
}
|
||||
|
||||
insertArguments(label: String, args: any[]) {
|
||||
if (args) {
|
||||
for (let index in args) {
|
||||
label = label.replace(`{${index}}`, args[index]);
|
||||
insertArguments(label: string, args: string[]) {
|
||||
if(args) {
|
||||
for(let index in args) {
|
||||
label = label.replace(`{${index}}`, this.get(args[index], []));
|
||||
}
|
||||
}
|
||||
return label;
|
||||
|
||||
Reference in New Issue
Block a user