Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
abzokhattab opened this issue Oct 21, 2021 · 3 comments

Comments

@abzokhattab
Copy link

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
Copy link
Owner

mashpie commented Oct 21, 2021

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
Copy link
Author

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
Copy link
Owner

mashpie commented Oct 21, 2021

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

so in your case:

i18n.__("error:Server error")

should do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants