@doku-dev/doku-fragment
Version:
A new Angular UI library that moving away from Bootstrap and built from scratch.
104 lines (99 loc) • 4.14 kB
JavaScript
import { registerLocaleData, DOCUMENT } from '@angular/common';
import { provideHttpClient, HttpClient } from '@angular/common/http';
import * as en from '@angular/common/locales/en';
import * as id from '@angular/common/locales/id';
import * as i0 from '@angular/core';
import { inject, Injectable, makeEnvironmentProviders, LOCALE_ID, APP_INITIALIZER } from '@angular/core';
import { loadTranslations } from '@angular/localize';
import { map, tap } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
class I18nService {
constructor() {
this.route = inject(ActivatedRoute);
/**
* @default 'selected-language'
*/
this.SELECTED_LANG_KEY = 'selected-language';
}
/**
* Get active language from query params or local storage.
*
* The value is normalized to uppercase.
* Defaults to "ID" if not exists on local storage or when the language is not supported.
*/
getActiveLanguage() {
const paramLang = this.route.snapshot.queryParamMap.get('lang');
if (paramLang && ['EN', 'ID'].includes(paramLang)) {
return paramLang;
}
let value = localStorage.getItem(this.SELECTED_LANG_KEY) || 'ID';
value = value.toUpperCase();
if (['EN', 'ID'].includes(value))
return value;
return 'ID';
}
/**
* The language value will be normalized to uppercase.
*/
setActiveLanguage(language) {
const normalizedLanguage = language.toUpperCase();
localStorage.setItem(this.SELECTED_LANG_KEY, normalizedLanguage);
}
}
I18nService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: I18nService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
I18nService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: I18nService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: I18nService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}] });
registerLocaleData(en.default, 'en');
registerLocaleData(id.default, 'id');
function provideI18n(options) {
return makeEnvironmentProviders([
provideHttpClient(),
{
provide: LOCALE_ID,
useFactory: (i18nService, document) => {
// Set custom key for the selected language.
// We set the options on this provider because it first called
// rather than putting it to the APP_INITIALIZERS.
if (options === null || options === void 0 ? void 0 : options.key) {
i18nService.SELECTED_LANG_KEY = options.key;
}
const locale = i18nService.getActiveLanguage();
// Set locale to the html lang attribute/
document.documentElement.lang = locale.toLowerCase();
return locale;
},
deps: [I18nService, DOCUMENT],
},
{
provide: APP_INITIALIZER,
useFactory: (http, localeId) => {
const localeDir = normalizeDirPath((options === null || options === void 0 ? void 0 : options.localeDirectoryPath) || 'assets/locales');
return () => http
.get(`${localeDir}/${localeId.toLowerCase()}.json`)
.pipe(map((res) => res['translations']), tap((translations) => loadTranslations(translations)));
},
deps: [HttpClient, LOCALE_ID],
multi: true,
},
]);
}
function normalizeDirPath(path) {
let normalizedPath = path;
if (path.startsWith('/')) {
normalizedPath = normalizedPath.substring(1);
}
if (path.endsWith('/')) {
normalizedPath = normalizedPath.substring(0, normalizedPath.length - 1);
}
return normalizedPath;
}
/**
* Generated bundle index. Do not edit.
*/
export { I18nService, provideI18n };
//# sourceMappingURL=doku-dev-doku-fragment-providers.mjs.map