Skip to content

Commit

Permalink
chore: 优化example
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jan 24, 2024
1 parent 0de088f commit e857933
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 33 deletions.
5 changes: 4 additions & 1 deletion examples/vue2-js/src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export default {
"2": "VoerkaI18n is a great multilingual solution",
"3": "Home",
"4": "About",
"5": "Welcome to VoerkaI18n"
"5": "Welcome to VoerkaI18n",
"6": "Link",
"7": "ECSystem",
"8": "hello get"
}
5 changes: 4 additions & 1 deletion examples/vue2-js/src/languages/idMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export default {
"VoerkaI18n是一个非常不错的多语言解决方案": 2,
"首页": 3,
"关于": 4,
"欢迎使用VoerkaI18n": 5
"欢迎使用VoerkaI18n": 5,
"基本链接": 6,
"生态系统": 7,
"hello get": 8
}
4 changes: 3 additions & 1 deletion examples/vue2-js/src/languages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import defaultFormatters from "./formatters/zh.js" // 默认语言
import defaultMessages from "./zh.js"
import storage from "./storage.js"



const messages = {
'zh' : defaultMessages,
'en' : ()=>import("./en.js")
Expand Down Expand Up @@ -36,7 +38,7 @@ const scopeSettings = {

// 语言作用域
const scope = new VoerkaI18nScope({
id : "vue2-js", // 当前作用域的id,自动取当前工程的package.json的name
id : "voerkai18n-example-vue2-js", // 当前作用域的id,自动取当前工程的package.json的name
debug : false, // 是否在控制台输出调试信息
idMap, // 消息id映射列表
library : false, // 开发库时设为true
Expand Down
18 changes: 18 additions & 0 deletions examples/vue2-js/src/languages/translates/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@
"$files": [
"components\\HelloWorld.vue"
]
},
"基本链接": {
"en": "基本链接",
"$files": [
"components\\HelloWorld.vue"
]
},
"生态系统": {
"en": "生态系统",
"$files": [
"components\\HelloWorld.vue"
]
},
"hello get": {
"en": "hello get",
"$files": [
"components\\HelloWorld.vue"
]
}
}
5 changes: 4 additions & 1 deletion examples/vue2-js/src/languages/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export default {
"2": "VoerkaI18n是一个非常不错的多语言解决方案",
"3": "首页",
"4": "关于",
"5": "欢迎使用VoerkaI18n"
"5": "欢迎使用VoerkaI18n",
"6": "基本链接",
"7": "生态系统",
"8": "hello get"
}
13 changes: 7 additions & 6 deletions examples/vue2-js/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Vue.config.productionTip = false

console.log(t("hello"))
Vue.use(i18nPlugin,{i18nScope})

new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
i18nScope.on('ready',()=>{
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
})
3 changes: 2 additions & 1 deletion examples/vue2-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"serve": "set NODE_OPTIONS=\"--openssl-legacy-provider\" && vue-cli-service serve",
"build": "vue-cli-service build",
"translate": "node ../../packages/cli/index.js translate --",
"init:i18n": "node ../../packages/cli/index.js init",
Expand All @@ -22,6 +22,7 @@
"@vue/cli-plugin-typescript": "~4.4.6",
"@vue/cli-plugin-vuex": "~4.4.6",
"@vue/cli-service": "~4.4.6",
"tslib": "^2.6.2",
"typescript": "~3.9.3",
"vue-template-compiler": "^2.6.11"
}
Expand Down
4 changes: 4 additions & 0 deletions examples/vue2-ts/src/languages/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
"1": "首页",
"2": "关于"
}
94 changes: 94 additions & 0 deletions examples/vue2-ts/src/languages/formatters/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
格式化器用来对翻译文本内容中的插值变量进行处理
如何编写格式器请参阅官网!
*/


export default {
// global : true, // 简单地设置为true,代表当前所有格式化器均注册到全局,false只在当前scope生效
// global : { // 仅将里面的格式化器注册到全局
// $config:{... }
// xxxx : value => { ... },
// xxxx : (value,$config) => { ... },
// xxxx : (value,...args,$config) => { ... },
// xxxx : Formatter(value,...args,$config) => { ... },
// xxxx : FlexFormatter(value,params,$config) => { ... },
//}, // 是否注册到全局,false只在当前scope生效
// 直接对内置格式化器进行配置,请参阅官网文档
// $config:{
// datetime : {
// units : ["Year","Quarter","Month","Week","Day","Hour","Minute","Second","Millisecond","Microsecond"],
// date :{
// long : 'YYYY/MM/DD HH:mm:ss',
// short : "YYYY/MM/DD",
// format : "local"
// },
// quarter : {
// long : ["First Quarter","Second Quarter","Third Quarter","Fourth Quarter"],
// short : ["Q1","Q2","Q3","Q4"],
// format : "short"
// },
// month:{
// long : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
// short : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"],
// format : "long" // 0-长名称,1-短名称,2-数字
// },
// weekday:{
// long : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
// short : ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"],
// format : "long", // 0-长名称,1-短名称,2-数字
// },
// time : {
// long : "HH:mm:ss",
// short : "HH:mm:ss",
// format : 'local'
// },
// timeSlots : {
// slots : [12],
// lowerCases : ["am","pm"],
// upperCases : ["AM","PM"]
// },
// relativeTime : {
// units : ["seconds","minutes","hours","days","weeks","months","years"],
// now : "Now",
// before : "{value} {unit} ago",
// after : "after {value} {unit}"
// }
// },
// currency : {
// default : "{symbol}{value}{unit}",
// long : "{prefix} {symbol}{value}{unit}{suffix}",
// short : "{symbol}{value}{unit}",
// custom : "{prefix} {symbol}{value}{unit}{suffix}",
// format : "default",
// //--
// units : [""," thousands"," millions"," billions"," trillions"], //千,百万,十亿,万亿
// radix : 3, // 进制,即三位一进,中文是4位一进
// symbol : "$", // 符号
// prefix : "USD", // 前缀
// suffix : "", // 后缀
// division : 3, // ,分割位
// precision : 2, // 精度

// },
// number : {
// division : 3, // , 分割位,3代表每3位添加一个,
// precision : 0 // 精度,即保留小数点位置,0代表不限
// }
// },
// 改变特定数据类型的默认格式化器
// $types:{
// Null : value =>"",
// Undefined: value =>"",
// Boolean : value =>value ? "True":"False",
// }
// 以下可以自定义编写格式化器
// xxxx : value => { ... },
// xxxx : (value,$config) => { ... },
// xxxx : (value,...args,$config) => { ... },
// xxxx : Formatter(value,...args,$config) => { ... },
// xxxx : FlexFormatter(value,params,$config) => { ... },
}
94 changes: 94 additions & 0 deletions examples/vue2-ts/src/languages/formatters/zh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
格式化器用来对翻译文本内容中的插值变量进行处理
如何编写格式器请参阅官网!
*/


export default {
// global : true, // 简单地设置为true,代表当前所有格式化器均注册到全局,false只在当前scope生效
// global : { // 仅将里面的格式化器注册到全局
// $config:{... }
// xxxx : value => { ... },
// xxxx : (value,$config) => { ... },
// xxxx : (value,...args,$config) => { ... },
// xxxx : Formatter(value,...args,$config) => { ... },
// xxxx : FlexFormatter(value,params,$config) => { ... },
//}, // 是否注册到全局,false只在当前scope生效
// 直接对内置格式化器进行配置,请参阅官网文档
// $config:{
// datetime : {
// units : ["Year","Quarter","Month","Week","Day","Hour","Minute","Second","Millisecond","Microsecond"],
// date :{
// long : 'YYYY/MM/DD HH:mm:ss',
// short : "YYYY/MM/DD",
// format : "local"
// },
// quarter : {
// long : ["First Quarter","Second Quarter","Third Quarter","Fourth Quarter"],
// short : ["Q1","Q2","Q3","Q4"],
// format : "short"
// },
// month:{
// long : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
// short : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"],
// format : "long" // 0-长名称,1-短名称,2-数字
// },
// weekday:{
// long : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
// short : ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"],
// format : "long", // 0-长名称,1-短名称,2-数字
// },
// time : {
// long : "HH:mm:ss",
// short : "HH:mm:ss",
// format : 'local'
// },
// timeSlots : {
// slots : [12],
// lowerCases : ["am","pm"],
// upperCases : ["AM","PM"]
// },
// relativeTime : {
// units : ["seconds","minutes","hours","days","weeks","months","years"],
// now : "Now",
// before : "{value} {unit} ago",
// after : "after {value} {unit}"
// }
// },
// currency : {
// default : "{symbol}{value}{unit}",
// long : "{prefix} {symbol}{value}{unit}{suffix}",
// short : "{symbol}{value}{unit}",
// custom : "{prefix} {symbol}{value}{unit}{suffix}",
// format : "default",
// //--
// units : [""," thousands"," millions"," billions"," trillions"], //千,百万,十亿,万亿
// radix : 3, // 进制,即三位一进,中文是4位一进
// symbol : "$", // 符号
// prefix : "USD", // 前缀
// suffix : "", // 后缀
// division : 3, // ,分割位
// precision : 2, // 精度

// },
// number : {
// division : 3, // , 分割位,3代表每3位添加一个,
// precision : 0 // 精度,即保留小数点位置,0代表不限
// }
// },
// 改变特定数据类型的默认格式化器
// $types:{
// Null : value =>"",
// Undefined: value =>"",
// Boolean : value =>value ? "True":"False",
// }
// 以下可以自定义编写格式化器
// xxxx : value => { ... },
// xxxx : (value,$config) => { ... },
// xxxx : (value,...args,$config) => { ... },
// xxxx : Formatter(value,...args,$config) => { ... },
// xxxx : FlexFormatter(value,params,$config) => { ... },
}
4 changes: 4 additions & 0 deletions examples/vue2-ts/src/languages/idMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
"首页": 1,
"关于": 2
}
55 changes: 55 additions & 0 deletions examples/vue2-ts/src/languages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 注意:执行compile命令会重新生成本文件,所以请不要修改本文件
*/
import idMap from "./idMap" // 语言ID映射文件
import { translate,VoerkaI18nScope } from "@voerkai18n/runtime"
import defaultFormatters from "./formatters/zh"
import defaultMessages from "./zh"
import storage from "./storage"



const messages = {
'zh' : defaultMessages,
'en' : ()=>import("./en")
}

const formatters = {
'zh' : defaultFormatters,
'en' : ()=>import("./formatters/en")
}

// 语言配置文件
const scopeSettings = {
"languages": [
{
"name": "zh",
"title": "zh"
},
{
"name": "en",
"title": "en"
}
],
"defaultLanguage": "zh",
"activeLanguage": "zh",
"namespaces": {}
}

// 语言作用域
const scope = new VoerkaI18nScope({
id : "voerkai18n-example-vue2-ts", // 当前作用域的id,自动取当前工程的package.json的name
debug : false, // 是否在控制台输出调试信息
idMap, // 消息id映射列表
library : false, // 开发库时设为true
messages, // 语言包
formatters, // 扩展自定义格式化器
storage, // 语言配置存储器
...scopeSettings
})
// 翻译函数
const scopedTtranslate = translate.bind(scope)
export {
scopedTtranslate as t,
scope as i18nScope
}
17 changes: 17 additions & 0 deletions examples/vue2-ts/src/languages/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
get(key:string){
if(globalThis.localStorage){
return globalThis.localStorage.getItem(key)
}
},
set(key:string,value:any){
if(globalThis.localStorage){
globalThis.localStorage.setItem(key,value)
}
},
remove(key:string){
if(globalThis.localStorage){
globalThis.localStorage.removeItem(key)
}
}
}
4 changes: 4 additions & 0 deletions examples/vue2-ts/src/languages/zh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
"1": "首页",
"2": "关于"
}
Loading

0 comments on commit e857933

Please sign in to comment.