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 on missing key does not return the key, but a failed promise

See original GitHub issue

Subject of the issue

The documentation says “If [the translation id] doesn’t exist it’ll probably just return the translation id.”

On that basis, I would expect a call like:

$translate("MISSING_KEY").then(function(txt) { console.log(txt); })

to log “MISSING_KEY”.

However, it does nothing at all, because the promise returned from $translate is completed with a failure.

The correct code would be:

$translate("MISSING_KEY").then(function(txt) { console.log(txt); }, function(txt) { console.log(txt); })

as you can see in the “it('should return translation id if translation doesn\'t exist'” test at translate.spec.js:276

This means that the documentation is misleading and makes code that uses the service difficult to write correctly.

For example, this code in ng-admin will return nothing at all if the “errorMessage” translation key is not found:

            $translate(errorMessage, {
                status: error && error.status,
                details: error && error.data && typeof error.data === 'object' ? JSON.stringify(error.data) : {}
            }).then(text => notification.log(text, { addnCls: 'humane-flatty-error' }));

… because the author did not anticipate that the promise returned by $translate could fail. (I will raise a separate bug with ng-admin to try to fix this.)

Your environment

  • version of angular-translate: 2.11.0
  • version of angular: 1.4.8
  • which browser and its version: n/a

Steps to reproduce

Reproduced in the unit test linked above.

Expected behaviour

The promise returned by $translate("MISSING_KEY") should complete with the value "MISSING_KEY"

Actual behaviour

The promise returned by $translate("MISSING_KEY") does not complete, but fails with an error value of "MISSING_KEY"

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
reda-alaouicommented, Sep 27, 2016

Hello,

It seems that this behaviour appeared between 2.4.1 and 2.11.0 We discovered it when updating angular translate library.

We have a process that call angular-translate for a bunch of keys for translation. We store all related promises in an array and then call $q.all on it.

Before, when one of the key was missing, the promise was resolved anyway with the key of each missing translation. Now, the rejection of one promise implies the rejection of all promises.

Moreover, I think this is akward since the filter does not behave this way. It just returns the key when the translation is missing.

Is it possible to fallback to previous behaviour by introducing a setting for example?

0reactions
knallicommented, Jun 28, 2016

Well, I think the (new) promise document by mdn is only slightly older than our release of 2.0 😃 Said this, I want to say this is a style which we have introduced some time ago (finally released early 2014).

There are already some things perhaps everyone would change in a new system (see #995), but the api (v2) is fixed.

So, basically: Yes. Would be better. But not in v2 any more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation key not returned by $translate service ... - GitHub
Hi When using $translate service, the rejected promise doesn't include the translation key.
Read more >
translate.get sometimes returns key instead of translation
get function to fetch the translation from root keys and returns the key itself on failed. So, to resolve this issue you have...
Read more >
Improve Google Translate
Improve Translate​​ Help improve Google Translate for the languages that you speak. Contribute to help people around the world understand your language better....
Read more >
Ngx-translate: outputs key instead of translated string - ionic-v3
I've added the onLangChange event and the translations object returns empty. So it would appear that it is not retrieving any translations. 2 ......
Read more >
Translation - React-admin - Marmelab
For react-admin, the “locale” is just a key for your i18nProvider, so it can have ... translate: (key, options) => string, changeLocale: locale...
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