jb-core 0.17.0 → 0.18.1

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/i18n/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## config file
4
4
  JB Design System use html tag `lang` attribute to set the default language of it's components.
5
+
5
6
  ```html
6
7
  <html lang="fa">
7
8
  <!-- or -->
@@ -9,13 +10,46 @@ JB Design System use html tag `lang` attribute to set the default language of it
9
10
  ```
10
11
 
11
12
  if you want to set your locale manually in javascript you just have to import `i18n` and set your default locale:
13
+
12
14
  ```ts
13
15
  import {i18n} from 'jb-core/i18n';
14
16
  // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale for more setting detail
15
17
  i18n.setLocale(new Intl.Locale("fa"))
16
18
  ```
17
19
 
18
- ## internal methods
20
+ ## Dictionary
21
+
22
+ Dictionary is where we keep our messages and texts string for different languages
23
+
24
+ ```ts
25
+ import {JBDictionary} from 'jb-core/i18n';
26
+
27
+ export const dictionary = new JBDictionary({
28
+ "fa":{
29
+ yourKey:"مقدار شما"
30
+ },
31
+ "en":{
32
+ yourKey:"your value"
33
+ }
34
+ });
35
+ ```
36
+ ### Add new language
37
+
38
+ you can add or replace currently exists language by using `setLanguage` method.
39
+
40
+ ```ts
41
+ dictionary.setLanguage("jp",{yourKey:"あなたの鍵"})
42
+ ```
43
+
44
+ ### getValue
45
+
46
+ ```ts
47
+ import {i18n} from "jb-core/i18n";
48
+
49
+ dictionary.get(i18n,"yourKey")
50
+ ```
51
+
52
+ ## Internal Methods
19
53
 
20
54
  this methods are internal methods and intended to be used inside jb design system modules but you can also use them if you are creating modules
21
55
 
@@ -1,3 +1,20 @@
1
1
  import type { JBI18N } from "./i18n";
2
2
  export declare function getRequiredMessage(context: JBI18N, label?: string): string;
3
+ export declare class JBDictionary<T extends object> {
4
+ dictionary: Record<string, T>;
5
+ constructor(initialDictionary: Record<string, T>);
6
+ /**
7
+ * add new or replace existing language with given dictionary
8
+ * @param languageKey language standard key like "fa" or "en"
9
+ * @param dictionary key value object of strings and messages
10
+ */
11
+ setLanguage(languageKey: string, dictionary: T): void;
12
+ /**
13
+ * get value of given key in dictionary with fallback to english dictionary
14
+ * @param i18n instance of i18n config
15
+ * @param key key of dictionary object
16
+ * @returns value of the dictionary with "en" fallback
17
+ */
18
+ get<K extends keyof T>(i18n: JBI18N, key: K): T[K];
19
+ }
3
20
  //# sourceMappingURL=dictionary.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dictionary.d.ts","sourceRoot":"","sources":["../lib/dictionary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,wBAAgB,kBAAkB,CAAC,OAAO,EAAC,MAAM,EAAC,KAAK,CAAC,EAAC,MAAM,UAQ9D"}
1
+ {"version":3,"file":"dictionary.d.ts","sourceRoot":"","sources":["../lib/dictionary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,wBAAgB,kBAAkB,CAAC,OAAO,EAAC,MAAM,EAAC,KAAK,CAAC,EAAC,MAAM,UAQ9D;AACD,qBAAa,YAAY,CAAC,CAAC,SAAS,MAAM;IACxC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAC,CAAC,CAAC,CAAK;gBACrB,iBAAiB,EAAC,MAAM,CAAC,MAAM,EAAC,CAAC,CAAC;IAG9C;;;;OAIG;IACH,WAAW,CAAC,WAAW,EAAC,MAAM,EAAC,UAAU,EAAC,CAAC;IAI3C;;;;;OAKG;IACH,GAAG,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,IAAI,EAAC,MAAM,EAAC,GAAG,EAAC,CAAC,GAAE,CAAC,CAAC,CAAC,CAAC;CAI/C"}
@@ -1 +1 @@
1
- {"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../lib/i18n.ts"],"names":[],"mappings":"AAAA;;;EAGE;AACF,qBAAa,MAAM;IACjB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAA;;IAoBnB,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM;CAG9B;AAED,eAAO,MAAM,IAAI,QAAe,CAAC"}
1
+ {"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../lib/i18n.ts"],"names":[],"mappings":"AAAA;;;EAGE;AACF,qBAAa,MAAM;IACjB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAA;;IAqBnB,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM;CAG9B;AAED,eAAO,MAAM,IAAI,QAAe,CAAC"}
@@ -1,2 +1,2 @@
1
- var e=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",region:"US"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const l=new e;exports.JBI18N=e,exports.getRequiredMessage=function(e,l){return"fa"===e.locale.language?l?`لطفاً ${l} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":l?`Please enter your ${l}`:"Please complete this field"},exports.i18n=l;
1
+ var e=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",numeric:!1,region:"IR"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const t=new e;exports.JBDictionary=class{constructor(e){this.dictionary={},this.dictionary=e}setLanguage(e,t){this.dictionary[e]=t}get(e,t){const n=this.dictionary[e.locale.language]?this.dictionary[e.locale.language]:this.dictionary.en;return n[t]?n[t]:this.dictionary.en[t]}},exports.JBI18N=e,exports.getRequiredMessage=function(e,t){return"fa"===e.locale.language?t?`لطفاً ${t} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":t?`Please enter your ${t}`:"Please complete this field"},exports.i18n=t;
2
2
  //# sourceMappingURL=index.cjs.js.map
Binary file
Binary file
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"AAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,8CC5BxB,SAAmC,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C"}
1
+ {"version":3,"file":"index.cjs.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"AAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,SAAQ,EACR,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,uBCpBxB,MAEE,WAAA,CAAY,GADZ,KAAA,WAA+B,CAAE,EAE/B,KAAK,WAAa,CACnB,CAMD,WAAA,CAAY,EAAmB,GAC7B,KAAK,WAAW,GAAe,CAChC,CAQD,GAAA,CAAuB,EAAY,GACjC,MAAM,EAAM,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,GACxG,OAAO,EAAI,GAAK,EAAI,GAAK,KAAK,WAAW,GAAM,EAChD,+CAhCH,SAAmC,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C"}
@@ -1,2 +1,2 @@
1
- var e=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",region:"US"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const l=new e;function n(e,l){return"fa"===e.locale.language?l?`لطفاً ${l} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":l?`Please enter your ${l}`:"Please complete this field"}export{e as JBI18N,n as getRequiredMessage,l as i18n};
1
+ var e=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",numeric:!1,region:"IR"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const a=new e;function n(e,a){return"fa"===e.locale.language?a?`لطفاً ${a} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":a?`Please enter your ${a}`:"Please complete this field"}var t=class{constructor(e){this.dictionary={},this.dictionary=e}setLanguage(e,a){this.dictionary[e]=a}get(e,a){const n=this.dictionary[e.locale.language]?this.dictionary[e.locale.language]:this.dictionary.en;return n[a]?n[a]:this.dictionary.en[a]}};export{t as JBDictionary,e as JBI18N,n as getRequiredMessage,a as i18n};
2
2
  //# sourceMappingURL=index.js.map
Binary file
Binary file
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"AAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,EC5BxB,SAAgB,EAAmB,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"AAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,SAAQ,EACR,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,EC7BxB,SAAgB,EAAmB,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C,CACD,IAAa,EAAb,MAEE,WAAA,CAAY,GADZ,KAAA,WAA+B,CAAE,EAE/B,KAAK,WAAa,CACnB,CAMD,WAAA,CAAY,EAAmB,GAC7B,KAAK,WAAW,GAAe,CAChC,CAQD,GAAA,CAAuB,EAAY,GACjC,MAAM,EAAM,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,GACxG,OAAO,EAAI,GAAK,EAAI,GAAK,KAAK,WAAW,GAAM,EAChD"}
@@ -1,2 +1,2 @@
1
- !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).JBCoreI18N={})}(this,(function(e){var n=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",region:"US"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const o=new n;e.JBI18N=n,e.getRequiredMessage=function(e,n){return"fa"===e.locale.language?n?`لطفاً ${n} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":n?`Please enter your ${n}`:"Please complete this field"},e.i18n=o}));
1
+ !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).JBCoreI18N={})}(this,(function(e){var n=class{constructor(){if("fa"===(document?.documentElement?.lang||"en"))this.locale=new Intl.Locale("fa",{calendar:"persian",numeric:!1,region:"IR"});else this.locale=new Intl.Locale("en",{calendar:"gregory",region:"US"})}setLocale(e){this.locale=e}};const t=new n;e.JBDictionary=class{constructor(e){this.dictionary={},this.dictionary=e}setLanguage(e,n){this.dictionary[e]=n}get(e,n){const t=this.dictionary[e.locale.language]?this.dictionary[e.locale.language]:this.dictionary.en;return t[n]?t[n]:this.dictionary.en[n]}},e.JBI18N=n,e.getRequiredMessage=function(e,n){return"fa"===e.locale.language?n?`لطفاً ${n} خود را وارد نمایید`:"لطفاً این قسمت را پر کنید":n?`Please enter your ${n}`:"Please complete this field"},e.i18n=t}));
2
2
  //# sourceMappingURL=index.umd.js.map
Binary file
Binary file
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"qOAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,kCC5BxB,SAAmC,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C"}
1
+ {"version":3,"file":"index.umd.js","names":[],"sources":["../lib/i18n.ts","../lib/dictionary.ts"],"sourcesContent":[],"mappings":"qOAIA,IAAa,EAAb,MAEE,WAAA,GAEE,GACO,QAFM,UAAU,iBAAiB,MAAQ,MAG5C,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,SAAQ,EACR,OAAQ,YAKV,KAAK,OAAS,IAAI,KAAK,OAAO,KAAM,CAClC,SAAU,UACV,OAAQ,MAKf,CACD,SAAA,CAAU,GACR,KAAK,OAAS,CACf,GAGH,MAAa,EAAO,IAAI,iBCpBxB,MAEE,WAAA,CAAY,GADZ,KAAA,WAA+B,CAAE,EAE/B,KAAK,WAAa,CACnB,CAMD,WAAA,CAAY,EAAmB,GAC7B,KAAK,WAAW,GAAe,CAChC,CAQD,GAAA,CAAuB,EAAY,GACjC,MAAM,EAAM,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,GACxG,OAAO,EAAI,GAAK,EAAI,GAAK,KAAK,WAAW,GAAM,EAChD,mCAhCH,SAAmC,EAAe,GAChD,MACO,OADA,EAAQ,OAAO,SAEX,EAAA,SAAe,uBAA2B,4BAG1C,EAAA,qBAA2B,IAAQ,4BAE/C"}
@@ -8,4 +8,29 @@ export function getRequiredMessage(context:JBI18N,label?:string){
8
8
  default:
9
9
  return label?`Please enter your ${label}`:'Please complete this field';
10
10
  }
11
+ }
12
+ export class JBDictionary<T extends object> {
13
+ dictionary: Record<string,T> = {}
14
+ constructor(initialDictionary:Record<string,T>){
15
+ this.dictionary = initialDictionary;
16
+ }
17
+ /**
18
+ * add new or replace existing language with given dictionary
19
+ * @param languageKey language standard key like "fa" or "en"
20
+ * @param dictionary key value object of strings and messages
21
+ */
22
+ setLanguage(languageKey:string,dictionary:T){
23
+ this.dictionary[languageKey] = dictionary;
24
+ }
25
+ //TODO: add support for nested path
26
+ /**
27
+ * get value of given key in dictionary with fallback to english dictionary
28
+ * @param i18n instance of i18n config
29
+ * @param key key of dictionary object
30
+ * @returns value of the dictionary with "en" fallback
31
+ */
32
+ get<K extends keyof T>(i18n:JBI18N,key:K):T[K]{
33
+ const obj = this.dictionary[i18n.locale.language]?this.dictionary[i18n.locale.language]:this.dictionary["en"];
34
+ return obj[key]?obj[key]:this.dictionary["en"][key];
35
+ }
11
36
  }
package/i18n/lib/i18n.ts CHANGED
@@ -10,7 +10,8 @@ export class JBI18N {
10
10
  case "fa":
11
11
  this.locale = new Intl.Locale("fa", {
12
12
  calendar: "persian",
13
- region: "US",
13
+ numeric:false,
14
+ region: "IR",
14
15
  })
15
16
  break;
16
17
  case "en":
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "theme",
16
16
  "event"
17
17
  ],
18
- "version": "0.17.0",
18
+ "version": "0.18.1",
19
19
  "bugs": "https://github.com/javadbat/jb-core/issues",
20
20
  "license": "MIT",
21
21
  "files": [