Awad Maharoof
Awad Maharoof

Reputation: 2370

angular translate custom json format

While re-writing an application I decided to use angular translate. I have loaded my translations using the staticFilesLoader

The problem I have is because my translation table is structured in the following format:

{
    "key": "_WelcomeMessage_",
    "value": "Welcome!",
    "description": "This is the welcome message"
}

I do not want to put the effort to change the format of my files as my application is already translated to 3 languages and contains close to 600 key value pairs.

Is there a way I can get past this problem? Thanks in advance.

Upvotes: 1

Views: 492

Answers (1)

lin
lin

Reputation: 18402

Unfortunately, no. If your current logic is like replacing the key value with the next attribute value you have to change your structure like this. You could write a little script which generates you the right output based on your current structure.

{
    "_WelcomeMessage_": "Welcome!",
    "description": "This is the welcome message"
}

Your script could be look like this (done with AngularJS but you could do it with node.js or php .. etc. aswell), fiddle demo:

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {

   $scope.output = {};
   $scope.input = [{
      "key": "_WelcomeMessage_",
      "value": "Welcome!",
      "description": "This is the welcome message"
  },{
      "key": "_anOtherMessage",
      "value": "Message to be fine!",
      "description": "This is the welcome message"
  }];

  angular.forEach($scope.input, function (translationItem) {
        $scope.output[translationItem.key] = translationItem.value;
        $scope.output[translationItem.key + 'description'] = translationItem.description;
  });
});

Upvotes: 1

Related Questions