2016-02-12 51 views
1

我想知道在我的练习中是否有任何不好的气味对于国际化的Angular。js i18n角度更好的练习

我把上的I18n角控制器转换功能(因为我不知道如何把它放在HTML模板文件)

约在国际化的范围,我用这种方式I18n.t("city." + city_name),表明是CITY_NAME在“城市”范围内。我可以用这种方式写出I18n.t(city_name, scope: "city"),以使它更易于理解。

我很感谢每一个评论和建议,以加强我目前的解决方案。

数据结构

departures_lst是国家的英文名称e.g的列表,:[美国,中国,日本]

每个国家都有许多城市的名字。例如[纽约,洛杉矶,波士顿]

角控制器

App.controller("departures_ctrl", function($scope, $location, $http) { 
    $http.get("/departures.json") 
    .success(function (response) { 
     $scope.departures_lst = response; 
    }); 
    $scope.get_destinations = function(city_name) { 
      return $location.url("/depart_from/" + city_name); 
    }; 
    $scope.i18nCountryName = function(country_name) { 
      return I18n.t("country." + country_name) + country_name 
    }; 
    $scope.i18nCityName = function(city_name) { 
      return I18n.t("city." + city_name) + city_name 
    }; 
}); 

HTML teamplate?

<div class="panel panel-transparent" ng-repeat="departure in departures_lst"> 
    <h5>{{i18nCountryName(departure.country)}}</h5> 
    <li class="col-sm-3 col-lg-3 col-xs-3" ng-repeat="city in departure.cities"> 
     <a ng-click="get_destinations(city)"> 
     <i class="fa fa-plane"></i> 
     {{i18nCityName(city)}} 
     </a> 
    </li> 
</div> 
+0

这大概是最好[代码审查(http://codereview.stackexchange.com/) – DrewJordan

回答

0

你应该总是试着把本地化放在标记中(尽可能),这样你可以进一步从你的逻辑和数据中封装布局。我一直使用伟大的角度定位插件。你会得到一些伟大的过滤器和指令来玩与参数化和其他功能内置。例如:

你有一个字符串,你必须插入名称一个用户,您可以定义您的本地化文件中的字符串为:

"some-loc-id-tag": "insert the user name {user} here!" 

而在你的控制范围内具有这样的性质:

$scope.model.userName 

,可以与像这样的本地化字符串显示用户名在HTML中:

<div data-i18n="loc.some-loc-id-tag" data-user="{{model.userName}}"></div> 

检查出来: https://github.com/doshprompt/angular-localization