fix(transloco): 🐛 Allow translateSignal API to access any scope#891
fix(transloco): 🐛 Allow translateSignal API to access any scope#891maleetz wants to merge 1 commit intojsverse:masterfrom
Conversation
@jsverse/transloco
@jsverse/transloco-locale
@jsverse/transloco-messageformat
@jsverse/transloco-optimize
@jsverse/transloco-persist-lang
@jsverse/transloco-persist-translations
@jsverse/transloco-preload-langs
@jsverse/transloco-schematics
@jsverse/transloco-scoped-libs
@jsverse/transloco-utils
@jsverse/transloco-validator
commit: |
|
I also created a branch to showcase an alternative which consolidates the scope loading logic from the directive / pipe so that we can reuse it in selectTranslate for this use case: maleetz@213423c#diff-c07e4a02be032d92cac1cfbef925cccb4f35c3a4a1697a76a9993f7790ca296c. Things I like about the consolidation:
Things I don't like:
|
When configuring Transloco with scopes.autoPrefixKeys:false, the key parameter for selectTranslate must include a scope prefix to select scoped translations. Using that, we can then find the correct ProviderScope with any inline loaders that must be resolved before translating. ✅ Closes: jsverse#875
894a21d to
cf59c01
Compare
There was a problem hiding this comment.
Hey @maleetz, thanks for the continued effort on this — really appreciate you sticking with it.
I've merged PR #866 as an immediate fix (takes [lang.length - 1] instead of [0]), which addresses the most common case. However, looking at this PR more closely, I think we should go with the alternative approach you yourself explored in 213423c, your instincts there were right.
The approach you explored is closer, but it still has the same gap - when autoPrefixKeys is on (the default), it doesn't load all scopes. The fix needs to unconditionally forkJoin-load every scope in the array, exactly like the directive does today. The autoPrefixKeys setting should only affect how keys are prefixed, not which scopes get loaded.
To summarize what the implementation should do:
- When
selectTranslatereceives a scope array, forkJoin-load all scopes (unconditionally, regardless of
autoPrefixKeys) - Then resolve the translation key against the appropriate scope
The concerns you raised about it are valid but manageable.
- The "riskier change" is worth it for correctness. What risks do you foresee?
- The extra inline loader wait is a one-time cost per scope, and you already wait for it when working with the pipe/directive.
Let me know if you want to pick this up or if you'd prefer I handle it!
| lang?: string | TranslocoScope | TranslocoScope[], | ||
| lang?: TranslocoScope | TranslocoScope[], |
There was a problem hiding this comment.
The string here was to mark that this also accepts a language. I agree that the string is a bit too general, but please revert it for the time being.
|
Thanks for the reply! As far as I can tell, the auto prefixing functionality only applies to the signal / observable API right now. The directive and pipe do not use auto prefixing as shown here #875. Demo component: Config: Output: t("a.title"): A Title This PRThe goal of this PR is to allow the caller to access all scoped translations with inline loaders via the signal / observable API. Since we can now request the translation without auto prefixing, the only remaining task is to make sure to wait for the required inline loader to load. This can be accomplished by finding the correct loader or, as you found in my previous commit (213423c), waiting for all of them to load. I chose to go with the "find logic" to keep risk to a minimum since the current selectTranslate code already operated with one scope (even if multiple were provided). Feel free to take on the refactor to wait for them all. Also, I think this sounds like a good idea (#866 (comment)):
Might be a bit challenging for people to adopt though since they would have to make a lot of changes in their apps. |
Use the autoPrefixKeys=false configuration to find the best provider scope based on the requested key(s), which will then wait for any inline loaders to resolve before trying to translate.
This also includes a small refactor to move the autoPrefixKeys configuration logic into a private function.
✅ Closes: #875
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #875
Using an app.config.ts with multiple TRANSLOCO_SCOPE providers with inline loaders, you can select translations from either scope using the pipe or directive but only have access to the first scope with the translateSignal API.
What is the new behavior?
To avoid introducing a breaking change but still fix this issue, we can utilize the new autoPrefixKeys configuration property from #868.
If the user has configured their transloco service to not automatically prefix the translation keys with the requested scopes then their translation keys will contain the full path of the translation, including the requested scope. We can utilize that full path to find the correct transloco scope in the providers array to support any inline loaders for that scope.
This PR does not address requesting translations from multiple scopes with inline loaders, if someone needs to do that they should create separate signals.
Does this PR introduce a breaking change?