javascriptangularjsangular-translateangular-transitions

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>


Solution

  • Try Something like this:

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