feat(locale): provide dir on defineLocale (#2620)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Alex
2024-11-13 16:19:21 +05:00
committed by GitHub
parent 9c00f7c7b7
commit 937585cb3f
26 changed files with 467 additions and 470 deletions

View File

@@ -9,6 +9,7 @@ export type Translator = (path: string, option?: TranslatorOption) => string
export type LocaleContext = {
locale: Ref<Locale>
lang: Ref<string>
dir: Ref<string>
code: Ref<string>
t: Translator
}
@@ -18,7 +19,7 @@ export function buildTranslator(locale: MaybeRef<Locale>): Translator {
}
export function translate(path: string, option: undefined | TranslatorOption, locale: Locale): string {
const prop: string = get(locale, path, path)
const prop: string = get(locale, `messages.${path}`, path)
return prop.replace(
/\{(\w+)\}/g,
@@ -29,11 +30,13 @@ export function translate(path: string, option: undefined | TranslatorOption, lo
export function buildLocaleContext(locale: MaybeRef<Locale>): LocaleContext {
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)
return {
lang,
code,
dir,
locale: localeRef,
t: buildTranslator(locale)
}