Hierarchical TranslateService Proposal #1599
CodeAndWeb
started this conversation in
Ideas
Replies: 1 comment
-
@CodeAndWeb notes I could find (will need to read it twice) Loading behaviour
Parent are still loadingWhat about adding a loading state jn the store translation resolutionWhat about allowing one currant & fallback lang per store? I don't see a use of having multiple If you do so, then you isolate it. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hierarchical TranslateService Proposal
Overview
This proposal defines a hierarchical architecture for ngx-translate that enables child
TranslateService instances to inherit and override translations from their parent services
while maintaining isolation and efficient loading.
Example Application Structure
This architecture supports applications with:
Use Cases:
Desired behaviour
Language Loading Behavior
use(lang)
orsetFallbackLang(lang)
on any service triggers language loading for all services in the hierarchy (up and down)use()
andsetFallbackLang()
observables complete only when all services finish loading (or failed)Problem: New child services may be instantiated while parent language loading is in progress.
Translation Isolation
Isolation Principles
Benefits
Translation Resolution
Services resolve translations by checking their own store first, then traversing up the
hierarchy to parent stores. Child and sibling translations are not accessible.
Resolution order for a translation key in SubScreen1:
Option A: Language Priority (Recommended)
MissingTranslationHandler
from SubScreen1 (may inherit from parent)Option B: Scope Priority
MissingTranslationHandler
from SubScreen1Recommendation: Option A provides more intuitive behavior by exhausting all current
language options before falling back to fallback languages. This matches user expectations when switching languages dynamically.
Resolution Example
Translation Files:
AppLevel (currentLang: en):
Screen1 (currentLang: en):
Translation Resolution Results (assuming no fallbackLang):
a.app
"lorem"
"lorem"
a.screen
"ipsum"
common.save
"Save"
"Save"
override
"app"
"scrren1"
a
{app: "lorem"}
{screen: "ipsum"}
Important Limitations
No Object Merging: Translation objects from different scopes are not merged.
Calling
instant("a")
in Screen1 returns{screen: "ipsum"}
, not{app: "lorem", screen: "ipsum"}
.Object merging would create unpredictable behavior
and performance issues when dealing with large translation objects or deep nesting.
Observables
onLangChange
Emits after the all services complete loading the language.
Event data:
{ lang: string }
Breaking change:
onLangChange
does not provide the translations anymore. This is notpossible due to the hierarchy structure.
onFallbackLangChange
Emits after the all services complete loading the language.
Event data:
{ lang: string }
Breaking change:
onLangChange
does not provide the translations anymore. This is notpossible due to the hierarchy structure.
onTranslationChange
Emits when translations are loaded or updated (both: current and fallback language) in
the current service or any higher scoped service.
Event data:
{ lang: string }
Breaking change:
onTranslationChange
does not provide the translations anymore. This is notpossible due to the hierarchy structure.
Implementation Details
TranslateService
becomes an abstract class, keeping the shared functionality.TranslateService
implementation is split intoRootTranslateService
ChildTranslateService
TranslateStore
might become partially obsolete.TranslatePipe
andTranslateDirective
must be updated to work with the new Observables.extend
is restricted to the service itself. Maybe is even obsolete and can be removed?RootTranslateService
onLangChange
andonTranslationChange
eventsChildTranslateService
use(lang)
setFallbackLang(lang)
Related: #1585
Beta Was this translation helpful? Give feedback.
All reactions