简体   繁体   中英

Angular Translation using key as a variable Not working

I am working on an Angular Js app and use the Angular-translate module

In one case the translation may have its key as a variable so I created an object in the translations object with all possible variables .

when I use it with dot notation like translate='status.awaiting' it works good but If I used it as a variable with bracket notation like translate='status[status]' it didn't work at all!

here is a minimal example of what I got .. thanks in advance!

 angular.module('myApp',['pascalprecht.translate']) .config(function($translateProvider){ $translateProvider.useSanitizeValueStrategy(null); $translateProvider .translations('ar', { status:{ "new":"جديد", "awaiting":"منتظر", "confirmed":"مؤكد", "shipped":"مشحون", "delivered":"مستلم", "returned":"مرتجع", "canceled":"ملغى" } }) .preferredLanguage('ar'); }) .controller('myCtrl',function($scope){ $scope.current_status = 'awaiting'; }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate/2.15.1/angular-translate.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div translate='status.awaiting'> </div> <div>{{current_status}} </div> <div translate='status[current_status]'> </div> </div> 

试试这样的事情:

  <div  translate='status.{{current_status}}'> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM