fnando / i18n

A small library to provide the I18n translations on the JavaScript.
https://fnando.github.io/i18n/
MIT License
169 stars 20 forks source link

Feature Request: ignore '.' (dot) in translation keys #59

Closed hssdiv closed 1 year ago

hssdiv commented 1 year ago

I was having same issue as people in i18n-js issues like this one: https://github.com/fnando/i18n-js/issues/424

Maybe somebody will need solution for i18n library:

1) import library from ./src like that: import { I18n, TranslateOptions } from 'i18n-js/src';

2) add defaultSeparator const i18n: I18n = new I18n(translations, { defaultSeparator: '|' });

3) apply patch-package (https://github.com/ds300/patch-package) here is it's code:

diff --git a/node_modules/i18n-js/src/I18n.ts b/node_modules/i18n-js/src/I18n.ts
index 43476f5..d53712e 100644
--- a/node_modules/i18n-js/src/I18n.ts
+++ b/node_modules/i18n-js/src/I18n.ts
@@ -255,11 +255,11 @@ export class I18n {
    * @returns {void}
    */
   public store(translations: Dict): void {
-    const map = propertyFlatList(translations);
+    const map = propertyFlatList(translations, this.defaultSeparator);

-    map.forEach((path) =>
-      set(this.translations, path, get(translations, path)),
-    );
+    map.forEach((path) => {
+      this.translations[path] = translations[path.split(this.defaultSeparator)[0]][path.split(this.defaultSeparator)[1]]
+    })

     this.hasChanged();
   }
diff --git a/node_modules/i18n-js/src/helpers/lookup.ts b/node_modules/i18n-js/src/helpers/lookup.ts
index 595d17c..b11dd27 100644
--- a/node_modules/i18n-js/src/helpers/lookup.ts
+++ b/node_modules/i18n-js/src/helpers/lookup.ts
@@ -36,9 +36,7 @@ export function lookup(i18n: I18n, scope: Scope, options: Dict = {}): any {
     .map((component) => i18n.transformKey(component))
     .join(".");

-  const entries = locales.map((locale) =>
-    get(i18n.translations, [locale, scope].join(".")),
-  );
+  const entries = locales.map((locale) => i18n.translations[[locale, scope].join(i18n.defaultSeparator)]);

   entries.push(options.defaultValue);

diff --git a/node_modules/i18n-js/src/helpers/propertyFlatList.ts b/node_modules/i18n-js/src/helpers/propertyFlatList.ts
index 5ebc9e2..d83ee90 100644
--- a/node_modules/i18n-js/src/helpers/propertyFlatList.ts
+++ b/node_modules/i18n-js/src/helpers/propertyFlatList.ts
@@ -10,9 +10,11 @@ interface Indexable {

 class PropertyFlatList {
   public target: Dict;
+  public defaultSeparator: string;

-  constructor(target: Dict) {
+  constructor(target: Dict, defaultSeparator: string) {
     this.target = target;
+    this.defaultSeparator = defaultSeparator;
   }

   call(): string[] {
@@ -30,8 +32,7 @@ class PropertyFlatList {
   compute(value: unknown, path: string): unknown {
     if (!isArray(value) && isObject(value)) {
       return Object.keys(value).map((key) => 
-        this.compute((value as Indexable)[key] as unknown, `${path}.${key}`),
-      );
+        this.compute((value as Indexable)[key] as unknown, `${path}${this.defaultSeparator}${key}`));
     } else {
       return path;
     }
@@ -70,6 +71,6 @@ class PropertyFlatList {
  *
  * @returns {string[]} The list of paths.
  */
-export function propertyFlatList(target: Dict): string[] {
-  return new PropertyFlatList(target).call();
+export function propertyFlatList(target: Dict, defaultSeparator: string): string[] {
+  return new PropertyFlatList(target, defaultSeparator).call();
 }