2019-06-24 21:47:24 +02:00
|
|
|
import DiscordModules from "./discordmodules";
|
|
|
|
import RawStrings from "../data/strings";
|
|
|
|
import Utilities from "./utilities";
|
2019-06-25 22:36:34 +02:00
|
|
|
import FormattableString from "../structs/string";
|
|
|
|
import Events from "./emitter";
|
2019-06-24 21:47:24 +02:00
|
|
|
|
2019-06-26 05:17:16 +02:00
|
|
|
const {Dispatcher, DiscordConstants, UserSettingsStore} = DiscordModules;
|
2019-06-24 21:47:24 +02:00
|
|
|
const Messages = {};
|
|
|
|
|
2019-06-26 05:17:16 +02:00
|
|
|
const discordLocale = UserSettingsStore.locale.split("-")[0];
|
|
|
|
|
2019-06-24 21:47:24 +02:00
|
|
|
export let currentLocale = "en";
|
|
|
|
export function setLocale(newLocale) {
|
|
|
|
currentLocale = newLocale;
|
2019-06-25 22:36:34 +02:00
|
|
|
Utilities.extend(Messages, RawStrings[currentLocale]);
|
|
|
|
Events.emit("strings-updated");
|
2019-06-24 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.extend(Messages, RawStrings[currentLocale]);
|
|
|
|
|
2019-06-26 05:17:16 +02:00
|
|
|
if (RawStrings[discordLocale] && discordLocale != "en") setLocale(discordLocale);
|
|
|
|
|
2019-06-24 21:47:24 +02:00
|
|
|
Dispatcher.subscribe(DiscordConstants.ActionTypes.USER_SETTINGS_UPDATE, ({settings}) => {
|
|
|
|
const newLocale = settings.locale;
|
|
|
|
if (newLocale && newLocale != currentLocale) setLocale(newLocale.split("-")[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default new Proxy(Messages, {
|
|
|
|
get: function(strings, category) {
|
2019-06-25 22:36:34 +02:00
|
|
|
if (!strings.hasOwnProperty(category)) {
|
|
|
|
return new Proxy({}, {
|
|
|
|
get: function() {
|
|
|
|
return `String group "${category}" not found.`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return new Proxy(strings[category], {
|
|
|
|
get: function(obj, prop) {
|
|
|
|
if (typeof(obj[prop]) == "string") return new FormattableString(obj[prop]);
|
|
|
|
return obj[prop];
|
2019-06-24 21:47:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|