aralroca / next-translate

Next.js plugin + i18n API for Next.js 🌍 - Load page translations and use them in an easy way!
MIT License
2.65k stars 206 forks source link

Make missing translations in any locale fallback to default locale #463

Open jahirfiquitiva opened 3 years ago

jahirfiquitiva commented 3 years ago

So, I had something like:

locales/en/common.json

{
  "hello": "hello",
  "world": "world",
}

locales/es/common.json

{
  "hello": "hola",
}

So, when I used t('common:world'), in Spanish it would show literally common:world, instead of the default text in the default locale: world

Therefore, this is a request for you to add support for such thing. Thanks in advance.

aralroca commented 3 years ago

@jahirfiquitiva You can configure it by doing the following:

{
  // ...rest of config
  "loadLocaleFrom": async (lang, ns) => {
     const locales = await import(`./locales/${lang}/${ns}.json`).then((m) => m.default)
     const defaultLocales = await import(`./locales/en/${ns}.json`).then((m) => m.default)
     return { ...defaultLocales, ...locales }
   }
}

By default, it is not interesting to do it because it downloads 2 dictionaries for each page and it is better that whoever wants to configure it.

j-schumann commented 3 years ago

I was missing this feature too. Maybe this custom loader can be used in development while for the build process the plugin triggers this loader and stores the result ({ ...defaultLocales, ...locales }) in single language dictionaries again.

jasonwilliams commented 2 years ago

+1 on this. This is something other translations libraries provide so i was suprised to see it didn't fallback out the box. I understand its for performance reasons. Maybe there could be some opt-in like you described.

btw im not sure if I should be voting for this feature here or here

j-schumann commented 2 years ago

I understand its for performance reasons. Maybe there could be some opt-in like you described.

With an additional build step, that is triggered when next.js builds the app, there would be no performance hit as the app would use the single file for the requested locale, already merged with the fallback language.

@aralroca maybe you can use something from https://github.com/vercel/next.js/discussions/17479 to create the merged file(s) before webpack includes the locales.