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.

Add check for presence of a translation

See original GitHub issue

Current behavior

There is no way to be certain about the existence of a translation for a specific key. The best thing I can do is checking if the translation is the same as the given key: if (this.translate.instant(myKey) === myKey) { ... } However, besides not being pretty, this cannot detect the case when the translation really is the same as the key.

Expected behavior

It would be nice to have a safe and concise test for the presence of a translation.

How do you think that we should implement this?

if(this.translate.hasTranslation(key)) { ... }

Nice bonus: Give it an optional argument for testing another language instead of the current: hasTranslation(translationKey: string, language?: string): boolean

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:26
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
skidrow88commented, Dec 7, 2021

Another way to do it:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) { }


public hasTranslation(translationKey: string): boolean {
    let isTranslated: boolean = true;
    if (this.translate.instant(translationKey) == translationKey){
      isTranslated = false;
    }
    return isTranslated;
  }
2reactions
M4tizcommented, Oct 8, 2020

A slightly improved version of @spixy where you can reference deep keys via dot notation eg. VERY.DEEP.TRANSLATION

import { TranslateService } from '@ngx-translate/core';

constructor(private translateService: TranslateService) { }
 

public hasTranslation(translationKey: string, language?: string): boolean {
        const translation: object | string = this.translateService.translations[language || this.translateService.currentLang];
        const value = key
            .split('.')
            .reduce((t, k) => t[k] || {}, translation || {});
        return value !== undefined;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a function to check the existence of a translation in ...
I need to check if a translation for the plugin currently exists, otherwise present a fall-back for a limited number of languages. Example:...
Read more >
Types of Quality Checks
Check that insertables in the source are correctly transferred to the translation. Insertables are unicode characters such as new line hyphens. The insertable ......
Read more >
Checks and fixups
The quality checks help catch common translator errors, ensuring the translation is in good shape. The checks can be ignored in case of...
Read more >
Translate and localize your app - Play Console Help
If you add text translations without localized graphic assets, your app's ... To view your app in another language and check your app's...
Read more >
Improve Translate
Contributing is a major part of our process to add new languages to Google Translate. If you speak a language that we're working...
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