Open plalx opened 3 years ago
@plalx I've made that very same observation to finally understand where I was going wrong.
AFAIK, the forChild() function is to be called only from a lazy loaded module which will have his own injector. Any feature module that are eagerly loaded will be provided in the root injector.
In other words, calling forRoot() and forChild() in modules that are all loaded eagerly loaded during application bootstrapping will overriding each other.
For lazy-loaded modules with different translation loaders (loading .json
from different files) it seems to be either (in the case of the lazy-loaded):
isolate: false
, extend: true
) React to parent module translation events automatically without having to connect anything, just as they say, but cannot load the lazy loaded specific files.isolate: true
, extend: true
) We have to propagate changes to parent's translation event changes to the lazy child ourselves, and we can have our specific translations working! But the parent's translation won't work.It's like I can't blend the two.
I got pretty close though maybe you could have a look and play within StackBlitz
: https://stackblitz.com/edit/translations-and-lazy-loading?file=README.md
I'm not sure whether this should be considered a bug or not, but I find it VERY misleading that
forChild()
seems to be overriding providers defined withforRoot()
even though no explicit overrides have been specified.I'm setting up the
TranslateModule
for all tests (getTestBed().initTestEnvironment(...)
) with the following module:Then in a component test I was trying to load the component's parent module (which uses
TranslateModule.forChild()
) and noticed that the rootloader
andmissingTranslationHandler
were not picked-up or being overriden.Looking at
forChild
's implementation we can see that it overrides theloader
andmissingTranslationHandler
even though none were provided as argument.I'm not sure if it's possible (new to Angular), but I would expect
forChild
providers to default to parent/root values when none provided. I guess one way of doing that could be to omit a default provider entirely? Anyway, I know I could generally just useTranslateModule
and droppingforChild()
, but in this case theforChild()
call is in a third-party library so it's not an option...Any workarounds?