Skip to content

Commit

Permalink
refactor(config): external locale config for on demand import (vexip-…
Browse files Browse the repository at this point in the history
…ui#302)

* refactor: extra locale config

* docs: update

BREAKING CHANGE: `en-US` and `ta-IN` isn't included in internal locale config no longer.
Now these config are exported independently, using e.g. `import { enUSLocale } from 'vexip-ui'` 
to import locale config that you want to use. See docs about global config for more details.
  • Loading branch information
qmhc authored Apr 6, 2023
1 parent e9ee8ce commit 86eab10
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 50 deletions.
26 changes: 12 additions & 14 deletions common/config/src/locale/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { computed, provide, inject, unref } from 'vue'
import { mergeObjects } from '@vexip-ui/utils'
import { zhCNLocale } from './zh-CN'
import { enUSLocale } from './en-US'
import { taINLocale } from './ta-IN'

import type { App, ComputedRef } from 'vue'
import type { LocaleConfig, LocaleNames, LocaleOptions } from './helper'
Expand All @@ -11,45 +9,45 @@ import type { MaybeRef } from '../types'
export * from './helper'
export * from './zh-CN'
export * from './en-US'
export * from './ta-IN'

export const PROVIDED_LOCALE = '__vxp-provided-locale'
export const globalLocal = computed(() => zhCNLocale())

const cached = new Map<string, LocaleConfig>()

export function getDefaultLocaleConfig(locale?: string) {
if (!locale) {
return globalLocal.value
}

switch (locale) {
case 'en-US':
return enUSLocale()
case 'ta-IN':
return taINLocale()
default:
return zhCNLocale()
}
return cached.get(locale) || globalLocal.value
}

export function registerLocale(locale: LocaleConfig) {
locale.locale && cached.set(locale.locale, locale)
}

export function configLocale(sourceLocale: MaybeRef<LocaleOptions>, app?: App) {
if (app) {
const locale = computed(() => {
const locale = unref(sourceLocale)

return mergeObjects(getDefaultLocaleConfig(locale.locale), locale, false)
return mergeObjects(getDefaultLocaleConfig(locale.locale), locale)
})

app.provide(PROVIDED_LOCALE, locale)
} else {
const upstreamLocale = inject<ComputedRef<LocaleConfig> | null>(PROVIDED_LOCALE, null)
const locale = computed(() => {
const locale = unref(sourceLocale)
const providedLocale = mergeObjects(getDefaultLocaleConfig(locale.locale), locale)
// const providedLocale = mergeObjects(getDefaultLocaleConfig(locale.locale), locale)

if (!upstreamLocale?.value) {
return providedLocale
return mergeObjects(getDefaultLocaleConfig(locale.locale), locale)
}

return mergeObjects(upstreamLocale.value as any, providedLocale)
return mergeObjects(upstreamLocale.value as any, locale)
})

provide(PROVIDED_LOCALE, locale)
Expand Down
8 changes: 4 additions & 4 deletions components/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { toCapitalCase } from '@vexip-ui/utils'

import type { Ref, App } from 'vue'
import type { LocaleOptions, IconsOptions } from '@vexip-ui/config'
import type { LocaleConfig, LocaleOptions, IconsOptions } from '@vexip-ui/config'
import type { PropsOptions } from './props'

type MaybeRef<T> = T | Ref<T>
Expand All @@ -23,19 +23,19 @@ export interface InstallOptions {
icons?: MaybeRef<IconsOptions>
}

export function buildInstall(components: any[] = [], defaultLocale?: 'zh-CN' | 'en-US') {
export function buildInstall(components: any[] = [], defaultLocale?: LocaleConfig) {
return function install(app: App, options: InstallOptions = {}) {
const {
prefix = '',
namespace = '',
props = {},
locale = { locale: defaultLocale },
locale = defaultLocale,
zIndex,
icons = {}
} = options

const withDefaultLocale = computed(() => {
return { locale: defaultLocale, ...unref(locale) }
return { ...defaultLocale, ...unref(locale) }
})

configNamespace(namespace, app)
Expand Down
8 changes: 0 additions & 8 deletions components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,7 @@ import { Title, Text, Blockquote, OL, UL, H1, H2, H3, H4, H5, H6, P, Strong } fr
import { buildInstall } from './create'
import { install as installDirectives } from '@/directives'

export type {
ComponentSize,
ComponentState,
LocaleConfig,
LocaleNames,
LocaleOptions
} from '@vexip-ui/config'
export type { PropsOptions } from './props'

export { version } from './version'

const components = [
Expand Down
1 change: 1 addition & 0 deletions docs/common/header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ function formatComponentName(name: string) {
display: flex;
padding: 0;
color: var(--vxp-content-color-base);
cursor: pointer;
background-color: transparent;
border: 0;
}
Expand Down
18 changes: 14 additions & 4 deletions docs/guides/global/desc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,24 @@ After set, all components name will be prefixed with `V`.
I18n can be configured by passing the `locale` option in the second parameter when calling `app.use`.

```ts
import { enUSLocale } from 'vexip-ui'

app.use(install, {
locale: {
locale: 'en-US'
}
locale: enUSLocale()
})
```

The default language used can be set through `locale.locale`. Currently, Vexip UI provides two built-in languages, `'zh-CN'` and `'en-US'`.
The default language of Vexip UI is `'zh-CN'`. You can register a locale via `registerLocale` method, and then change the locale config via `locale.locale` property.

```ts
import { registerLocale, enUSLocale } from 'vexip-ui'

registerLocale(enUSLocale())

app.use(install, {
locale: { locale: 'en-US' }
})
```

Also you can customize the i18n for each components:

Expand Down
18 changes: 14 additions & 4 deletions docs/guides/global/desc.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,24 @@ createApp(App).use(install, { prefix: 'V' })
在调用 `app.use` 时通过在第二个参数传入 `locale` 选项可以为所有组件配置国际化。

```ts
import { enUSLocale } from 'vexip-ui'

app.use(install, {
locale: {
locale: 'zh-CN'
}
locale: enUSLocale()
})
```

通过 `locale.locale` 可以设置使用的默认语言,目前 Vexip UI 提供了 `'zh-CN'``'en-US'` 两种内置语言。
Vexip UI 的默认语言为 `'zh-CN'`,你可以通过 `registerLocale` 方法注册一个国际化配置,随后通过修改 `locale.locale` 属性快速切换。

```ts
import { registerLocale, enUSLocale } from 'vexip-ui'

registerLocale(enUSLocale())

app.use(install, {
locale: { locale: 'en-US' }
})
```

同时你还可以定制化一些组件的国际化:

Expand Down
3 changes: 3 additions & 0 deletions docs/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref } from 'vue'
import { registerLocale, enUSLocale } from 'vexip-ui'
import { createI18n } from 'vue-i18n'
import { zhCN } from './zh-CN'
import { enUS } from './en-US'
Expand Down Expand Up @@ -27,3 +28,5 @@ export const i18n = createI18n({
export const vexipuiLocale = ref<LocaleOptions>({
locale: 'zh-CN'
})

registerLocale(enUSLocale())
2 changes: 1 addition & 1 deletion docs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@/*": ["../*"],
"@docs/*": ["./*"],
"vexip-ui/es/*": ["../components/*"],
"vexip-ui": ["../components"]
"vexip-ui": [".."]
},
"lib": ["esnext", "dom"]
},
Expand Down
2 changes: 1 addition & 1 deletion docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineConfig(({ command }) => {
find: /^@vexip-ui\/((?!icons).+)/,
replacement: resolve(__dirname, '../common/$1/src')
},
{ find: /^vexip-ui$/, replacement: resolve(__dirname, '../components') }
{ find: /^vexip-ui$/, replacement: resolve(__dirname, '..') }
]
: [])
],
Expand Down
2 changes: 1 addition & 1 deletion components/full-lib.ts → full-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import '@/style/index.scss'
import '@/style/dark/preset.scss'

export * from '.'
export * from './components'
9 changes: 9 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from './components'
export { registerLocale, zhCNLocale, enUSLocale, taINLocale } from '@vexip-ui/config'

export type {
ComponentSize,
ComponentState,
LocaleConfig,
LocaleNames,
LocaleOptions
} from '@vexip-ui/config'
9 changes: 0 additions & 9 deletions scripts/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,9 @@ async function main() {
} from './typography'
import { install as installDirectives } from '@/directives'
import { buildInstall } from './create'
export type {
ComponentSize,
ComponentState,
LocaleConfig,
LocaleNames,
LocaleOptions
} from '@vexip-ui/config'
export type { PropsOptions } from './props'
export { version } from './version'
const components = [
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"./*"
],
"@vexip-ui/*": ["common/*/src"],
"vexip-ui": ["components"],
"vexip-ui": ["."],
"vue-router": ["node_modules/vue-router"]
},
"lib": [
Expand All @@ -39,6 +39,7 @@
"directives",
"playground",
"index.ts",
"full-lib.ts",
"types.d.ts"
],
"exclude": [
Expand Down
5 changes: 2 additions & 3 deletions vite.full.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface Manifest {
}

const pkg = JSON.parse(readFileSync(resolve(__dirname, 'package.json'), 'utf-8')) as Manifest
const componentsDir = resolve(__dirname, 'components')

const logLevel = process.env.LOG_LEVEL

Expand All @@ -41,9 +40,9 @@ export default defineConfig(async () => {
},
build: {
outDir,
sourcemap: false,
sourcemap: true,
lib: {
entry: resolve(componentsDir, 'full-lib.ts'),
entry: resolve(__dirname, 'full-lib.ts'),
formats: ['es', 'cjs', 'iife'],
name: 'VexipUI',
fileName: format => `vexip-ui.${format === 'es' ? 'mjs' : format === 'cjs' ? 'cjs' : 'js'}`
Expand Down

0 comments on commit 86eab10

Please sign in to comment.