mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.09k stars 421 forks source link

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

Open abzokhattab opened 3 years ago

abzokhattab commented 3 years ago

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"
}
mashpie commented 3 years ago

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

abzokhattab commented 3 years ago

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?

mashpie commented 3 years ago

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

so in your case:

i18n.__("error:Server error")

should do