Skip to content

Provide a handler to hook in the translation process #1555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
victorlazzaroli opened this issue May 25, 2025 · 2 comments
Open

Provide a handler to hook in the translation process #1555

victorlazzaroli opened this issue May 25, 2025 · 2 comments

Comments

@victorlazzaroli
Copy link

victorlazzaroli commented May 25, 2025

Current behavior

Currently, there is no way to modify an interpolable translation string before interpolation is applied by the parser.

Expected behavior

Introduce a hook or extension point that allows modification of the raw translation value before the interpolation phase begins.
I prefer to not touch the getValue logic because it is something that can put a lot of constraints to internal logic and future features or changes.

What is the motivation / use case for changing the behavior?

Consider the following translation object:

"dictionary": {
   "_": "Dictionary default",
   "key1": "key one {{placeholder}}",
   "key2": "key two"
}

When using the translate service, the entire object under dictionary is passed to the parser, which processes all keys. However, there is no built-in way to manipulate or extract a specific value (e.g., only key1) or even to determine which key to resolve dynamically.

In such cases, being able to hook into the translation process and modify the raw translation value before interpolation would be beneficial.

How do you think that we should implement this?

I’ve submitted a proposed solution in the following PR: #1554

@CodeAndWeb
Copy link
Member

What about using the TranslateCompiler - it provides exactly the way to manipulate the translations after loading.

@victorlazzaroli
Copy link
Author

victorlazzaroli commented May 29, 2025

I considered using the compiler to flatten the tree structure, but the downside is that it would need to process the entire file every time it's loaded.

For this reason, I believe introducing a hook is a more efficient and convenient solution, especially when working with large files. 😊

Here’s the situation we’re facing:

  • Support for multiple languages
  • A very large JSON file where translations are organized as a deeply nested tree structure
  • The translation pipe receives the key of a dictionary object (e.g., "dictionary" in my example), which we cannot change. As a result, instead of returning a translated string, it outputs [object Object] or the stringified dictionary itself

While the compiler could address this by rewriting the JSON files entirely, I believe the proposed hook provides a flexible approach and more performant for such cases, it can improve the consuming of the tree or array structures introduced.
It allows consumers of the library to define the desired behavior without affecting the library’s internal logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants