van-i18n 0.0.0 → 0.1.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
@@ -1,2 +1,55 @@
1
1
  # van-i18n
2
2
  i18n provider for vanjs app
3
+
4
+ ## API Design
5
+
6
+ - `createI18NProvider(lang, loclas)` : initialize once
7
+ - `useTranslation()` : use anywhere, returns :
8
+ - `t(key)` : reactive translation
9
+ - `setLang(lang, rtl = false)` : change language globally
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import van from "vanjs-core";
15
+ import { createI18NProvider, useTranslation } from "van-i18n";
16
+
17
+ const {div, p, button, br} = van.tags
18
+
19
+ const locals = {
20
+ fr: {
21
+ start: "Commencer",
22
+ end: "Terminer",
23
+ },
24
+ en: {
25
+ start: "Start",
26
+ end: "Finish",
27
+ },
28
+ };
29
+
30
+ createI18NProvider("en", locals);
31
+
32
+ const App = () => {
33
+ const [t, setLang] = useTranslation();
34
+
35
+ return div(
36
+ p(t("start")),
37
+ br(),
38
+ button({ onclick: () => setLang("en") }, "EN"),
39
+ button({ onclick: () => setLang("fr") }, "FR"),
40
+ );
41
+ };
42
+
43
+ document.body.appendChild(App());
44
+
45
+ ```
46
+
47
+ ## Features
48
+
49
+ - Reactivity
50
+ - Simple Architecture
51
+ - No nesting
52
+ - No wrapping components
53
+ - Automatically updates `<html lang=""..`
54
+ - Handle directions
55
+ - Lightweight
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "van-i18n",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
4
  "description": "i18n provider for vanjs",
5
5
  "keywords": [
6
6
  "i18n",
7
7
  "provider",
8
8
  "vanjs"
9
9
  ],
10
- "exports":{
11
- "." : {
12
- "import" : "./src/index.js"
10
+ "exports": {
11
+ ".": {
12
+ "import": "./src/index.js"
13
13
  }
14
14
  },
15
15
  "homepage": "https://github.com/zakarialaoui10/van-i18n#readme",
@@ -26,5 +26,8 @@
26
26
  "main": "index.js",
27
27
  "scripts": {
28
28
  "test": "echo \"Error: no test specified\" && exit 1"
29
+ },
30
+ "dependencies": {
31
+ "vanjs-core": "^1.6.0"
29
32
  }
30
33
  }
@@ -0,0 +1 @@
1
+ export * from './use-translation.js'
@@ -0,0 +1,17 @@
1
+ import van from "vanjs-core";
2
+
3
+ export function useTranslation() {
4
+
5
+ const i18n = globalThis.__van__i18n__store__
6
+ const t = (key) =>
7
+ van.derive(() =>
8
+ i18n.locals?.[i18n.lang.val]?.[key] ?? key
9
+ );
10
+
11
+ const setLang = (lang) => {
12
+ i18n.lang.val = lang;
13
+ document.documentElement.lang = lang;
14
+ };
15
+
16
+ return [t, setLang];
17
+ }
package/src/provider.js CHANGED
@@ -0,0 +1,9 @@
1
+ import van from "vanjs-core";
2
+
3
+ export function createI18NProvider(lang, locals) {
4
+ globalThis.__van__i18n__store__ = {
5
+ lang: van.state(lang),
6
+ locals: locals,
7
+ };
8
+ document.documentElement.lang = lang;
9
+ }