@varlet/cli 1.27.10 → 1.27.11-alpha.1653999804324
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/generators/base/README.md +6 -0
- package/generators/{base → config/default/base}/docs/home.zh-CN.md +0 -0
- package/generators/config/default/base/package.json +98 -0
- package/generators/{base → config/default/base}/types/basicComponent.d.ts +0 -0
- package/generators/{base → config/default/base}/types/button.d.ts +0 -0
- package/generators/{base → config/default/base}/types/index.d.ts +0 -0
- package/generators/{base → config/default/base}/varlet.config.js +1 -0
- package/generators/{sfc → config/default/sfc}/src/button/Button.vue +0 -2
- package/generators/config/default/sfc/src/button/__tests__/index.spec.js +7 -0
- package/generators/{sfc → config/default/sfc}/src/button/button.less +0 -0
- package/generators/{sfc → config/default/sfc}/src/button/docs/zh-CN.md +0 -0
- package/generators/config/default/sfc/src/button/example/BasicUse.vue +7 -0
- package/generators/config/default/sfc/src/button/example/ModifyColor.vue +7 -0
- package/generators/{tsx → config/default/sfc}/src/button/example/index.vue +6 -16
- package/generators/{sfc → config/default/sfc}/src/button/index.ts +0 -0
- package/generators/{tsx → config/default/tsx}/src/button/Button.tsx +0 -0
- package/generators/config/default/tsx/src/button/__tests__/index.spec.js +7 -0
- package/generators/{tsx → config/default/tsx}/src/button/button.less +0 -0
- package/generators/{tsx → config/default/tsx}/src/button/docs/zh-CN.md +0 -0
- package/generators/config/default/tsx/src/button/example/BasicUse.vue +7 -0
- package/generators/config/default/tsx/src/button/example/ModifyColor.vue +7 -0
- package/generators/{sfc → config/default/tsx}/src/button/example/index.vue +6 -16
- package/generators/{tsx → config/default/tsx}/src/button/index.ts +0 -0
- package/generators/config/i18n/base/docs/home.en-US.md +11 -0
- package/generators/config/i18n/base/docs/home.zh-CN.md +11 -0
- package/generators/{base → config/i18n/base}/package.json +3 -2
- package/generators/config/i18n/base/types/basicComponent.d.ts +7 -0
- package/generators/config/i18n/base/types/button.d.ts +12 -0
- package/generators/config/i18n/base/types/index.d.ts +7 -0
- package/generators/config/i18n/base/types/locale.d.ts +24 -0
- package/generators/config/i18n/base/varlet.config.js +122 -0
- package/generators/config/i18n/sfc/src/button/Button.vue +36 -0
- package/generators/config/i18n/sfc/src/button/__tests__/index.spec.js +7 -0
- package/generators/config/i18n/sfc/src/button/button.less +14 -0
- package/generators/config/i18n/sfc/src/button/docs/en-US.md +35 -0
- package/generators/config/i18n/sfc/src/button/docs/zh-CN.md +35 -0
- package/generators/config/i18n/sfc/src/button/example/BasicUse.vue +11 -0
- package/generators/config/i18n/sfc/src/button/example/ModifyColor.vue +11 -0
- package/generators/config/i18n/sfc/src/button/example/index.vue +13 -0
- package/generators/config/i18n/sfc/src/button/example/locale/en-US.ts +5 -0
- package/generators/config/i18n/sfc/src/button/example/locale/index.ts +23 -0
- package/generators/config/i18n/sfc/src/button/example/locale/zh-CN.ts +5 -0
- package/generators/config/i18n/sfc/src/button/index.ts +10 -0
- package/generators/config/i18n/sfc/src/locale/__tests__/index.spec.js +51 -0
- package/generators/config/i18n/sfc/src/locale/docs/en-US.md +26 -0
- package/generators/config/i18n/sfc/src/locale/docs/zh-CN.md +25 -0
- package/generators/config/i18n/sfc/src/locale/en-US.d.ts +5 -0
- package/generators/config/i18n/sfc/src/locale/en-US.ts +7 -0
- package/generators/config/i18n/sfc/src/locale/index.ts +67 -0
- package/generators/config/i18n/sfc/src/locale/zh-CN.d.ts +5 -0
- package/generators/config/i18n/sfc/src/locale/zh-CN.ts +7 -0
- package/generators/config/i18n/tsx/src/button/Button.tsx +35 -0
- package/generators/config/i18n/tsx/src/button/__tests__/index.spec.js +7 -0
- package/generators/config/i18n/tsx/src/button/button.less +14 -0
- package/generators/config/i18n/tsx/src/button/docs/en-US.md +35 -0
- package/generators/config/i18n/tsx/src/button/docs/zh-CN.md +35 -0
- package/generators/config/i18n/tsx/src/button/example/BasicUse.vue +11 -0
- package/generators/config/i18n/tsx/src/button/example/ModifyColor.vue +11 -0
- package/generators/config/i18n/tsx/src/button/example/index.vue +13 -0
- package/generators/config/i18n/tsx/src/button/example/locale/en-US.ts +5 -0
- package/generators/config/i18n/tsx/src/button/example/locale/index.ts +23 -0
- package/generators/config/i18n/tsx/src/button/example/locale/zh-CN.ts +5 -0
- package/generators/config/i18n/tsx/src/button/index.ts +10 -0
- package/generators/config/i18n/tsx/src/locale/__tests__/index.spec.js +51 -0
- package/generators/config/i18n/tsx/src/locale/docs/en-US.md +26 -0
- package/generators/config/i18n/tsx/src/locale/docs/zh-CN.md +25 -0
- package/generators/config/i18n/tsx/src/locale/en-US.d.ts +5 -0
- package/generators/config/i18n/tsx/src/locale/en-US.ts +7 -0
- package/generators/config/i18n/tsx/src/locale/index.ts +67 -0
- package/generators/config/i18n/tsx/src/locale/zh-CN.d.ts +5 -0
- package/generators/config/i18n/tsx/src/locale/zh-CN.ts +7 -0
- package/lib/commands/create.d.ts +3 -1
- package/lib/commands/create.js +43 -11
- package/lib/commands/gen.js +13 -8
- package/lib/index.js +5 -1
- package/package.json +6 -7
- package/site/pc/components/AppHeader.vue +3 -5
- package/generators/sfc/src/button/__tests__/index.spec.js +0 -8
- package/generators/sfc/src/button/example/BasicUse.vue +0 -14
- package/generators/sfc/src/button/example/ModifyColor.vue +0 -14
- package/generators/tsx/src/button/__tests__/index.spec.js +0 -8
- package/generators/tsx/src/button/example/BasicUse.vue +0 -14
- package/generators/tsx/src/button/example/ModifyColor.vue +0 -14
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// lib
|
|
2
|
+
import _zhCN from '../../../locale/zh-CN'
|
|
3
|
+
import _enCN from '../../../locale/en-US'
|
|
4
|
+
// mobile example doc
|
|
5
|
+
import zhCN from './zh-CN'
|
|
6
|
+
import enUS from './en-US'
|
|
7
|
+
import { useLocale, add as _add, use as _use } from '../../../locale'
|
|
8
|
+
|
|
9
|
+
const { add, use: exampleUse, pack, packs, merge } = useLocale()
|
|
10
|
+
|
|
11
|
+
const use = (lang: string) => {
|
|
12
|
+
_use(lang)
|
|
13
|
+
exampleUse(lang)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { add, pack, packs, merge, use }
|
|
17
|
+
|
|
18
|
+
// lib
|
|
19
|
+
_add('zh-CN', _zhCN)
|
|
20
|
+
_add('en-US', _enCN)
|
|
21
|
+
// mobile example doc
|
|
22
|
+
add('zh-CN', zhCN as any)
|
|
23
|
+
add('en-US', enUS as any)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import enUS from '../en-US'
|
|
2
|
+
import { useLocale } from '../index'
|
|
3
|
+
|
|
4
|
+
function mockConsole(method, fn = () => {}) {
|
|
5
|
+
const originMethod = console[method]
|
|
6
|
+
|
|
7
|
+
console[method] = fn
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
mockRestore() {
|
|
11
|
+
console[method] = originMethod
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test('test add lang', () => {
|
|
17
|
+
const { add, use, pack, packs } = useLocale()
|
|
18
|
+
|
|
19
|
+
const LANG = 'custom'
|
|
20
|
+
const PACK = {
|
|
21
|
+
button: 'Custom Text',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
add(LANG, PACK)
|
|
25
|
+
use(LANG)
|
|
26
|
+
|
|
27
|
+
expect(pack.value.lang).toBe(LANG)
|
|
28
|
+
expect(packs[LANG]).toBe(PACK)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('test merge lang', () => {
|
|
32
|
+
const fn = jest.fn()
|
|
33
|
+
const { mockRestore } = mockConsole('warn', fn)
|
|
34
|
+
const { add, use, merge, pack } = useLocale()
|
|
35
|
+
|
|
36
|
+
const LANG = 'en-US'
|
|
37
|
+
const PACK = {
|
|
38
|
+
button: 'Custom Text',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
add(LANG, enUS)
|
|
42
|
+
use('zh_HK')
|
|
43
|
+
use(LANG)
|
|
44
|
+
merge('zh_HK', PACK)
|
|
45
|
+
merge(LANG, PACK)
|
|
46
|
+
|
|
47
|
+
expect(pack.value.button).toBe('Custom Text')
|
|
48
|
+
expect(fn).toHaveBeenCalledTimes(2)
|
|
49
|
+
|
|
50
|
+
mockRestore()
|
|
51
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Locale
|
|
2
|
+
|
|
3
|
+
### Intro
|
|
4
|
+
Component library uses Chinese as the default language, support multi-language switch,
|
|
5
|
+
built-in support for `Chinese`, `English`.
|
|
6
|
+
|
|
7
|
+
### Multi-language switch
|
|
8
|
+
The `Locale` component is introduced to realize multi-language switching, and `Locale.add` is used for language extension.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
Locale.add('en-US', enUS)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Use `Locale.use` to switch languages.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
Locale.use('en-US')
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Use `Locale.merge` to merge languages.
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
Locale.merge('en-US', {
|
|
24
|
+
button: 'Hello'
|
|
25
|
+
})
|
|
26
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 国际化
|
|
2
|
+
|
|
3
|
+
### 介绍
|
|
4
|
+
组件库使用中文作为默认语言,支持多语言切换,内置支持 `中文`,`英文`。
|
|
5
|
+
|
|
6
|
+
### 多语言切换
|
|
7
|
+
引入 `Locale` 组件实现多语言切换,使用 `Locale.add` 进行语言扩展。
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
Locale.add('en-US', enUS)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
使用 `Locale.use` 进行切换语言
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
Locale.use('en-US')
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
使用 `Locale.merge` 进行语言合并
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
Locale.merge('en-US', {
|
|
23
|
+
button: 'Hello'
|
|
24
|
+
})
|
|
25
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ref } from 'vue'
|
|
2
|
+
import type { Ref } from 'vue'
|
|
3
|
+
import zhCN from './zh-CN'
|
|
4
|
+
|
|
5
|
+
export type Pack = {
|
|
6
|
+
// Button component
|
|
7
|
+
button: string
|
|
8
|
+
|
|
9
|
+
// internal
|
|
10
|
+
lang?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function useLocale<T = Pack>() {
|
|
14
|
+
const packs: Record<string, Partial<T>> = {}
|
|
15
|
+
const pack: Ref<Partial<T>> = ref({})
|
|
16
|
+
|
|
17
|
+
const add = (lang: string, pack: Partial<T> & { lang?: string }) => {
|
|
18
|
+
pack.lang = lang
|
|
19
|
+
packs[lang] = pack
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const use = (lang: string) => {
|
|
23
|
+
if (!packs[lang]) {
|
|
24
|
+
console.warn(`The ${lang} does not exist. You can mount a language package using the add method`)
|
|
25
|
+
return {}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
pack.value = packs[lang]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const merge = (lang: string, pack: Partial<T>) => {
|
|
32
|
+
if (!packs[lang]) {
|
|
33
|
+
console.warn(`The ${lang} does not exist. You can mount a language package using the add method`)
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
packs[lang] = { ...packs[lang], ...pack }
|
|
38
|
+
|
|
39
|
+
use(lang)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
packs,
|
|
44
|
+
pack,
|
|
45
|
+
add,
|
|
46
|
+
use,
|
|
47
|
+
merge,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { packs, pack, add, use, merge } = useLocale()
|
|
52
|
+
|
|
53
|
+
add('zh-CN', zhCN)
|
|
54
|
+
use('zh-CN')
|
|
55
|
+
|
|
56
|
+
export { packs, pack, add, use, merge, useLocale }
|
|
57
|
+
|
|
58
|
+
export const _LocaleComponent = { packs, pack, add, use, merge, useLocale }
|
|
59
|
+
|
|
60
|
+
export default {
|
|
61
|
+
packs,
|
|
62
|
+
pack,
|
|
63
|
+
add,
|
|
64
|
+
use,
|
|
65
|
+
merge,
|
|
66
|
+
useLocale,
|
|
67
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineComponent, PropType } from 'vue'
|
|
2
|
+
import './button.less'
|
|
3
|
+
|
|
4
|
+
// i18n for component's internal
|
|
5
|
+
// import { pack } from '../locale'
|
|
6
|
+
|
|
7
|
+
export default defineComponent({
|
|
8
|
+
name: 'BButton',
|
|
9
|
+
props: {
|
|
10
|
+
color: {
|
|
11
|
+
type: String
|
|
12
|
+
},
|
|
13
|
+
onClick: {
|
|
14
|
+
type: Function as PropType<(e: Event) => void>
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
setup(props, { slots }) {
|
|
18
|
+
const handleClick = (e: Event) => props.onClick?.(e)
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
const { color } = props
|
|
22
|
+
|
|
23
|
+
return <>
|
|
24
|
+
<button
|
|
25
|
+
class="basic-button"
|
|
26
|
+
style={{ background: color }}
|
|
27
|
+
onClick={ handleClick }
|
|
28
|
+
>
|
|
29
|
+
{/* { pack.value.button } */}
|
|
30
|
+
{ slots.default?.() }
|
|
31
|
+
</button>
|
|
32
|
+
</>
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Button
|
|
2
|
+
|
|
3
|
+
### Basic Use
|
|
4
|
+
```html
|
|
5
|
+
<b-button>start</b-button>
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
### Theme Color Button
|
|
9
|
+
```html
|
|
10
|
+
<b-button color="#009688">start</b-button>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```vue
|
|
14
|
+
import ModifyColor from '../example/ModifyColor'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### Props
|
|
20
|
+
|
|
21
|
+
| Prop | Description | Type | Default |
|
|
22
|
+
| --- | --- | --- | --- |
|
|
23
|
+
| `color` | 按钮颜色 | _string_ | `default` |
|
|
24
|
+
|
|
25
|
+
### Events
|
|
26
|
+
|
|
27
|
+
| Event | Description | Arguments |
|
|
28
|
+
| --- | --- | --- |
|
|
29
|
+
| `click` | Triggered when the button is clicked | `event: Event` |
|
|
30
|
+
|
|
31
|
+
### Slots
|
|
32
|
+
|
|
33
|
+
| Slot | Description | Arguments |
|
|
34
|
+
|-----------| --- | --- |
|
|
35
|
+
| `default` | Button content | `-` |
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# 按钮
|
|
2
|
+
|
|
3
|
+
### 基本使用
|
|
4
|
+
```html
|
|
5
|
+
<b-button>组件库从我开始</b-button>
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
### 主题色按钮
|
|
9
|
+
```html
|
|
10
|
+
<b-button color="#009688">组件库从我开始</b-button>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```vue
|
|
14
|
+
import ModifyColor from '../example/ModifyColor'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### 属性
|
|
20
|
+
|
|
21
|
+
| 参数 | 说明 | 类型 | 默认值 |
|
|
22
|
+
| --- | --- | --- | --- |
|
|
23
|
+
| `color` | 按钮颜色 | _string_ | `default` |
|
|
24
|
+
|
|
25
|
+
### 事件
|
|
26
|
+
|
|
27
|
+
| 事件名 | 说明 | 参数 |
|
|
28
|
+
| --- | --- | --- |
|
|
29
|
+
| `click` | 点击按钮时触发 | `event: Event` |
|
|
30
|
+
|
|
31
|
+
### 插槽
|
|
32
|
+
|
|
33
|
+
| 插槽名 | 说明 | 参数 |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| `default` | 按钮内容 | `-` |
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { watchLang } from '@varlet/cli/site/utils'
|
|
3
|
+
import BButton from '../Button'
|
|
4
|
+
import { pack, use } from './locale'
|
|
5
|
+
|
|
6
|
+
watchLang(use, 'pc')
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<template>
|
|
10
|
+
<b-button color="#03A9F4">{{ pack.start }}</b-button>
|
|
11
|
+
</template>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import AppType from '@varlet/cli/site/mobile/components/AppType'
|
|
3
|
+
import { watchLang } from '@varlet/cli/site/utils'
|
|
4
|
+
import BasicUse from './BasicUse'
|
|
5
|
+
import { pack, use } from './locale'
|
|
6
|
+
|
|
7
|
+
watchLang(use)
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<template>
|
|
11
|
+
<app-type>{{ pack.basicUse }}</app-type>
|
|
12
|
+
<basic-use/>
|
|
13
|
+
</template>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// lib
|
|
2
|
+
import _zhCN from '../../../locale/zh-CN'
|
|
3
|
+
import _enCN from '../../../locale/en-US'
|
|
4
|
+
// mobile example doc
|
|
5
|
+
import zhCN from './zh-CN'
|
|
6
|
+
import enUS from './en-US'
|
|
7
|
+
import { useLocale, add as _add, use as _use } from '../../../locale'
|
|
8
|
+
|
|
9
|
+
const { add, use: exampleUse, pack, packs, merge } = useLocale()
|
|
10
|
+
|
|
11
|
+
const use = (lang: string) => {
|
|
12
|
+
_use(lang)
|
|
13
|
+
exampleUse(lang)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { add, pack, packs, merge, use }
|
|
17
|
+
|
|
18
|
+
// lib
|
|
19
|
+
_add('zh-CN', _zhCN)
|
|
20
|
+
_add('en-US', _enCN)
|
|
21
|
+
// mobile example doc
|
|
22
|
+
add('zh-CN', zhCN as any)
|
|
23
|
+
add('en-US', enUS as any)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import enUS from '../en-US'
|
|
2
|
+
import { useLocale } from '../index'
|
|
3
|
+
|
|
4
|
+
function mockConsole(method, fn = () => {}) {
|
|
5
|
+
const originMethod = console[method]
|
|
6
|
+
|
|
7
|
+
console[method] = fn
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
mockRestore() {
|
|
11
|
+
console[method] = originMethod
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test('test add lang', () => {
|
|
17
|
+
const { add, use, pack, packs } = useLocale()
|
|
18
|
+
|
|
19
|
+
const LANG = 'custom'
|
|
20
|
+
const PACK = {
|
|
21
|
+
button: 'Custom Text',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
add(LANG, PACK)
|
|
25
|
+
use(LANG)
|
|
26
|
+
|
|
27
|
+
expect(pack.value.lang).toBe(LANG)
|
|
28
|
+
expect(packs[LANG]).toBe(PACK)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('test merge lang', () => {
|
|
32
|
+
const fn = jest.fn()
|
|
33
|
+
const { mockRestore } = mockConsole('warn', fn)
|
|
34
|
+
const { add, use, merge, pack } = useLocale()
|
|
35
|
+
|
|
36
|
+
const LANG = 'en-US'
|
|
37
|
+
const PACK = {
|
|
38
|
+
button: 'Custom Text',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
add(LANG, enUS)
|
|
42
|
+
use('zh_HK')
|
|
43
|
+
use(LANG)
|
|
44
|
+
merge('zh_HK', PACK)
|
|
45
|
+
merge(LANG, PACK)
|
|
46
|
+
|
|
47
|
+
expect(pack.value.button).toBe('Custom Text')
|
|
48
|
+
expect(fn).toHaveBeenCalledTimes(2)
|
|
49
|
+
|
|
50
|
+
mockRestore()
|
|
51
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Locale
|
|
2
|
+
|
|
3
|
+
### Intro
|
|
4
|
+
Component library uses Chinese as the default language, support multi-language switch,
|
|
5
|
+
built-in support for `Chinese`, `English`.
|
|
6
|
+
|
|
7
|
+
### Multi-language switch
|
|
8
|
+
The `Locale` component is introduced to realize multi-language switching, and `Locale.add` is used for language extension.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
Locale.add('en-US', enUS)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Use `Locale.use` to switch languages.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
Locale.use('en-US')
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Use `Locale.merge` to merge languages.
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
Locale.merge('en-US', {
|
|
24
|
+
button: 'Hello'
|
|
25
|
+
})
|
|
26
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 国际化
|
|
2
|
+
|
|
3
|
+
### 介绍
|
|
4
|
+
组件库使用中文作为默认语言,支持多语言切换,内置支持 `中文`,`英文`。
|
|
5
|
+
|
|
6
|
+
### 多语言切换
|
|
7
|
+
引入 `Locale` 组件实现多语言切换,使用 `Locale.add` 进行语言扩展。
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
Locale.add('en-US', enUS)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
使用 `Locale.use` 进行切换语言
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
Locale.use('en-US')
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
使用 `Locale.merge` 进行语言合并
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
Locale.merge('en-US', {
|
|
23
|
+
button: 'Hello'
|
|
24
|
+
})
|
|
25
|
+
```
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ref } from 'vue'
|
|
2
|
+
import type { Ref } from 'vue'
|
|
3
|
+
import zhCN from './zh-CN'
|
|
4
|
+
|
|
5
|
+
export type Pack = {
|
|
6
|
+
// Button component
|
|
7
|
+
button: string
|
|
8
|
+
|
|
9
|
+
// internal
|
|
10
|
+
lang?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function useLocale<T = Pack>() {
|
|
14
|
+
const packs: Record<string, Partial<T>> = {}
|
|
15
|
+
const pack: Ref<Partial<T>> = ref({})
|
|
16
|
+
|
|
17
|
+
const add = (lang: string, pack: Partial<T> & { lang?: string }) => {
|
|
18
|
+
pack.lang = lang
|
|
19
|
+
packs[lang] = pack
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const use = (lang: string) => {
|
|
23
|
+
if (!packs[lang]) {
|
|
24
|
+
console.warn(`The ${lang} does not exist. You can mount a language package using the add method`)
|
|
25
|
+
return {}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
pack.value = packs[lang]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const merge = (lang: string, pack: Partial<T>) => {
|
|
32
|
+
if (!packs[lang]) {
|
|
33
|
+
console.warn(`The ${lang} does not exist. You can mount a language package using the add method`)
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
packs[lang] = { ...packs[lang], ...pack }
|
|
38
|
+
|
|
39
|
+
use(lang)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
packs,
|
|
44
|
+
pack,
|
|
45
|
+
add,
|
|
46
|
+
use,
|
|
47
|
+
merge,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { packs, pack, add, use, merge } = useLocale()
|
|
52
|
+
|
|
53
|
+
add('zh-CN', zhCN)
|
|
54
|
+
use('zh-CN')
|
|
55
|
+
|
|
56
|
+
export { packs, pack, add, use, merge, useLocale }
|
|
57
|
+
|
|
58
|
+
export const _LocaleComponent = { packs, pack, add, use, merge, useLocale }
|
|
59
|
+
|
|
60
|
+
export default {
|
|
61
|
+
packs,
|
|
62
|
+
pack,
|
|
63
|
+
add,
|
|
64
|
+
use,
|
|
65
|
+
merge,
|
|
66
|
+
useLocale,
|
|
67
|
+
}
|
package/lib/commands/create.d.ts
CHANGED