BetterDiscordApp-rauenzi/renderer/src/modules/localemanager.js

38 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-04-08 02:31:02 +02:00
import * as Locales from "../../../assets/locales";
2019-06-27 06:21:51 +02:00
import DiscordModules from "./discordmodules";
import Utilities from "./utilities";
import Events from "./emitter";
2022-08-01 23:53:43 +02:00
const {Dispatcher, UserSettingsStore} = DiscordModules;
2019-06-27 06:21:51 +02:00
export default new class LocaleManager {
get discordLocale() {return UserSettingsStore.locale;}
get defaultLocale() {return "en-US";}
2019-06-27 06:21:51 +02:00
constructor() {
2019-06-27 06:21:51 +02:00
this.locale = "";
2021-04-08 02:31:02 +02:00
this.strings = Utilities.extend({}, Locales[this.defaultLocale]);
}
2019-06-28 01:50:20 +02:00
2021-04-08 02:31:02 +02:00
initialize() {
this.setLocale(this.discordLocale);
2022-08-01 23:53:43 +02:00
Dispatcher.subscribe("USER_SETTINGS_UPDATE", ({settings}) => {
2019-06-27 06:21:51 +02:00
const newLocale = settings.locale;
if (newLocale && newLocale != this.locale) this.setLocale(newLocale);
2019-06-27 06:21:51 +02:00
});
}
2019-06-27 06:21:51 +02:00
2021-04-08 02:31:02 +02:00
setLocale(newLocale) {
2019-06-27 06:21:51 +02:00
let newStrings;
if (newLocale != this.defaultLocale) {
2021-04-08 02:31:02 +02:00
newStrings = Locales[newLocale];
2019-06-27 06:21:51 +02:00
if (!newStrings) return this.setLocale(this.defaultLocale);
}
else {
2021-04-08 02:31:02 +02:00
newStrings = Locales[this.defaultLocale];
2019-06-27 06:21:51 +02:00
}
this.locale = newLocale;
2021-10-22 22:34:48 +02:00
Utilities.extendTruthy(this.strings, newStrings);
Events.emit("strings-updated");
}
2019-06-27 06:21:51 +02:00
};