151 lines
4.3 KiB
TypeScript
151 lines
4.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { environment } from '../../environments/environment';
|
|
import { MatPaginatorIntl } from '@angular/material/paginator';
|
|
import { Subject, firstValueFrom } from 'rxjs';
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class I18nService {
|
|
|
|
locale: string = "de-informal";
|
|
locales: any = ["en", "de-informal"];
|
|
i18n: any;
|
|
|
|
constructor(private http: HttpClient,
|
|
private route: ActivatedRoute) { }
|
|
|
|
getLocales() {
|
|
return this.locales;
|
|
}
|
|
|
|
getLocale() {
|
|
return this.locale;
|
|
}
|
|
|
|
setLocale(locale) {
|
|
this.locale = locale;
|
|
}
|
|
|
|
async fetch() {
|
|
try {
|
|
this.locales = await firstValueFrom(this.http.get(environment.apiUrl + "/i18n"));
|
|
} catch {
|
|
this.locales = ["en", "de-informal"];
|
|
}
|
|
let browserLocale = window.navigator.language || window.parent.navigator.language;
|
|
|
|
if (browserLocale.indexOf("-") != -1) {
|
|
browserLocale = browserLocale.split("-")[0];
|
|
}
|
|
|
|
let locale = browserLocale || this.locales[0];
|
|
try {
|
|
locale = localStorage.getItem("bstly.locale") || locale;
|
|
} catch {
|
|
locale = window.location.search.split("locale=")[1];
|
|
}
|
|
|
|
if (locale == 'de') {
|
|
locale = 'de-informal';
|
|
}
|
|
|
|
if (this.locales.indexOf(locale) == -1) {
|
|
locale = this.locales[0];
|
|
}
|
|
|
|
|
|
try {
|
|
this.i18n = await firstValueFrom(this.http.get(environment.apiUrl + "/i18n/" + locale));
|
|
} catch {
|
|
this.i18n = await firstValueFrom(this.http.get("/assets/i18n/" + locale + ".json"));
|
|
}
|
|
|
|
this.setLocale(locale);
|
|
}
|
|
|
|
get(key, args: string[]): string {
|
|
return this.getInternal(key, args, this.i18n, "", true);
|
|
}
|
|
|
|
getEmpty(key, args: string[]): string {
|
|
return this.getInternal(key, args, this.i18n, "", false);
|
|
}
|
|
|
|
getInternal(key, args: string[], from, path, empty: boolean): string {
|
|
key += '';
|
|
if (!from) {
|
|
return empty ? this.empty(key, args, path) : (key || "");
|
|
} else if (from[key]) {
|
|
if (typeof from[key] === 'object') {
|
|
if (from[key][""]) {
|
|
return this.insertArguments(from[key][""], args);
|
|
}
|
|
return empty ? this.empty(key, args, path) : (key || "");
|
|
}
|
|
return this.insertArguments(from[key], args);
|
|
} else {
|
|
let keys = key.split(".");
|
|
if (from[keys[0]]) {
|
|
key = keys.slice(1, keys.length).join(".");
|
|
return this.getInternal(key, args, from[keys[0]], path + keys[0] + ".", empty)
|
|
}
|
|
}
|
|
|
|
return empty ? this.empty(key, args, path) : (key || "");
|
|
}
|
|
|
|
empty(key, args: string[], path: string): string {
|
|
return (path ? path + (path.endsWith(".") ? "" : ".") : "") + key + (args && args.length > 0 ? (" [" + args + "]") : "");
|
|
}
|
|
|
|
insertArguments(label: string, args: string[]) {
|
|
if (args) {
|
|
for (let index in args) {
|
|
label = label.replace(`{${index}}`, this.get(args[index], null));
|
|
}
|
|
}
|
|
return label;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@Injectable()
|
|
export class I18nPaginatorIntl implements MatPaginatorIntl {
|
|
|
|
changes = new Subject<void>();
|
|
|
|
i18n: I18nService;
|
|
|
|
itemsPerPageLabel: string;
|
|
nextPageLabel: string;
|
|
previousPageLabel: string;
|
|
firstPageLabel: string;
|
|
lastPageLabel: string;
|
|
|
|
|
|
injectI18n(i18n: I18nService) {
|
|
this.i18n = i18n;
|
|
|
|
this.firstPageLabel = this.i18n.get('paginator.firstPage', []);
|
|
this.itemsPerPageLabel = this.i18n.get('paginator.itemsPerPage', []);
|
|
this.lastPageLabel = this.i18n.get('paginator.lastPage', []);
|
|
|
|
this.nextPageLabel = this.i18n.get('paginator.nextPage', []);
|
|
this.previousPageLabel = this.i18n.get('paginator.previousPage', []);
|
|
|
|
}
|
|
|
|
|
|
getRangeLabel(page: number, pageSize: number, length: number): string {
|
|
if (length === 0) {
|
|
return this.i18n.get('paginator.empty', []);
|
|
}
|
|
|
|
const amountPages = Math.ceil(length / pageSize);
|
|
return this.i18n.get('paginator.range', [page + 1 + "", amountPages + ""]);
|
|
}
|
|
} |