i18n-node icon indicating copy to clipboard operation
i18n-node copied to clipboard

how to pass a default translation if a phrase isnt existed in the en.json file

Open abzokhattab opened this issue 3 years ago • 3 comments

Hello everybody ,,

How to pass a default translation if a phrase isn't existed in the en.json file

i cant find any option to add a default translation if the translation is not found ?

example

en.json ->
{
"Hi","Hola"
}

i18.__("hello") --> not there in the file 
{
"Hi","Hola",
"hello":"hello"
}

howover i want to set a defualt value if the translation is not found to be --->

{
"Hi","Hola",
"hello":"default_value"
}

abzokhattab avatar Oct 21 '21 13:10 abzokhattab

you could setup fallbacks, ie.:

i18n.configure({
  // setup some locales - other locales default to en silently
  locales: ['en', 'de'],

  // fallback from Dutch to German and from any localized German (de-at, de-li etc.) to German
  fallbacks: { nl: 'de', 'de-*': 'de' },
  //...

check tests for inspiration: https://github.com/mashpie/i18n-node/blob/master/test/i18n.fallbacks.js

mashpie avatar Oct 21 '21 17:10 mashpie

Thanks, @mashpie for the reply however, i meant a default translation for a specific phrase not a fallback to another language:

consider this senario: i have only one locale file (en.json) with the following content:

{
"hi":"hola"
}

then i want to translate a phrase that isnt there in the locale file example ->

i18n.__("error")

results:

{
"hi":"hola",
"error","error"
}

as you can see the error phrase was translated to error because it is not presented in the file

What I'd like to do is add a default value for phrases that don't exist in the file. Here's what I'm expecting ->

i18n.__("error", {defaultValue:"Server error"})

results:

{
"hi":"hola",
"error","Server error"
}

is there such an option?

abzokhattab avatar Oct 21 '21 17:10 abzokhattab

Looks like you are looking for https://github.com/mashpie/i18n-node#defaults

so in your case:

i18n.__("error:Server error")

should do

mashpie avatar Oct 21 '21 18:10 mashpie