switch to posix strings for language internal

This commit is contained in:
Matt Hill
2026-01-16 16:25:08 -07:00
parent 8ca3d56aa9
commit fee03ef407
8 changed files with 71 additions and 58 deletions

View File

@@ -56,7 +56,7 @@ export const ENGLISH = {
'Beginning shutdown': 57,
'Add': 58,
'Ok': 59,
'french': 60,
'fr_FR': 60,
'This value cannot be changed once set': 61,
'Continue': 62,
'Click or drop file here': 63,
@@ -462,10 +462,10 @@ export const ENGLISH = {
'StartOS UI': 485,
'WiFi': 486,
'Documentation': 487, // as in, a website to view documentation
'spanish': 488,
'polish': 489,
'german': 490,
'english': 491,
'es_ES': 488,
'pl_PL': 489,
'de_DE': 490,
'en_US': 491,
'Start Menu': 492,
'Install Progress': 493,
'Downloading': 494,
@@ -670,9 +670,9 @@ export const ENGLISH = {
'Preserve': 706,
'Overwrite': 707,
'Unlock': 708,
'Drive': 709, // as in, a storage device
'Drive': 709, // the noun, a storage device
'Transfer': 710, // the verb
'The list is empty': 711,
'Restart now': 712,
'Later': 713, // as in, (do it) later
} as const
} as Record<any, any>

View File

@@ -6,7 +6,7 @@ import {
TuiLanguageSwitcherService,
} from '@taiga-ui/i18n'
import { ENGLISH } from './dictionaries/en'
import { i18nService } from './i18n.service'
import { i18nService, Languages } from './i18n.service'
export type i18nKey = keyof typeof ENGLISH
export type i18n = Record<(typeof ENGLISH)[i18nKey], string>
@@ -20,7 +20,7 @@ export const I18N_LOADER = new InjectionToken<
>('')
export const I18N_STORAGE = new InjectionToken<
(lang: TuiLanguageName) => Promise<void>
(lang: Languages) => Promise<void>
>('', {
factory: () => () => Promise.resolve(),
})

View File

@@ -2,6 +2,20 @@ import { inject, Injectable, signal } from '@angular/core'
import { TuiLanguageName, TuiLanguageSwitcherService } from '@taiga-ui/i18n'
import { I18N, I18N_LOADER, I18N_STORAGE } from './i18n.providers'
export const languages = ['en_US', 'es_ES', 'de_DE', 'fr_FR', 'pl_PL'] as const
export type Languages = (typeof languages)[number]
/**
* Maps POSIX locale strings to TUI language names
*/
export const LANGUAGE_TO_TUI: Record<Languages, TuiLanguageName> = {
en_US: 'english',
es_ES: 'spanish',
de_DE: 'german',
fr_FR: 'french',
pl_PL: 'polish',
}
@Injectable({
providedIn: 'root',
})
@@ -12,20 +26,32 @@ export class i18nService extends TuiLanguageSwitcherService {
readonly loading = signal(false)
override setLanguage(language: TuiLanguageName = 'english'): void {
/**
* Current language as POSIX locale string
*/
get lang(): Languages {
return (
(Object.entries(LANGUAGE_TO_TUI).find(
([, tui]) => tui === this.language,
)?.[0] as Languages) || 'en_US'
)
}
setLang(language: Languages = 'en_US'): void {
const tuiLang = LANGUAGE_TO_TUI[language]
const current = this.language
super.setLanguage(language)
super.setLanguage(tuiLang)
this.loading.set(true)
if (current === language) {
this.i18nLoader(language).then(value => {
if (current === tuiLang) {
this.i18nLoader(tuiLang).then(value => {
this.i18n.set(value)
this.loading.set(false)
})
} else {
this.store(language).then(() =>
this.i18nLoader(language).then(value => {
this.i18nLoader(tuiLang).then(value => {
this.i18n.set(value)
this.loading.set(false)
}),
@@ -33,12 +59,3 @@ export class i18nService extends TuiLanguageSwitcherService {
}
}
}
export const languages = [
'english',
'spanish',
'polish',
'german',
'french',
] as const
export type Languages = (typeof languages)[number]

View File

@@ -18,22 +18,22 @@ export interface Language {
* Available languages with their metadata
*/
export const LANGUAGES: Language[] = [
{ code: 'en', name: 'english', nativeName: 'English' },
{ code: 'es', name: 'spanish', nativeName: 'Español' },
{ code: 'de', name: 'german', nativeName: 'Deutsch' },
{ code: 'fr', name: 'french', nativeName: 'Français' },
{ code: 'pl', name: 'polish', nativeName: 'Polski' },
{ code: 'en', name: 'en_US', nativeName: 'English' },
{ code: 'es', name: 'es_ES', nativeName: 'Español' },
{ code: 'de', name: 'de_DE', nativeName: 'Deutsch' },
{ code: 'fr', name: 'fr_FR', nativeName: 'Français' },
{ code: 'pl', name: 'pl_PL', nativeName: 'Polski' },
]
/**
* Maps i18n language names to ISO language codes
* Maps POSIX locale strings to ISO language codes
*/
export const LANGUAGE_TO_CODE: Record<Languages, LanguageCode> = {
english: 'en',
spanish: 'es',
german: 'de',
french: 'fr',
polish: 'pl',
en_US: 'en',
es_ES: 'es',
de_DE: 'de',
fr_FR: 'fr',
pl_PL: 'pl',
}
/**