Skip to content

Instantly share code, notes, and snippets.

@vivainio
Last active June 16, 2016 15:14
Show Gist options
  • Select an option

  • Save vivainio/f214b9d14a353e718024a59b130f28db to your computer and use it in GitHub Desktop.

Select an option

Save vivainio/f214b9d14a353e718024a59b130f28db to your computer and use it in GitHub Desktop.
Typescript replacement for $translate.instant()
class CheckedTranslate {
static $inject = ['$rootScope', '$translate']
constructor(private $rootScope: ng.IRootScopeService, private $translate: ng.translate.ITranslateService) {}
translate(localizations: any) {
// obviously this mutates passed localizations object behind your back
this.$rootScope.$on('$translateChangeSuccess', () => {
for (var key in localizations) {
localizations[key] = this.$translate.instant(localizations[key]);
}
})
return localizations;
}
}
angular.module('app').service('CheckedTranslate', CheckedTranslate);
// app code
// declare your localizations with convenient key
const locs = {
imagesLabel: "Views.DocumentsPage.Images_Label"
}
// in controller ctor:
// this.locs = CheckedTranslate.translate(locs);
// somewhere in controller (with existence check!):
// var s = locs.imagesLabel
// in template:
// <span ng-bind="$ctrl.locs.imagesLabel"></span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment