12345678910111213141516171819202122232425262728293031323334353637383940 |
- import { Stringifiable, insertValues } from "./misc";
- /** Trans rights! 🏳️⚧️ */
- const trans: Record<string, Record<string, string>> = {};
- let curLang: string;
- /**
- * Returns the translated text for the specified key in the current language set by {@linkcode tr.setLanguage()}
- * If the key is not found in the previously registered translation, the key itself is returned.
- *
- * ⚠️ Remember to register a language with {@linkcode tr.addLanguage()} and set it as active with {@linkcode tr.setLanguage()} before using this function, otherwise it will always return the key itself.
- * @param key Key of the translation to return
- * @param args Optional arguments to be passed to the translated text. They will replace placeholders in the format `%n`, where `n` is the 1-indexed argument number
- */
- function tr(key: string, ...args: Stringifiable[]) {
- if(!curLang)
- return key;
- const trText = trans[curLang]?.[key];
- if(!trText)
- return key;
- if(args.length > 0 && trText.match(/%\d/)) {
- return insertValues(trText, ...args);
- }
- return trText;
- }
- tr.addLanguage = (language: string, translations: Record<string, string>) => {
- trans[language] = translations;
- };
- tr.setLanguage = (language: string) => {
- curLang = language;
- };
- tr.getLanguage = () => {
- return curLang;
- };
- export { tr };
|