Skip to content

Commit

Permalink
Allways fallback to default language (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Nov 27, 2022
1 parent b81115b commit 6107548
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/components/dialogs/hacs-dialog-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class HacsDialogBase extends LitElement {
shouldUpdate(changedProperties: PropertyValues) {
changedProperties.forEach((_oldValue, propName) => {
if (propName === "hass") {
this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"';
this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"';
}
});
return (
Expand All @@ -42,6 +42,6 @@ export class HacsDialogBase extends LitElement {

public connectedCallback() {
super.connectedCallback();
this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"';
this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"';
}
}
2 changes: 1 addition & 1 deletion src/components/dialogs/hacs-download-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class HacsDonwloadDialog extends HacsDialogBase {
shouldUpdate(changedProperties: PropertyValues) {
changedProperties.forEach((_oldValue, propName) => {
if (propName === "hass") {
this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"';
this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"';
}
if (propName === "repositories") {
this._fetchRepository();
Expand Down
42 changes: 22 additions & 20 deletions src/localize/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@ import { languages } from "./generated";
const DEFAULT_LANGUAGE = "en";
const IGNORE_LANGUAGES = new Set(["en_GB"]);
const logger = new HacsLogger("localize");
const warnings: { language: string[]; sting: Record<string, Array<string>> } = {
language: [],
sting: {},
};

const _localizationCache = {};

export function localize(language: string, string: string, replace?: Record<string, any>): string {
let lang = (language || localStorage.getItem("selectedLanguage") || DEFAULT_LANGUAGE)
try {
return _localize(language, string, replace);
} catch (err: any) {
logger.error(err?.message || `Unknown error translating '${string}' with '${language}'`);
return _localize(DEFAULT_LANGUAGE, string, replace);
}
}

function _localize(language: string, string: string, replace?: Record<string, any>): string {
let lang = (language || window?.localStorage?.getItem("selectedLanguage") || DEFAULT_LANGUAGE)
.replace(/['"]+/g, "")
.replace("-", "_");

if (IGNORE_LANGUAGES.has(lang) || !languages[lang]) {
if (!IGNORE_LANGUAGES.has(lang) && !warnings.language?.includes(lang)) {
warnings.language.push(lang);
logger.warn(
`Language '${lang.replace(
"_",
"-"
)}' is not added to HACS, using '${DEFAULT_LANGUAGE}' instead. https://hacs.xyz/docs/developer/translation`
);
}
if (!languages[lang] || IGNORE_LANGUAGES.has(lang)) {
lang = DEFAULT_LANGUAGE;
}

const translatedValue = languages[lang]?.[string] || languages[DEFAULT_LANGUAGE][string];

if (!translatedValue) {
logger.error(`Translation problem with '${string}' for '${lang}'`);
if (lang !== DEFAULT_LANGUAGE) {
throw Error(`'${string}' is unknown'`);
}
return string;
}

const messageKey = string + translatedValue;
const messageKey = `${string}_${translatedValue}`;

let translatedMessage = _localizationCache[messageKey] as IntlMessageFormat | undefined;

if (!translatedMessage) {
try {
translatedMessage = new IntlMessageFormat(translatedValue, language);
translatedMessage = new IntlMessageFormat(translatedValue, lang);
} catch (err: any) {
logger.error(`Translation problem with '${string}' for '${lang}'`);
if (lang !== DEFAULT_LANGUAGE) {
throw Error(`Translation problem with '${string}' for '${lang}'`);
}
return string;
}
_localizationCache[messageKey] = translatedMessage;
Expand All @@ -54,7 +54,9 @@ export function localize(language: string, string: string, replace?: Record<stri
try {
return translatedMessage.format<string>(replace) as string;
} catch (err: any) {
logger.error(`Translation problem with '${string}' for '${lang}'`);
if (lang !== DEFAULT_LANGUAGE) {
throw Error(`Translation problem with placeholders for '${string}' for '${lang}'`);
}
return string;
}
}

0 comments on commit 6107548

Please sign in to comment.