core icon indicating copy to clipboard operation
core copied to clipboard

Translate in a given language

Open X4V1 opened this issue 2 years ago • 1 comments

Hello,

I have a specific need and I don't find a solution with no downside. Maybe I just missed something but I can't find what.

My need: get a translation in a lang different than the currently used language. (example: app in english, need just one translation in french)

I found 2 ways of doing it:

  1. Change the entire app language (TranslateService.use), retrieve the traduction and set back the original language.
  2. Retrieve all the translations of the given language and manually find what I need (TranslateService. getTranslation).

I don't like the first approach because the entire app lang is changed and I can't be sure that angular is not trying to get a translation during that process (would cause a wrong translation if it happens) For the second approach, the interpolation is not longer possible (or must be done manually).

Is there an easier way to do it ?

X4V1 avatar Jan 27 '22 15:01 X4V1

Hi X4V1, you can create your own custom loader, load more than one resource and merge them as needed. Take a look at my article where I use this trick to make translations much more modular, but the loader I implemented should help you get started on a good idea. Angular: Modular translations

ppowstanski avatar Mar 10 '22 06:03 ppowstanski