Last active
June 16, 2016 15:14
-
-
Save vivainio/f214b9d14a353e718024a59b130f28db to your computer and use it in GitHub Desktop.
Typescript replacement for $translate.instant()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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