Comparing version 1.0.1 to 1.1.0
@@ -1,1 +0,1 @@ | ||
import{ref as n,inject as t}from"vue";const e=Symbol("i18n"),o=(n,t)=>{const e=n[0];if(~e.indexOf("[")){const[r,a]=e.split("["),s=parseInt(a.replace("]",""));if(s>-1){if(!t[r]&&t[r].length>0&&t[r][s]&&""!==t[r][s])throw new Error("Not Found");return 1===n.length?"string"==typeof t[r][s]?t[r][s]:"":o(n.slice(1),t[r][s])}throw new Error(`Not Found: ${e}`)}if(t[n[0]]||""===t[n[0]])return 1===n.length?"string"==typeof t[n[0]]?t[n[0]]:"":o(n.slice(1),t[n[0]]);throw new Error("Not Found")},r=t=>{const r=n(t.locale||"en"),a=t.messages;return{messages:a,t:n=>{const t=a[r.value]||a.en;if("string"!=typeof n)return console.warn("Warn(i18n):","keypath must be a type of string"),"";try{return o(n.split("."),t)}catch(t){return console.warn(`Warn(i18n): the keypath '${n}' not found`),""}},setLocale:n=>{a[n]||console.warn(`Warn(i18n): the '${n}' language pack not found, fall back to English language pack`),r.value=n},getLocale:()=>r.value,install(n){const t=this;n.provide(e,t),n.config.globalProperties.$t=t.t,n.config.globalProperties.$i18n=t}}};function a(){return t(e)}export{r as createI18n,e as i18nSymbol,a as useI18n}; | ||
import{ref as e,inject as t}from"vue";const n=Symbol("i18n"),o=(e,t,n)=>{const r=e[0];if(~r.indexOf("[")){const[l,a]=r.split("["),s=parseInt(a.replace("]",""));if(s>-1){if(!t[l]&&t[l].length>0&&t[l][s]&&""!==t[l][s])throw new Error("Not Found");return 1===e.length?"string"==typeof t[l][s]?t[l][s]:"":o(e.slice(1),t[l][s],n)}throw new Error(`Not Found: ${r}`)}if(t[e[0]]||""===t[e[0]]){if(1===e.length){let o="string"==typeof t[e[0]]?t[e[0]]:"";return n&&(o=((e,t)=>{const n=/{(\w*)}/g;let o,r=e;for(;null!==(o=n.exec(e));){if(console.log(o),!Object.prototype.hasOwnProperty.call(t,o[1]))throw new Error(`Not Found Params: ${o[1]}`);r=r.replace(o[0],t[o[1]])}return r})(o,n)),o}return o(e.slice(1),t[e[0]],n)}throw new Error("Not Found")},r=t=>{const r=e(t.locale||"en"),l=t.messages;return{messages:l,t:(e,t)=>{const n=l[r.value]||l.en;if("string"!=typeof e)return console.warn("Warn(i18n):","keypath must be a type of string"),"";try{return o(e.split("."),n,t)}catch(t){return console.log(t),console.warn(`Warn(i18n): the keypath '${e}' not found`),""}},setLocale:e=>{l[e]||console.warn(`Warn(i18n): the '${e}' language pack not found, fall back to English language pack`),r.value=e},getLocale:()=>r.value,install(e){const t=this;e.provide(n,t),e.config.globalProperties.$t=t.t,e.config.globalProperties.$i18n=t}}};function l(){return t(n)}export{r as createI18n,n as i18nSymbol,l as useI18n}; |
{ | ||
"name": "vue3-i18n", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A i18n plugin for Vue3", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.esm.js", |
@@ -13,10 +13,10 @@ # vue3-i18n | ||
## Usage | ||
## install | ||
### install | ||
`npm install vue3-i18n --save` | ||
### Quickstart | ||
## Quickstart | ||
```javascript | ||
// i18n.js | ||
// i18n.js | ||
import { createI18n } from "vue3-i18n"; | ||
@@ -28,2 +28,9 @@ | ||
test: "test", | ||
object: { | ||
a: "1233", | ||
}, | ||
parse: "welcome to {name}", | ||
parses: { | ||
a: "welcome to {name}", | ||
}, | ||
}, | ||
@@ -33,2 +40,9 @@ zh: { | ||
test: "测试", | ||
object: { | ||
a: "1233", | ||
}, | ||
parse: "welcome to {name}", | ||
parses: { | ||
a: "welcome to {name}", | ||
}, | ||
}, | ||
@@ -44,4 +58,4 @@ }; | ||
``` | ||
```javascript | ||
// main.js | ||
@@ -53,8 +67,6 @@ import { createApp } from "vue"; | ||
app | ||
.use(i18n) | ||
.mount("#app"); | ||
app.use(i18n).mount("#app"); | ||
``` | ||
### Usage | ||
## Usage | ||
@@ -64,5 +76,8 @@ ```html | ||
<!-- array --> | ||
<p>{{ $t("test[1]") }}</p> | ||
<p>{{ $t("menu[0]") }}</p> | ||
<!-- object --> | ||
<p>{{ $t("test.a") }}</p> | ||
<p>{{ $t("object.a") }}</p> | ||
<!-- parse --> | ||
<p>{{ $t("parse", { name: "wangsw" }) }}</p> | ||
<p>{{ $t("parses.a", { name: "wangsw" }) }}</p> | ||
``` | ||
@@ -88,7 +103,33 @@ | ||
#### TODO | ||
### API | ||
### i18n.t(key,parmas) | ||
| t() | Type | Description | | ||
| ------ | -------- | --------------- | | ||
| key | `string` | translate text | | ||
| params | `object` | format params | | ||
| return | `string` | translated text | | ||
### i18n.setLocale() | ||
| setLocale() | Type | Description | | ||
| ----------- | -------- | ----------- | | ||
| params | `string` | set locale | | ||
| return | `void` | | | ||
### i18n.getLocale() | ||
| getLocale() | Type | Description | | ||
| ----------- | -------- | -------------- | | ||
| params | `void` | | | ||
| return | `string` | current locale | | ||
## TODO | ||
- [ ] directive | ||
- [ ] Named formatting | ||
- [x] Named formatting | ||
## License | ||
MIT | ||
MIT |
@@ -6,3 +6,18 @@ import { ref, App } from "vue"; | ||
const recursiveRetrieve = (chain: string[], messages: Messages): string => { | ||
const parseAndReplaceString = (str: string, params: any): string => { | ||
const reg = /{(\w*)}/g; | ||
let arr; | ||
let st: string = str; | ||
while ((arr = reg.exec(str)) !== null) { | ||
console.log(arr); | ||
if (Object.prototype.hasOwnProperty.call(params, arr[1])) { | ||
st = st.replace(arr[0], params[arr[1]]); | ||
} else { | ||
throw new Error(`Not Found Params: ${arr[1]}`); | ||
} | ||
} | ||
return st; | ||
}; | ||
const recursiveRetrieve = (chain: string[], messages: Messages, params?: any): string => { | ||
const key = chain[0]; | ||
@@ -15,15 +30,8 @@ if (~key.indexOf("[")) { | ||
if (num > -1) { | ||
if ( | ||
!messages[okey] && | ||
messages[okey].length > 0 && | ||
messages[okey][num] && | ||
messages[okey][num] !== "" | ||
) { | ||
if (!messages[okey] && messages[okey].length > 0 && messages[okey][num] && messages[okey][num] !== "") { | ||
throw new Error("Not Found"); | ||
} else if (chain.length === 1) { | ||
return typeof messages[okey][num] === "string" | ||
? messages[okey][num] | ||
: ""; | ||
return typeof messages[okey][num] === "string" ? messages[okey][num] : ""; | ||
} else { | ||
return recursiveRetrieve(chain.slice(1), messages[okey][num]); | ||
return recursiveRetrieve(chain.slice(1), messages[okey][num], params); | ||
} | ||
@@ -37,5 +45,9 @@ } else { | ||
} else if (chain.length === 1) { | ||
return typeof messages[chain[0]] === "string" ? messages[chain[0]] : ""; | ||
let string: string = typeof messages[chain[0]] === "string" ? messages[chain[0]] : ""; | ||
if (params!) { | ||
string = parseAndReplaceString(string, params); | ||
} | ||
return string; | ||
} else { | ||
return recursiveRetrieve(chain.slice(1), messages[chain[0]]); | ||
return recursiveRetrieve(chain.slice(1), messages[chain[0]], params); | ||
} | ||
@@ -48,3 +60,3 @@ } | ||
const messages = config.messages; | ||
const t = (key: string) => { | ||
const t = (key: string, params?: any): string => { | ||
const pack = messages[locale.value] || messages.en; | ||
@@ -56,4 +68,5 @@ if (typeof key !== "string") { | ||
try { | ||
return recursiveRetrieve(key.split("."), pack); | ||
return recursiveRetrieve(key.split("."), pack, params); | ||
} catch (error) { | ||
console.log(error); | ||
console.warn(`Warn(i18n): the keypath '${key}' not found`); | ||
@@ -65,5 +78,3 @@ return ""; | ||
if (!messages[loc]) { | ||
console.warn( | ||
`Warn(i18n): the '${loc}' language pack not found, fall back to English language pack` | ||
); | ||
console.warn(`Warn(i18n): the '${loc}' language pack not found, fall back to English language pack`); | ||
} | ||
@@ -70,0 +81,0 @@ locale.value = loc; |
@@ -12,2 +12,4 @@ import { App } from "vue"; | ||
export interface Params extends Object {} | ||
export interface I18nInstance { | ||
@@ -14,0 +16,0 @@ messages: Messages; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129
10495
8
117