mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-19 06:21:46 +01:00
feat(useLocale): handle generic messages (#3100)
Co-authored-by: hywax <me@hywax.space> Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -6,19 +6,19 @@ import { get } from './index'
|
||||
|
||||
export type TranslatorOption = Record<string, string | number>
|
||||
export type Translator = (path: string, option?: TranslatorOption) => string
|
||||
export type LocaleContext = {
|
||||
locale: Ref<Locale>
|
||||
export type LocaleContext<M> = {
|
||||
locale: Ref<Locale<M>>
|
||||
lang: Ref<string>
|
||||
dir: Ref<Direction>
|
||||
code: Ref<string>
|
||||
t: Translator
|
||||
}
|
||||
|
||||
export function buildTranslator(locale: MaybeRef<Locale>): Translator {
|
||||
export function buildTranslator<M>(locale: MaybeRef<Locale<M>>): Translator {
|
||||
return (path, option) => translate(path, option, unref(locale))
|
||||
}
|
||||
|
||||
export function translate(path: string, option: undefined | TranslatorOption, locale: Locale): string {
|
||||
export function translate<M>(path: string, option: undefined | TranslatorOption, locale: Locale<M>): string {
|
||||
const prop: string = get(locale, `messages.${path}`, path)
|
||||
|
||||
return prop.replace(
|
||||
@@ -27,11 +27,11 @@ export function translate(path: string, option: undefined | TranslatorOption, lo
|
||||
)
|
||||
}
|
||||
|
||||
export function buildLocaleContext(locale: MaybeRef<Locale>): LocaleContext {
|
||||
export function buildLocaleContext<M>(locale: MaybeRef<Locale<M>>): LocaleContext<M> {
|
||||
const lang = computed(() => unref(locale).name)
|
||||
const code = computed(() => unref(locale).code)
|
||||
const dir = computed(() => unref(locale).dir)
|
||||
const localeRef = isRef(locale) ? locale : ref(locale)
|
||||
const localeRef = isRef(locale) ? locale : ref(locale) as Ref<Locale<M>>
|
||||
|
||||
return {
|
||||
lang,
|
||||
|
||||
Reference in New Issue
Block a user