Add either an option or new method to specify on a particular translation to not fallback to default locale. Probably would need to be a new method so as to not impact existing api. I'd suggest tnf for translate no fallback.
Use case, as you develop your app you often add text which is not yet translated in other languages.
Often times, you can get by with existing strings to some extent in the meantime but they aren't ideal. And the localized non ideal message is still better then showing an unlocalized ideal message that they can't understand.
Example:
let messages = {
msgs: {
// ORIGINAL MESSAGES (that have already been translated)
error: "An Error occurred",
// ADDED LATER
newMoreDescriptiveError: "An Error occurred because of XYZ"
}
}
I'd like the ability to do something like:
this.$i18n.tnf( 'msgs.newMoreDescriptiveError') || this.$i18n.t('msgs.error')
(notice t == tnf for t-no-fallback on first one)
This would allow you to:
IF current Locale has 'msgs.newMoreDescriptiveError' use it.
ELSE IF Locale has 'msg.error' use that.
ELSE use 'msg.newMoreDescriptiveError' from fallback locale.
I have other use cases where I could construct strings using two linked locales for things like table headings and placeholder text.
I would like this as well. Or at least a way of checking if the key exists before calling the $t() method.
Due to the default behaviour of letting the tag untranslated when not found, what I ended doing in my case is this workaround:
translation (label) {
return this.$t(`my.path.to.the.${label}`).includes('my.path.to.the.')
? label
: this.$t(`my.path.to.the.${label}`)
}
For example, if my label is "LABEL", my path to the translation will be "my.path.to.the.LABEL". If that does not exists, it will print it untranslated, which includes the text I search for, and therefore I can safely return the original label as a user friendly fallback.
This feature looks very useful.
However, if I understand correctly, the suggested code have a problem in asynchronus situation.
To implement the behavior ELSE use 'msg.newMoreDescriptiveError' from fallback locale., an i18n instance should remember the path given to $tnf.
If several workers call $tnf simultaneously, it is indeterministic to determine fallback key for $t.
I suggest
tnf(
t('keyAddedLast', {'name': name, `v`:v}),
t('keyAddedRecently', {'name': name}),
t('keyOldest'),
"This is not key of message"
)
where
// pseudo code
function tnf (...translates: Array<string>): string {
if (translates.length === 0) throw new Error('No translates given.')
for (let translate of translates) {
// if an untranslated path is found, skip it
if (te(translate)) {
continue
}
return te
}
warn('No fallback found.')
return null
}
we supported in Vue I18n v9.
https://vue-i18n.intlify.dev/api/composition.html#t-key-defaultmsg-options
please try it!
Most helpful comment
Due to the default behaviour of letting the tag untranslated when not found, what I ended doing in my case is this workaround:
For example, if my label is "LABEL", my path to the translation will be "my.path.to.the.LABEL". If that does not exists, it will print it untranslated, which includes the text I search for, and therefore I can safely return the original label as a user friendly fallback.