Skip to content

Commit

Permalink
refactor(i18n): simplify getting default lang
Browse files Browse the repository at this point in the history
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 (ngx-translate/core#565)
  • Loading branch information
tkohr committed Mar 20, 2023
1 parent 6af4504 commit 3af42f4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
13 changes: 1 addition & 12 deletions libs/util/i18n/src/lib/i18n.constants.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions libs/util/i18n/src/lib/util-i18n.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
}
}

0 comments on commit 3af42f4

Please sign in to comment.