Browse Source

feat: translation system

Sven 1 year ago
parent
commit
16ce257
3 changed files with 42 additions and 0 deletions
  1. 5 0
      .changeset/wild-cups-press.md
  2. 1 0
      lib/index.ts
  3. 36 0
      lib/translation.ts

+ 5 - 0
.changeset/wild-cups-press.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Added lightweight translation system

+ 1 - 0
lib/index.ts

@@ -4,3 +4,4 @@ export * from "./dom";
 export * from "./math";
 export * from "./misc";
 export * from "./onSelector";
+export * from "./translation";

+ 36 - 0
lib/translation.ts

@@ -0,0 +1,36 @@
+import { Stringifiable, insertValues } from "./misc";
+
+/** Trans rights! 🏳️‍⚧️ */
+const trans: Record<string, Record<string, string>> = {};
+let currentLanguage: string;
+
+/**
+ * Returns the translated text for the specified key in the current language set by `setLanguage()`  
+ * If the key is not found in the previously registered translation, the key itself is returned.  
+ *   
+ * ⚠️ Remember to register a language with `tr.addLanguage()` and set it as active with `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(!currentLanguage)
+    return key;
+  const trText = trans[currentLanguage]?.[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) => {
+  currentLanguage = language;
+};
+
+export { tr };