Update and wire in the pt_BR translations

This commit is contained in:
Dermot Duffy
2022-02-13 14:43:49 -08:00
parent 526e9b3250
commit 96ef5b244c
2 changed files with 33 additions and 10 deletions
+19 -2
View File
@@ -1,13 +1,28 @@
import * as en from './languages/en.json';
import * as pt_BR from './languages/pt_br.json';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const languages: any = {
en: en,
pt_BR: pt_BR,
};
export function localize(string: string, search = '', replace = ''): string {
const lang = (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_');
// Get the browser language.
let lang = localStorage
.getItem('selectedLanguage')
?.replace(/['"]+/g, '')
.replace('-', '_');
// If that's not specified, try to find the Home Assistant language.
if (!lang) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const hass = (document.querySelector('home-assistant') as any)?.hass;
lang = hass.selectedLanguage || hass.language;
}
// Default to English is there's still no language setting.
lang = lang || 'en';
let translated: string;
try {
@@ -16,7 +31,9 @@ export function localize(string: string, search = '', replace = ''): string {
translated = string.split('.').reduce((o, i) => o[i], languages['en']);
}
if (translated === undefined) translated = string.split('.').reduce((o, i) => o[i], languages['en']);
if (translated === undefined) {
translated = string.split('.').reduce((o, i) => o[i], languages['en']);
}
if (search !== '' && replace !== '') {
translated = translated.replace(search, replace);