kazupon / vue-i18n

:globe_with_meridians: Internationalization plugin for Vue.js

Home Page:https://kazupon.github.io/vue-i18n/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(Series-)fallback translation

LukasOchmann opened this issue · comments

Clear and concise description of the problem

Depending on the status of an Request-response i want to show a unique message:

const { t } = i18n.global
const response = call("..")
alert(t(`messages.status.${response.status}`)

if an unexpected status is comming up, the translation key would be shown. Therefore i fist check if the key exists using te

const { t, te } = i18n.global
const response = call("..")
const path = `messages.status.${response.status}`
alert(te(path) ? t(path): t('messages.default`)

in my opinion this is not very clean

Suggested solution

A function ta(Array) (or maybe there is a better naming).

export declare interface VueI18n<...> {
  ta(keys: Path[]): TranslateResult;
  ta(keys: Path[], locale: Locale): TranslateResult;
  ta(keys: Path[], locale: Locale, list unknown[]): TranslateResult;
  ta(keys: Path[], locale: Locale, named: Object): TranslateResult;
  ta(keys: Path[], list: unknown[]): TranslateResult;
  ta(keys: Path[], named: Record<string, unknown>): TranslateResult;
}

Each tries to resolve the provided Paths from the left to the right. And returns the first successfully resolved TranslateResult

Alternative

Another t Signature like:

...
t(key: Path, fallback: Path): TranslateResult;
t(key: Path, fallback: Path, locale: Locale): TranslateResult;
t(key: Path, fallback: Path, locale: Locale, list: unkown[]): TranslateResult;
t(key: Path, fallback: Path, locale: Locale, named: object): TranslateResult;
t(key: Path, fallback: Path, list: unkown[]): TranslateResult;
t(key: Path, fallback: Path, named: Record<string, unkown>): TranslateResult;

Tried to resolve thekey if it dose not exist the fallback will be tried.

Additional context

This proposal is inspired by i18next behaviour: https://www.i18next.com/translation-function/essentials#multiple-fallback-keys

Validations