Hierarchical TranslateService Proposal #1599
CodeAndWeb
started this conversation in
Ideas
Replies: 0 comments
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)
MissingTranslationHandlerfrom SubScreen1 (may inherit from parent)Option B: Scope Priority
MissingTranslationHandlerfrom 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):
{ "a": { "app": "lorem" }, "override": "app", "common": { "save": "Save" } }Screen1 (currentLang: en):
{ "a": { "screen": "ipsum" }, "override": "screen1", "screen1": { "title": "Screen 1 Title" } }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
onLangChangeEmits after the all services complete loading the language.
Event data:
{ lang: string }Breaking change:
onLangChangedoes not provide the translations anymore. This is notpossible due to the hierarchy structure.
onFallbackLangChangeEmits after the all services complete loading the language.
Event data:
{ lang: string }Breaking change:
onLangChangedoes not provide the translations anymore. This is notpossible due to the hierarchy structure.
onTranslationChangeEmits 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:
onTranslationChangedoes not provide the translations anymore. This is notpossible due to the hierarchy structure.
Implementation Details
TranslateServicebecomes an abstract class, keeping the shared functionality.TranslateServiceimplementation is split intoRootTranslateServiceChildTranslateServiceTranslateStoremight become partially obsolete.TranslatePipeandTranslateDirectivemust be updated to work with the new Observables.extendis restricted to the service itself. Maybe is even obsolete and can be removed?RootTranslateServiceonLangChangeandonTranslationChangeeventsChildTranslateServiceuse(lang)setFallbackLang(lang)Related: #1585
Beta Was this translation helpful? Give feedback.
All reactions