translate is undefined. I want to use it as a pure function.
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[X ] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
Current behavior
[transloco DOC] You don’t have to inject the service each time you need to translate a key. Transloco has an exported translate() function:
translate
provided as a pure function does not work. Sometimes it works, but when I’m not sure, I refresh the browser and get an error.
let service: TranslocoService;
export function translate<T = any>(key: TranslateParams, params: HashMap = {}, lang?: string): T {
// service is undefined
return service.translate(key, params, lang);
}
Expected behavior
I want to use it as a pure function.
Minimal reproduction of the problem with instructions
For bug reports please provide the STEPS TO REPRODUCE and if possible a MINIMAL DEMO of the problem, for that you could use our stackblitz example
What is the motivation / use case for changing the behavior?
Environment
Angular version: X.Y.Z
Browser:
- [ X] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Angular Translate: $translate undefined in controller
This function calls '$translate.use'-method inside the controller. Funny enough, this works for the main-page but not for all other controllers ...
Read more >PHP Fatal error: Call to undefined function ... - Drupal
When I try to use the "translate" button for a view, I get the following error message: PHP Fatal error: Call to undefined...
Read more >What are Pure Functions and Side Effects in JavaScript?
Pure functions and side effects are well-known concepts in functional programming. These concepts are widely used in JavaScript as well.
Read more >DCL60-CPP. Obey the one-definition rule
An inline function shall be defined in every translation unit in which it is odr-used. The most common approach to multitranslation unit compilation...
Read more >Translating text (Advanced) - Google Cloud
Before you can start using the Cloud Translation API, you must have a ... when attempting to translate content with other forms of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m having the same issue. Sometimes it works and sometimes it doesn’t. When refreshing the page, the value is always either undefined or it shows the name of the key.
From what I have saw in your StackBlitz, you try to use the translate before the service beed initialized. You must wait until you initial the service in order to use this method