translation.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { insertValues } from "./misc.js";
  2. import type { Stringifiable } from "./types.js";
  3. /** Trans rights! 🏳️‍⚧️ */
  4. const trans: Record<string, Record<string, string>> = {};
  5. let curLang: string;
  6. const trLang = (language: string, key: string, ...args: Stringifiable[]): string => {
  7. if(!language)
  8. return key;
  9. const trText = trans[language]?.[key];
  10. if(!trText)
  11. return key;
  12. if(args.length > 0 && trText.match(/%\d/)) {
  13. return insertValues(trText, ...args);
  14. }
  15. return trText;
  16. };
  17. /**
  18. * Returns the translated text for the specified key in the current language set by {@linkcode tr.setLanguage()}
  19. * Use {@linkcode tr.forLang()} to get the translation for a specific language instead of the currently set one.
  20. * If the key is not found in the currently set language, the key itself is returned.
  21. *
  22. * ⚠️ 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.
  23. * @param key Key of the translation to return
  24. * @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
  25. */
  26. function tr(key: string, ...args: Stringifiable[]): string {
  27. return trLang(curLang, key, ...args);
  28. }
  29. /**
  30. * Returns the translated text for the specified key in the specified language.
  31. * If the key is not found in the specified previously registered translation, the key itself is returned.
  32. *
  33. * ⚠️ Remember to register a language with {@linkcode tr.addLanguage()} before using this function, otherwise it will always return the key itself.
  34. * @param language Language to use for the translation
  35. * @param key Key of the translation to return
  36. * @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
  37. */
  38. tr.forLang = trLang;
  39. /**
  40. * Registers a new language with its translations.
  41. * The translations are a key-value pair where the key is the translation key and the value is the translated text.
  42. *
  43. * The translations can contain placeholders in the format `%n`, where `n` is the 1-indexed argument number.
  44. * These placeholders will be replaced by the arguments passed to the translation functions.
  45. * @param language Language code to register
  46. * @param translations Translations for the specified language
  47. */
  48. tr.addLanguage = (language: string, translations: Record<string, string>): void => {
  49. trans[language] = translations;
  50. };
  51. /**
  52. * Sets the active language for the translation functions.
  53. * This language will be used by the {@linkcode tr()} function to return the translated text.
  54. * If the language is not registered with {@linkcode tr.addLanguage()}, the translation functions will always return the key itself.
  55. * @param language Language code to set as active
  56. */
  57. tr.setLanguage = (language: string): void => {
  58. curLang = language;
  59. };
  60. /**
  61. * Returns the active language set by {@linkcode tr.setLanguage()}
  62. * If no language is set, this function will return `undefined`.
  63. * @returns Active language code
  64. */
  65. tr.getLanguage = (): string => {
  66. return curLang;
  67. };
  68. /**
  69. * Returns the translations for the specified language or currently active one.
  70. * If the language is not registered with {@linkcode tr.addLanguage()}, this function will return `undefined`.
  71. * @param language Language code to get translations for - defaults to the active language set by {@linkcode tr.setLanguage()}
  72. * @returns Translations for the specified language
  73. */
  74. tr.getTranslations = (language?: string): Record<string, string> | undefined => {
  75. return trans[language ?? curLang];
  76. };
  77. export { tr };