Add localization

This commit is contained in:
ludeeus
2019-12-15 23:28:48 +00:00
parent 859ab76340
commit 19a88abd43
4 changed files with 48 additions and 3 deletions
+5 -3
View File
@@ -15,9 +15,11 @@ import { BoilerplateCardConfig } from './types';
import { actionHandler } from './action-handler-directive';
import { CARD_VERSION } from './const';
import { localize } from './localize/localize';
/* eslint no-console: 0 */
console.info(
`%c BOILERPLATE-CARD \n%c Version ${CARD_VERSION} `,
`%c BOILERPLATE-CARD \n%c ${localize('common.version')} ${CARD_VERSION} `,
'color: orange; font-weight: bold; background: black',
'color: white; font-weight: bold; background: dimgray',
);
@@ -40,7 +42,7 @@ export class BoilerplateCard extends LitElement {
public setConfig(config: BoilerplateCardConfig): void {
// TODO Check for required fields and that they are of the proper format
if (!config || config.show_error) {
throw new Error('Invalid configuration');
throw new Error(localize('common.invalid_configuration'));
}
if (config.test_gui) {
@@ -66,7 +68,7 @@ export class BoilerplateCard extends LitElement {
if (this._config.show_warning) {
return html`
<ha-card>
<div class="warning">Show Warning</div>
<div class="warning">${localize('common.show_warning')}</div>
</ha-card>
`;
}
+7
View File
@@ -0,0 +1,7 @@
{
"common": {
"version": "Version",
"invalid_configuration": "Invalid configuration",
"show_warning": "Show Warning"
}
}
+7
View File
@@ -0,0 +1,7 @@
{
"common": {
"version": "VErsjon",
"invalid_configuration": "Ikke gyldig konfiguration",
"show_warning": "Vis advarsel"
}
}
+29
View File
@@ -0,0 +1,29 @@
import * as en from './languages/en.json';
import * as nb from './languages/nb.json';
var languages = {
en: en,
nb: nb,
};
export function localize(string: string, search: string = '', replace: string = '') {
const section = string.split('.')[0];
const key = string.split('.')[1];
const lang = (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_');
var tranlated: string;
try {
tranlated = languages[lang][section][key];
} catch (e) {
tranlated = languages['en'][section][key];
}
if (tranlated === undefined) tranlated = languages['en'][section][key];
if (search !== '' && replace !== '') {
tranlated = tranlated.replace(search, replace);
}
return tranlated;
}