Files
mattermostest/src/main/i18nManager.ts
Devin Binnie 9b36c25e4e [MM-52696] Upgrade and clean up Desktop App dev dependencies (#2970)
* Upgrade to ESLint v8

* Upgrade TypeScript, api-types, react-intl

* Remove unnecessary dependencies

* Update to React 17.0.2

* npm audit fixes, remove storybook

* Lock some packages

* Remove nan patch

* Remove some deprecated dependencies

* Fix lint/type/tests

* Merge'd

* Fix bad use of spawn

* Fix notarize

* Fix afterpack, switch to tsc es2020

* Fix api types

* Use @mattermost/eslint-plugin
2024-03-07 15:55:33 -05:00

64 lines
1.7 KiB
TypeScript

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {ipcMain} from 'electron';
import {GET_AVAILABLE_LANGUAGES, GET_LANGUAGE_INFORMATION} from 'common/communication';
import {Logger} from 'common/log';
import type {Language} from '../../i18n/i18n';
import {languages} from '../../i18n/i18n';
export function localizeMessage(s: string, defaultString = '', values: any = {}) {
let str = i18nManager.currentLanguage.url[s] || defaultString;
for (const key of Object.keys(values)) {
str = str.replace(new RegExp(`{${key}}`, 'g'), values[key]);
}
return str;
}
const log = new Logger('i18nManager');
export class I18nManager {
currentLanguage: Language;
constructor() {
this.currentLanguage = this.getLanguages().en;
ipcMain.handle(GET_LANGUAGE_INFORMATION, this.getCurrentLanguage);
ipcMain.handle(GET_AVAILABLE_LANGUAGES, this.getAvailableLanguages);
}
setLocale = (locale: string) => {
log.debug('setLocale', locale);
if (this.isLanguageAvailable(locale)) {
this.currentLanguage = this.getLanguages()[locale];
log.info('Set new language', locale);
return true;
}
log.warn('Failed to set new language', locale);
return false;
};
getLanguages = () => {
return languages;
};
getAvailableLanguages = () => {
return Object.keys(languages);
};
isLanguageAvailable = (locale: string) => {
return Boolean(this.getLanguages()[locale]);
};
getCurrentLanguage = () => {
return this.currentLanguage;
};
}
const i18nManager = new I18nManager();
export default i18nManager;