-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useGlobalConfig, useLocale hooks
- Loading branch information
Showing
12 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useGlobalConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { configProviderContextKey } from '../tokens' | ||
import { mergeObjects } from '../shared' | ||
import type { InstallOptions } from '../tokens' | ||
import type { App, Ref } from 'vue' | ||
import type { MaybeRef } from '@vueuse/core' | ||
|
||
const globalConfig = ref<InstallOptions>() | ||
|
||
/** | ||
* 获取全局配置 Hooks | ||
* @param key | ||
* @param defaultValue | ||
*/ | ||
export function useGlobalConfig<T extends keyof InstallOptions, U extends InstallOptions[T]>(key: T, defaultValue?: U): Ref<Exclude<InstallOptions[T], undefined | U>> | ||
export function useGlobalConfig(): Ref<InstallOptions> | ||
export function useGlobalConfig(key?: keyof InstallOptions, defaultValue = undefined) { | ||
const config = getCurrentInstance() ? inject(configProviderContextKey, globalConfig) : globalConfig | ||
|
||
if (key) | ||
return computed(() => config.value?.[key] ?? defaultValue) | ||
else | ||
return config | ||
} | ||
|
||
/** | ||
* 注入配置 | ||
* @param config | ||
* @param app | ||
* @param global | ||
*/ | ||
export function provideGlobalConfig(config: MaybeRef<InstallOptions>, app?: App, global = false) { | ||
const sourceConfig = getCurrentInstance() ? useGlobalConfig() : undefined | ||
const provideFn = app?.provide ?? (getCurrentInstance() ? provide : undefined) | ||
|
||
if (!provideFn) return | ||
|
||
const context = computed(() => { | ||
const cfg = unref(config) | ||
if (!sourceConfig?.value) return cfg | ||
|
||
return mergeObjects(sourceConfig.value, cfg) | ||
}) | ||
|
||
provideFn(configProviderContextKey, context) | ||
|
||
// 初始化 | ||
if (global || !globalConfig.value) | ||
globalConfig.value = context.value | ||
|
||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { deepGet } from '../shared' | ||
import { en } from '../locale' | ||
import { useGlobalConfig } from './useGlobalConfig' | ||
import type { language } from '../locale' | ||
import type { Ref } from 'vue' | ||
import type { MaybeRef } from '@vueuse/core' | ||
|
||
export type OnuTranslatorOption = Record<string, string | number> | ||
export type OnuTranslator = (path: string, option?: OnuTranslatorOption) => string | ||
export interface OnuLocaleContext { | ||
locale: Ref<language> | ||
lang: Ref<string> | ||
t: OnuTranslator | ||
} | ||
|
||
export const translate = (path: string, option: OnuTranslatorOption | undefined, locale: language) => { | ||
return (deepGet(locale, path, path) as string).replace(/\{(\w+)\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}`) | ||
} | ||
|
||
export const buildTranslator = (locale: MaybeRef<language>): OnuTranslator => (path, option) => translate(path, option, unref(locale)) | ||
|
||
export const buildLocaleContext = (locale: MaybeRef<language>): OnuLocaleContext => { | ||
const lang = computed(() => unref(locale).name) | ||
const RefLocale = isRef(locale) ? locale : ref(locale) | ||
|
||
return { | ||
locale: RefLocale, | ||
lang, | ||
t: buildTranslator(locale), | ||
} | ||
} | ||
|
||
export function useLocale() { | ||
const locale = useGlobalConfig('locale') | ||
return buildLocaleContext(computed(() => locale.value || en)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './vue' | ||
export * from './shared' | ||
export * from './locale' | ||
export * from './tokens' | ||
export * from './hooks' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Locale guide | ||
|
||
1. Define component locale, like `utils/locale/*`. | ||
2. Add other language support? Add locale file in `locale`, then export it from `index.ts`. | ||
3. Use i118n in component, please learn more about `useLocale`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export { default as en } from './lang/en' | ||
export { default as zhCn } from './lang/zh-cn' | ||
|
||
export interface TranslatePair { | ||
[key: string]: string | string[] | TranslatePair | ||
} | ||
|
||
export interface language { | ||
name: string | ||
locales: TranslatePair | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
name: 'en', | ||
locales: { | ||
empty: { | ||
emptyText: 'No data', | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
name: 'zh-cn', | ||
locales: { | ||
empty: { | ||
emptyText: '暂无数据', | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { language } from '../locale' | ||
import type { InjectionKey, Ref } from 'vue' | ||
import type{ MaybeRef } from '@vueuse/core' | ||
|
||
export interface InstallOptions { | ||
prefix?: string | ||
namespace?: MaybeRef<string> | ||
locale?: MaybeRef<language> | ||
zIndex?: MaybeRef<number> | ||
} | ||
|
||
export const configProviderContextKey: InjectionKey<Ref<InstallOptions>> = Symbol('__onu_config_provider') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './config-provider' |