Giter Club home page Giter Club logo

Comments (3)

knalli avatar knalli commented on May 27, 2024 1

Tip: If you still have to deal with the time until its loaded, you may have a look at $translate.onReady() or translate-cloak.

from angular-translate.

knalli avatar knalli commented on May 27, 2024

Hi @Agustin-Q

that's is interesting finding. The feature is really a couple of years old, but no one has issued this one. Unfortunately, the project more or less deprecated as angular (Angular JS 1.x) itself is deprecated also. Speaking of this, you are aware not using the latest versions?

However, you may look into it for a fix. Yes, it looks like the directive should resolve the well-known (static) defined value, but instead waiting for the async loaded one. On other hand, a service call to $translate(…) should maybe resolve to the right one? Nevertheless, this would be a breaking change, so I would recommend a flag for this.

The directive is calling the service here with already a couple of flags.

$translate(translationId, interpolateParams, translateInterpolation, defaultTranslationText, scope.translateLanguage, scope.sanitizeStrategy)
.then(function (translation) {
applyTranslation(translation, scope, true, translateAttr);
}, function (translationId) {
applyTranslation(translationId, scope, false, translateAttr);
});

... which invokes the service function here:

var $translate = function (translationId, interpolateParams, interpolationId, defaultTranslationText, forceLanguage, sanitizeStrategy) {

In detail, these lines wait for the existing (async) load and could maybe modified not waiting for the load.

var promiseToWaitFor = (function () {
var promise = langPromises[uses] || langPromises[$preferredLanguage];
fallbackIndex = 0;
if ($storageFactory && !promise) {
// looks like there's no pending promise for $preferredLanguage or
// $uses. Maybe there's one pending for a language that comes from
// storage.
var langKey = Storage.get($storageKey);
promise = langPromises[langKey];
if ($fallbackLanguage && $fallbackLanguage.length) {
var index = indexOf($fallbackLanguage, langKey);
// maybe the language from storage is also defined as fallback language
// we increase the fallback language index to not search in that language
// as fallback, since it's probably the first used language
// in that case the index starts after the first element
fallbackIndex = (index === 0) ? 1 : 0;
// but we can make sure to ALWAYS fallback to preferred language at least
if (indexOf($fallbackLanguage, $preferredLanguage) < 0) {
$fallbackLanguage.push($preferredLanguage);
}
}
}
return promise;
}());

In specific this line

var promise = langPromises[uses] || langPromises[$preferredLanguage];

"finds" the existing promise.

Feel free providing the solution here or making up a PR.

Thank you.

from angular-translate.

Agustin-Q avatar Agustin-Q commented on May 27, 2024

Hi @knalli
Thanks for the detailed response I will look into a possible fix.
For the time being I think I can use the following workaround with forceAsyncReload disabled:

app.run(["$translate", function($translate){
  $translate.onReady(function(){
    $translate.refresh($translate.use());
  });
}]);

As I only need to load the async translations once.

Kind regards

from angular-translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.