Skip to content

Instantly share code, notes, and snippets.

@cburgmer
Created October 18, 2013 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cburgmer/7038779 to your computer and use it in GitHub Desktop.
Save cburgmer/7038779 to your computer and use it in GitHub Desktop.
Simple i18next translation filter for angular This solves async issues with the filter accessing the translation function before i18next is ready. The solution here is not optimal but clearly maps out the solution, something that https://gist.github.com/archer96/5239617 or https://github.com/archer96/ng-i18next/blob/master/src/provider.js fails …
angular.module('myApp')
.run(['$rootScope', function ($rootScope) {
window.i18n.init(options, function () {
// When finished loading translations, trigger re-evaluation of views for translations
$rootScope.$digest();
});
}])
.filter('translate', [function(){
return function(key, params) {
// i18next needs time to initialize (loading translations). In this phase translation does not work
try {
return i18n.t(key, params);
} catch (e) {}
};
}]);
@cburgmer
Copy link
Author

Basically that solves the following error: "i18next not finished initialization. you might have called t function before loading resources finished."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment