From 3af42f424440556b68b40c170d16a33b4ec33ccf Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Mon, 20 Mar 2023 11:45:25 +0100 Subject: [PATCH] refactor(i18n): simplify getting default lang note: we do not set the html lang attribute to the appropriate value so far which ngx-translate still does not seem to handle neither (https://github.com/ngx-translate/core/issues/565) --- libs/util/i18n/src/lib/i18n.constants.ts | 13 +------------ libs/util/i18n/src/lib/util-i18n.module.ts | 6 +++--- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/libs/util/i18n/src/lib/i18n.constants.ts b/libs/util/i18n/src/lib/i18n.constants.ts index bb8fc11b77..cd2016c2e7 100644 --- a/libs/util/i18n/src/lib/i18n.constants.ts +++ b/libs/util/i18n/src/lib/i18n.constants.ts @@ -1,10 +1,8 @@ import { HttpClient } from '@angular/common/http' import { ToolsApiService } from '@geonetwork-ui/data-access/gn4' import { TranslateCompiler, TranslateLoader } from '@ngx-translate/core' -import { TranslateHttpLoader } from '@ngx-translate/http-loader' import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler' import { Gn4TranslateLoader } from './gn4.translate.loader' -import { map } from 'rxjs/operators' import { FileTranslateLoader } from './file.translate.loader' export const DEFAULT_LANG = 'en' @@ -37,19 +35,10 @@ export const LANG_2_TO_3_MAPPER = Object.entries(LANG_3_TO_2_MAPPER).reduce( export function HttpLoaderFactory(http: HttpClient) { return new FileTranslateLoader(http, './assets/i18n/') } - -export function getLangFromHtml() { - const html: HTMLElement = document.getElementsByTagName('html')[0] - const lang = html.getAttribute('lang') - return lang.substr(0, 2) -} +//Deprecated, but currently still used in datafeeder export function getLangFromBrowser() { return navigator.language.substr(0, 2) } -export function getDefaultLang() { - return getLangFromHtml() || 'en' -} - export const TRANSLATE_DEFAULT_CONFIG = { compiler: { provide: TranslateCompiler, diff --git a/libs/util/i18n/src/lib/util-i18n.module.ts b/libs/util/i18n/src/lib/util-i18n.module.ts index 7ef1b5e7b8..0168ee468e 100644 --- a/libs/util/i18n/src/lib/util-i18n.module.ts +++ b/libs/util/i18n/src/lib/util-i18n.module.ts @@ -2,7 +2,7 @@ import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http' import { NgModule } from '@angular/core' import { ApiModule } from '@geonetwork-ui/data-access/gn4' import { TranslateModule, TranslateService } from '@ngx-translate/core' -import { getDefaultLang } from './i18n.constants' +import { DEFAULT_LANG } from './i18n.constants' import { I18nInterceptor } from './i18n.interceptor' import { CommonModule } from '@angular/common' @@ -20,7 +20,7 @@ import { CommonModule } from '@angular/common' }) export class UtilI18nModule { constructor(translate: TranslateService) { - translate.setDefaultLang(getDefaultLang()) - translate.use(translate.getBrowserLang() || getDefaultLang()) + translate.setDefaultLang(DEFAULT_LANG) + translate.use(translate.getBrowserLang() || DEFAULT_LANG) } }