@sv443-network/userutils 7.2.2 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @sv443-network/userutils
2
2
 
3
+ ## 7.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 7e8e147: - Added `tr.forLang()` to get the translation for a specified language code
8
+ - Added `tr.getTranslations()` to return the translations object for the given or currently active language
9
+
3
10
  ## 7.2.2
4
11
 
5
12
  ### Patch Changes
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  ## UserUtils
5
5
  Lightweight library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more.
6
6
 
7
- Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and UMD / global declaration via `@require`.
7
+ Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require`
8
8
  If you like using this library, please consider [supporting the development ❤️](https://github.com/sponsors/Sv443)
9
9
 
10
10
  <br>
@@ -63,9 +63,11 @@ View the documentation of previous major releases:
63
63
  - [`randomizeArray()`](#randomizearray) - returns a copy of the array with its items in a random order
64
64
  - [**Translation:**](#translation)
65
65
  - [`tr()`](#tr) - simple translation of a string to another language
66
+ - [`tr.forLang()`](#trforlang) - specify a language besides the currently set one for the translation
66
67
  - [`tr.addLanguage()`](#traddlanguage) - add a language and its translations
67
68
  - [`tr.setLanguage()`](#trsetlanguage) - set the currently active language for translations
68
69
  - [`tr.getLanguage()`](#trgetlanguage) - returns the currently active language
70
+ - [`tr.getTranslations()`](#trgettranslations) - returns the translations for the given language or the currently active one
69
71
  - [**Colors:**](#colors)
70
72
  - [`hexToRgb()`](#hextorgb) - convert a hex color string to an RGB number tuple
71
73
  - [`rgbToHex()`](#rgbtohex) - convert RGB numbers to a hex color string
@@ -1960,6 +1962,39 @@ console.log(tr("welcome_name", "John")); // "Willkommen, John"
1960
1962
 
1961
1963
  <br>
1962
1964
 
1965
+ ### tr.forLang()
1966
+ Usage:
1967
+ ```ts
1968
+ tr.forLang(language: string, key: string, ...values: Stringifiable[]): string
1969
+ ```
1970
+
1971
+ Returns the translation of the passed key in the specified language. Otherwise behaves exactly like [`tr()`](#tr)
1972
+ This function does not change the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
1973
+
1974
+ <details><summary><b>Example - click to view</b></summary>
1975
+
1976
+ ```ts
1977
+ import { tr } from "@sv443-network/userutils";
1978
+
1979
+ tr.addLanguage("en", {
1980
+ "welcome_name": "Welcome, %1",
1981
+ });
1982
+
1983
+ tr.addLanguage("de", {
1984
+ "welcome_name": "Willkommen, %1",
1985
+ });
1986
+
1987
+ // the language is set to "en"
1988
+ tr.setLanguage("en");
1989
+
1990
+ console.log(tr("welcome_name", "John")); // "Welcome"
1991
+ // the language doesn't need to be changed:
1992
+ console.log(tr.forLang("de", "welcome_name", "John")); // "Willkommen, John"
1993
+ ```
1994
+ </details>
1995
+
1996
+ <br>
1997
+
1963
1998
  ### tr.addLanguage()
1964
1999
  Usage:
1965
2000
  ```ts
@@ -2080,6 +2115,37 @@ tr.getLanguage(): string | undefined
2080
2115
  Returns the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
2081
2116
  If no language has been set yet, it will return undefined.
2082
2117
 
2118
+ <br>
2119
+
2120
+ ### tr.getTranslations()
2121
+ Usage:
2122
+ ```ts
2123
+ tr.getTranslations(language?: string): Record<string, string> | undefined
2124
+ ```
2125
+
2126
+ Returns the translations of the specified language.
2127
+ If no language is specified, it will return the translations of the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
2128
+ If no translations are found, it will return undefined.
2129
+
2130
+ <details><summary><b>Example - click to view</b></summary>
2131
+
2132
+ ```ts
2133
+ import { tr } from "@sv443-network/userutils";
2134
+
2135
+ tr.addLanguage("en", {
2136
+ "welcome": "Welcome",
2137
+ });
2138
+
2139
+ console.log(tr.getTranslations()); // undefined
2140
+ tr.setLanguage("en");
2141
+ console.log(tr.getTranslations()); // { "welcome": "Welcome" }
2142
+
2143
+ console.log(tr.getTranslations("en")); // { "welcome": "Welcome" }
2144
+
2145
+ console.log(tr.getTranslations("de")); // undefined
2146
+ ```
2147
+ </details>
2148
+
2083
2149
  <br><br>
2084
2150
 
2085
2151
  ## Colors:
@@ -8,7 +8,7 @@
8
8
  // ==UserLibrary==
9
9
  // @name UserUtils
10
10
  // @description Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more
11
- // @version 7.2.2
11
+ // @version 7.3.0
12
12
  // @license MIT
13
13
  // @copyright Sv443 (https://github.com/Sv443)
14
14
 
@@ -1392,18 +1392,22 @@ Has: ${checksum}`);
1392
1392
  // lib/translation.ts
1393
1393
  var trans = {};
1394
1394
  var curLang;
1395
- function tr(key, ...args) {
1395
+ var trLang = (language, key, ...args) => {
1396
1396
  var _a;
1397
- if (!curLang)
1397
+ if (!language)
1398
1398
  return key;
1399
- const trText = (_a = trans[curLang]) == null ? void 0 : _a[key];
1399
+ const trText = (_a = trans[language]) == null ? void 0 : _a[key];
1400
1400
  if (!trText)
1401
1401
  return key;
1402
1402
  if (args.length > 0 && trText.match(/%\d/)) {
1403
1403
  return insertValues(trText, ...args);
1404
1404
  }
1405
1405
  return trText;
1406
+ };
1407
+ function tr(key, ...args) {
1408
+ return trLang(curLang, key, ...args);
1406
1409
  }
1410
+ tr.forLang = trLang;
1407
1411
  tr.addLanguage = (language, translations) => {
1408
1412
  trans[language] = translations;
1409
1413
  };
@@ -1413,6 +1417,9 @@ Has: ${checksum}`);
1413
1417
  tr.getLanguage = () => {
1414
1418
  return curLang;
1415
1419
  };
1420
+ tr.getTranslations = (language) => {
1421
+ return trans[language != null ? language : curLang];
1422
+ };
1416
1423
 
1417
1424
  exports.DataStore = DataStore;
1418
1425
  exports.DataStoreSerializer = DataStoreSerializer;
package/dist/index.js CHANGED
@@ -1352,18 +1352,22 @@ var SelectorObserver = class {
1352
1352
  // lib/translation.ts
1353
1353
  var trans = {};
1354
1354
  var curLang;
1355
- function tr(key, ...args) {
1355
+ var trLang = (language, key, ...args) => {
1356
1356
  var _a;
1357
- if (!curLang)
1357
+ if (!language)
1358
1358
  return key;
1359
- const trText = (_a = trans[curLang]) == null ? void 0 : _a[key];
1359
+ const trText = (_a = trans[language]) == null ? void 0 : _a[key];
1360
1360
  if (!trText)
1361
1361
  return key;
1362
1362
  if (args.length > 0 && trText.match(/%\d/)) {
1363
1363
  return insertValues(trText, ...args);
1364
1364
  }
1365
1365
  return trText;
1366
+ };
1367
+ function tr(key, ...args) {
1368
+ return trLang(curLang, key, ...args);
1366
1369
  }
1370
+ tr.forLang = trLang;
1367
1371
  tr.addLanguage = (language, translations) => {
1368
1372
  trans[language] = translations;
1369
1373
  };
@@ -1373,5 +1377,8 @@ tr.setLanguage = (language) => {
1373
1377
  tr.getLanguage = () => {
1374
1378
  return curLang;
1375
1379
  };
1380
+ tr.getTranslations = (language) => {
1381
+ return trans[language != null ? language : curLang];
1382
+ };
1376
1383
 
1377
1384
  export { DataStore, DataStoreSerializer, Dialog, NanoEmitter, SelectorObserver, addGlobalStyle, addParent, autoPlural, clamp, compress, computeHash, currentDialogId, darkenColor, debounce, decompress, defaultDialogCss, defaultStrings, fetchAdvanced, getSiblingsFrame, getUnsafeWindow, hexToRgb, insertValues, interceptEvent, interceptWindowEvent, isScrollable, lightenColor, mapRange, observeElementProp, openDialogs, openInNewTab, pauseFor, preloadImages, randRange, randomId, randomItem, randomItemIndex, randomizeArray, rgbToHex, setInnerHtmlUnsafe, takeRandomItem, tr };
@@ -1,7 +1,8 @@
1
1
  import type { Stringifiable } from "./types.js";
2
2
  /**
3
3
  * Returns the translated text for the specified key in the current language set by {@linkcode tr.setLanguage()}
4
- * If the key is not found in the previously registered translation, the key itself is returned.
4
+ * Use {@linkcode tr.forLang()} to get the translation for a specific language instead of the currently set one.
5
+ * If the key is not found in the currently set language, the key itself is returned.
5
6
  *
6
7
  * ⚠️ 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.
7
8
  * @param key Key of the translation to return
@@ -9,8 +10,10 @@ import type { Stringifiable } from "./types.js";
9
10
  */
10
11
  declare function tr(key: string, ...args: Stringifiable[]): string;
11
12
  declare namespace tr {
13
+ var forLang: (language: string, key: string, ...args: Stringifiable[]) => string;
12
14
  var addLanguage: (language: string, translations: Record<string, string>) => void;
13
15
  var setLanguage: (language: string) => void;
14
16
  var getLanguage: () => string;
17
+ var getTranslations: (language?: string | undefined) => Record<string, string> | undefined;
15
18
  }
16
19
  export { tr };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sv443-network/userutils",
3
3
  "libName": "UserUtils",
4
- "version": "7.2.2",
4
+ "version": "7.3.0",
5
5
  "description": "Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",