Skip to content
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

Allow to override getValue method #1510

Open
JulienBier opened this issue Dec 12, 2024 · 4 comments
Open

Allow to override getValue method #1510

JulienBier opened this issue Dec 12, 2024 · 4 comments

Comments

@JulienBier
Copy link

Current behavior

Cannot override getValue() method

Expected behavior

Define my own getValue() method

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

In the previous version 15.0.0, I could override the getValue method in TranslateDefaultParser, to have my own local override implementation. But in the version 16.0.0, the getValue() cannot be overriden anymore
https://ngx-translate.org/getting-started/migration-guide/#24-translateparser-getvalue

How do you think that we should implement this?

Is there a way to inject my own getValue method in the app ?

Thank you for that nice library.

@CodeAndWeb
Copy link
Member

Currently there's no way to override it.

I've removed it from the parser because the idea of the parser is about parsing/interpolating the translation message.

The getValue() method was misplaced in this class - and even worse was used for 2 purposes:

  • finding a specific translation in your translation data
  • finding an interpolation value in a parameter

Which of the functionality do you want to override?

@JulienBier
Copy link
Author

The goal is to be able to override a key.
For example, we have keys defined as such:
'shared.component.button.label' and we want, in some cases depending on the context, to have an override as such:
'overrides.some_key.shared.component.button.label'
And the best place to do that seemed to me to be the getValue() method.

@CodeAndWeb
Copy link
Member

That is indeed not accessible at the moment.

The probably best way to do that currently would be in the loader by merging both objects into a single one, where the overrides "win".

I might also come up with a new class that will be responsible for storing and accessing the translations instead of keeping that code spread in the service itself.

@JulienBier
Copy link
Author

The probably best way to do that currently would be in the loader by merging both objects into a single one, where the overrides "win".

The problem is that we want to have the overrides or not, without having to reload the locales.

I might also come up with a new class that will be responsible for storing and accessing the translations instead of keeping that code spread in the service itself.

Having a service for this would be amazing.

In the meantime, I will stay with the previous version and continue to think about an alternative.

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