jb-core 0.16.1 → 0.18.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/README.md CHANGED
@@ -7,6 +7,10 @@ core modules of jb-design system mostly contain functions that help you manage y
7
7
 
8
8
  to see react modules please see [`jb-core/react`](https://github.com/javadbat/jb-core/tree/main/react)
9
9
 
10
+ ## I18N modules
11
+
12
+ to see i18n modules please see [`jb-core/i18n`](https://github.com/javadbat/jb-core/tree/main/i18n)
13
+
10
14
  ## listenAndSilentEvent
11
15
 
12
16
  this function listen to event in the capture phase and stop it's propagation and call your handler so you will be the only one who capture this event used for event forwarding (transformation) in web-components.
package/i18n/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # JB I18N Module
2
+
3
+ ## config file
4
+ JB Design System use html tag `lang` attribute to set the default language of it's components.
5
+
6
+ ```html
7
+ <html lang="fa">
8
+ <!-- or -->
9
+ <html lang="en">
10
+ ```
11
+
12
+ if you want to set your locale manually in javascript you just have to import `i18n` and set your default locale:
13
+
14
+ ```ts
15
+ import {i18n} from 'jb-core/i18n';
16
+ // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale for more setting detail
17
+ i18n.setLocale(new Intl.Locale("fa"))
18
+ ```
19
+
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
53
+
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
55
+
56
+ ### getRequiredMessage
57
+ used to get error message of required filed base on their label
58
+
59
+ ```ts
60
+ getRequiredMessage(context:JBI18N,label?:string)
61
+ //example
62
+ import {i18n,getRequiredMessage} from 'jb-core/i18n';
63
+ getRequiredMessage(i18n,"your label");
64
+ ```
@@ -0,0 +1,20 @@
1
+ import type { JBI18N } from "./i18n";
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(i18n: JBI18N, key: keyof T): T[keyof T];
19
+ }
20
+ //# sourceMappingURL=dictionary.d.ts.map
@@ -0,0 +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;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,IAAI,EAAC,MAAM,EAAC,GAAG,EAAC,MAAM,CAAC;CAI5B"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ this is the core module of i18n in jb design system its is just a main store for keeping data and change config.
3
+ every helper method will implement as an independent functions and class, so modules only use instance of an this class as a input to each used function
4
+ */
5
+ export declare class JBI18N {
6
+ locale: Intl.Locale;
7
+ constructor();
8
+ setLocale(locale: Intl.Locale): void;
9
+ }
10
+ export declare const i18n: JBI18N;
11
+ //# sourceMappingURL=i18n.d.ts.map
@@ -0,0 +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;;IAqBnB,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM;CAG9B;AAED,eAAO,MAAM,IAAI,QAAe,CAAC"}
@@ -0,0 +1,2 @@
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
+ //# sourceMappingURL=index.cjs.js.map
Binary file
Binary file
@@ -0,0 +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,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,CAAI,EAAY,GACd,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"}
@@ -0,0 +1,3 @@
1
+ export * from "./i18n.js";
2
+ export * from './dictionary.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA"}
@@ -0,0 +1,2 @@
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
+ //# sourceMappingURL=index.js.map
Binary file
Binary file
@@ -0,0 +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,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,CAAI,EAAY,GACd,MAAM,EAAM,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,EAAK,OAAO,UAAU,KAAK,WAAW,GACxG,OAAO,EAAI,GAAK,EAAI,GAAK,KAAK,WAAW,GAAM,EAChD"}
@@ -0,0 +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",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
+ //# sourceMappingURL=index.umd.js.map
Binary file
Binary file
@@ -0,0 +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,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,CAAI,EAAY,GACd,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"}
@@ -0,0 +1,36 @@
1
+ import type { JBI18N } from "./i18n";
2
+
3
+ export function getRequiredMessage(context:JBI18N,label?:string){
4
+ switch(context.locale.language){
5
+ case 'fa':
6
+ return label?`لطفاً ${label} خود را وارد نمایید`:'لطفاً این قسمت را پر کنید';
7
+ case 'en':
8
+ default:
9
+ return label?`Please enter your ${label}`:'Please complete this field';
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(i18n:JBI18N,key:keyof T){
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
+ }
36
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ this is the core module of i18n in jb design system its is just a main store for keeping data and change config.
3
+ every helper method will implement as an independent functions and class, so modules only use instance of an this class as a input to each used function
4
+ */
5
+ export class JBI18N {
6
+ locale: Intl.Locale
7
+ constructor() {
8
+ const lang = document?.documentElement?.lang || "en"
9
+ switch (lang) {
10
+ case "fa":
11
+ this.locale = new Intl.Locale("fa", {
12
+ calendar: "persian",
13
+ numeric:false,
14
+ region: "IR",
15
+ })
16
+ break;
17
+ case "en":
18
+ default:
19
+ this.locale = new Intl.Locale("en", {
20
+ calendar: "gregory",
21
+ region: "US",
22
+ });
23
+ break;
24
+
25
+ }
26
+ }
27
+ setLocale(locale: Intl.Locale) {
28
+ this.locale = locale;
29
+ }
30
+ }
31
+
32
+ export const i18n = new JBI18N();
@@ -0,0 +1,2 @@
1
+ export * from "./i18n.js"
2
+ export * from './dictionary.js'
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "jb-core-i18n",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts"
6
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "rootDir": "./lib",
5
+ "declarationDir": "./dist",
6
+ },
7
+ "include": [
8
+ "lib/**/*.ts",
9
+ ],
10
+ "exclude": [
11
+ "stories",
12
+ "node_modules",
13
+ "**/*.spec.ts",
14
+ "dist",
15
+ ],
16
+ "extends":"../configs/tsconfig-web-component.json"
17
+ }
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "theme",
16
16
  "event"
17
17
  ],
18
- "version": "0.16.1",
18
+ "version": "0.18.0",
19
19
  "bugs": "https://github.com/javadbat/jb-core/issues",
20
20
  "license": "MIT",
21
21
  "files": [
@@ -26,6 +26,7 @@
26
26
  "styles/",
27
27
  "configs/",
28
28
  "theme/",
29
+ "i18n/",
29
30
  "react/",
30
31
  "react/dist/"
31
32
  ],