javascript – 如何加载JSON数组以使用Angular-Translate转换AngularJS网页

我仍然在学习AngularJS,所以请原谅我所有糟糕的质量代码/缺乏知识.

我想做的事

我想将我的网页翻译成2种语言.当我在index.html中使用Angular-Translate翻译静态内容时,它目前有效.像这样

function translateConfiguration($translateProvider) {

    $translateProvider.useSanitizeValueStrategy(null);

    $translateProvider.useStaticFilesLoader({
      files: [{
        prefix: '../JSON/locale-',
        suffix: '.json'
      }]
});

$translateProvider.preferredLanguage('en');
$translateProvider.fallbackLanguage('en');

};

我为EN和RU语言环境加载我的简单JSON文件,结构如下

{
 "HEADING" : "Lorem",
 "TEXT" : "Ipsum"
}

然后我通过Angular-translate指令访问变量

<h1 class="z-logo header-text md-display-1" translate="HEADING">
</h1>

一切正常.

我希望能够对我的自定义指令和ng-repeat执行相同的操作.

问题

我的AngularJS网站上有多个自定义指令.例如

<about-card flex="33" flex-xs="100" flex-sm="100" class="center-content" ng-repeat="data in aboutCardCtrl.info">
</about-card>

以下是通过custom指令加载并从JSON文件中检索信息的模板代码

<md-card class="card-bg card-height">
  <md-card-title layout="column" class="about-card">
    <md-card-title-media class="center-elements">
      <img class="md-media-lg" ng-src="{{data.image}}" alt="{{data.imageAlt}}"/>
    </md-card-title-media>
    <md-card-title-text>
      <div class="md-headline card-title-padding">{{data.heading}}</div>
    </md-card-title-text>
  </md-card-title>
  <md-card-content>
    {{data.content | translate}}
  </md-card-content>
</md-card>

这是指令

(function() {
  'use strict'

  angular
    .module('webApp')
    .directive('aboutCard', aboutCard);

  function aboutCard() {
    return {
      restrict: 'EA',
      priority: 1001,
      templateUrl: '../TEMPLATES/about.html',
      controller: 'aboutCardController',
      controllerAs: 'aboutCardCtrl'
    };
  };
})();

这是控制器

(function() {
  'use strict'

  angular
    .module('webApp')
    .controller('aboutCardController', aboutCardController);

  aboutCardController.$inject = ['JsonData', '$translate'];

  function aboutCardController(JsonData, $translate) {
    var vm = this;

    vm.pathToJson = '../JSON/about-en.json';
    vm.info = [];

    JsonData.all(vm.pathToJson).then(function(response) {
      vm.info = response.data.information;
    });
  };
})();

这是JSON文件

  {
      "information": [{
        "heading": "Reliability",
        "content": "Our partners have been working on the market for over 10 years",
        "image": "../IMG/shield.svg",
        "imageAlt": "Reliability Image"
      }, {
        "heading": "Professionalism",
        "content": "We are ready to provide profesional opinion for our clients to ensure the right choice",
        "image": "../IMG/people.svg",
        "imageAlt": "Professionalism Image"
      }, {
        "heading": "Development",
        "content": "Organization of educational programs in collaboration with our partners",
        "image": "../IMG/cogwheel.svg",
        "imageAlt": "Development Image"
      }]
    }

在这里,我想我缺乏智慧来理解如何加载我的JSON文件并在自定义指令中使用ng-repeat在它们之间切换.由于JSON的格式不同.

我一直在浏览Angular-Translate维基页面,但所有示例仅包含index.html和主app.js文件,我似乎无法找到/理解谷歌搜索中发现的示例

https://technpol.wordpress.com/2013/11/02/adding-translation-using-angular-translate-to-an-angularjs-app/

https://github.com/angular-translate/angular-translate/issues/1154

最佳答案 您可以使用动态加载程序而不是静态.

$translateProvider.useLoader('$translatePartialLoader', {
  urlTemplate: 'JSON/{part}/{lang}.json'
});

然后像这样加载你需要的JSON:

$translatePartialLoader.addPart('about');

语言前缀将自动添加到路径中.

点赞