简体   繁体   English

AngularJS和i18n:在翻译列表项属性后应用ng-repeat过滤器

[英]AngularJS and i18n : apply ng-repeat filters after translating list items properties

JSFiddle : http://jsfiddle.net/X2fsw/2/ JSFiddle: http//jsfiddle.net/X2fsw/2/

I try to create a multilingual AngularJS application using angular-translate . 我尝试使用angular-translate创建一个多语言AngularJS应用程序。

I have a static list of items embedded in my code. 我的代码中嵌入了静态的项目列表。
Each item of this list has a title, and that title has to be displayed in the currently selected language. 此列表中的每个项目都有一个标题,该标题必须以当前选定的语言显示。
Translations are done directly in the view with the help of the translate service. 在翻译服务的帮助下,直接在视图中完成翻译。
Example: {{ myObject.title | translate }} 示例: {{ myObject.title | translate }} {{ myObject.title | translate }} . {{ myObject.title | translate }}

I wish to display the list using ng-repeat , then filter it by item title. 我希望使用ng-repeat显示列表,然后按项目标题过滤它。
However, the filter is applied on the translation key, not on the translated string. 但是,过滤器应用于翻译键,而不是翻译后的字符串。

What would be the best way to correct this behavior while keeping the ability to switch language at runtime? 在保持运行时切换语言的能力的同时,纠正此行为的最佳方法是什么?

I could store the translated string as another property (eg. myObject._title ) on every language change, but my list wouldn't be a constant anymore. 我可以将翻译后的字符串存储为每个语言更改的另一个属性(例如myObject._title ),但我的列表不再是常量。

What do you recommend? 您有什么推荐的吗?

I would consider writing a custom filter. 我会考虑编写自定义过滤器。 This ist described here: http://docs.angularjs.org/guide/filter . 这里描述: http//docs.angularjs.org/guide/filter In the custom filter you could use the $translate service translating your keys to the translated string ( http://pascalprecht.github.io/angular-translate/docs/en/#/guide/03_using-translate-service ) 在自定义过滤器中,您可以使用$ translate服务将您的密钥翻译为已翻译的字符串( http://pascalprecht.github.io/angular-translate/docs/en/#/guide/03_using-translate-service

so based on your fiddle: 所以基于你的小提琴:

myApp.filter('translateFilter', function($translate){
    return function(input, param){
        if(!param){
            return input;
        }
        var searchVal = param.key.toLowerCase();
        var result = [];
        angular.forEach(input, function(value){
            var translated = $translate(value.key);
            if(translated.toLowerCase().indexOf(searchVal)!==-1){
                result.push(value);
            }
        });
        return result;
    };
});

usage: 用法:

<li ng-repeat="day in days | translateFilter:search">
    {{ day.key | translate }}
</li>  

The following code works for me. 以下代码适用于我。 version: AngularJS v1.3.7 版本:AngularJS v1.3.7

    .filter('translateFilter', function($filter){
    return function(input, param){
        if(!param){
            return input;
        }
        var searchVal = param.toLowerCase();
        var result = [];
        angular.forEach(input, function(value){
            /* IMPORTANT */
            // The following "value.label", "label" should be modified to your key
            // which stores the real content
            var translated = $filter('translate')(value.label);
            if(translated.toLowerCase().indexOf(searchVal)!==-1){
                result.push(value);
            }
        });
        return result;
    };
});

This is for array of strings. 这是用于字符串数组。 Every string represents an enum in the server. 每个字符串代表服务器中的枚举。 In this example my translation file looks like this 在这个例子中,我的翻译文件是这样的

EN: "animal":{"C":"Cat","D":"Dog","M":"Mouse"}
SV: "animal:{"C":"Katt","D":"Hund","M":"Mus"}

In my case I orderBy the translated value. 在我的情况下,我命令翻译的价值。 But then still use the 'enum' value. 但是仍然使用'枚举'值。

angular.module('corniche').filter('translateFilter', function($filter){
return function(input, param){
    if(!param){
        return input;
    }
    var $translate = $filter('translate');
    input.sort(function(a, b){
        var textA = $translate(param+'.'+a.toUpperCase());
        var textB = $translate(param+'.'+b.toUpperCase());
        return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
    });
    return input;
};

}); });

in the ng-repeat i need to translate the 'enum' string again, like: 在ng-repeat中我需要再次翻译'enum'字符串,例如:

<li ng-repeat="enum in vm.enums|translateFilter:'animal'">
<div class="checkbox">
 <label>
  <input type="checkbox" ng-model="vm.selected[enum]">
  <span id="enum-label">{{vm.enumResource+'.'+enum|translate}}</span>
 </label>
</div>
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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