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

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-05-20 00:37:21 +02:00
import * as Locales from "@assets/locales/index";
2019-06-27 06:21:51 +02:00
import DiscordModules from "./discordmodules";
import Utilities from "./utilities";
import Events from "./emitter";
2022-10-10 08:32:57 +02:00
const {LocaleStore} = DiscordModules;
2019-06-27 06:21:51 +02:00
export default new class LocaleManager {
2022-08-17 02:33:44 +02:00
get discordLocale() {return LocaleStore?.locale ?? this.defaultLocale;}
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-10-10 08:32:57 +02:00
LocaleStore?.addChangeListener((newLocale) => this.setLocale(newLocale));
}
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
};