question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

translate is undefined. I want to use it as a pure function.

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mohamedaarab1994commented, Dec 9, 2019

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.

0reactions
itayodcommented, Dec 10, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found