2015-03-19 53 views
1

我正在使用Angular Translate。這是翻譯一個字符串的最簡單方法,其中一個單詞必須根據單數還是複數變化?像「添加10分」和「添加1分」?角度翻譯:如何處理單數和複數單詞?

+0

你可以隨時嘗試更多的你像「添加點數:5」這樣的通信消息。也許這不是你想要的解決方案,但它很粗糙,準備就緒。 – 2015-03-19 10:21:27

回答

0

從頁面示例中,我嘗試過使用「消息格式」,但添加插值操作會使我的代碼無法工作。問題是在這一行:

$translateProvider.addInterpolation('$translateMessageFormatInterpolation'); 

雖然這不是最優雅的方式得到它,它的工作原理:

app.js

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

app.config(['$translateProvider', function ($translateProvider) { 
$translateProvider.preferredLanguage('en'); 
//$translateProvider.addInterpolation('$translateMessageFormatInterpolation'); 

$translateProvider.translations('en', { 
    POINTS1: '{{val}} point added', 
    POINTSN:'{{val}} points added', 
    POINTS: '{NUM, select, one{point} morethanone{points}} added.' 
}); 

$translateProvider.translations('es', { 
    POINTS1: '{{val}} punto añadido', 
    POINTSN:'{{val}} puntos añadidos', 
    POINTS: '{NUM, select, one{punto} morethanone{puntos} añadidos.' 
}); 
}]); 

app.controller('Ctrl', ['$translate', '$scope', function ($translate, $scope) { 
    $scope.points = 10; 

    $scope.one_point = function() { 
     return points === 1; 
    }; 

    $scope.changeLanguage = function (langKey) { 
     $translate.use(langKey); 
    }; 
}]); 

的index.html

<!doctype html> 
<html ng-app="myApp"> 
<head> 
<!--script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap-dropdown.js"></script--> 
<script src="http://rawgithub.com/SlexAxton/messageformat.js/master/messageformat.js"></script> 
<script src="http://rawgithub.com/SlexAxton/messageformat.js/master/locale/de.js"></script> 
<script src="http://rawgithub.com/SlexAxton/messageformat.js/master/locale/fr.js"></script> 
<script src="http://code.angularjs.org/1.1.5/angular.min.js"></script> 
<script src="http://rawgithub.com/angular/bower-angular-cookies/master/angular-cookies.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate/master/angular-translate.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate-interpolation-messageformat/master/angular-translate-interpolation-messageformat.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate-storage-cookie/master/angular-translate-storage-cookie.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate-storage-local/master/angular-translate-storage-local.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate-loader-static-files/master/angular-translate-loader-static-files.min.js"></script> 
<script src="http://rawgithub.com/angular-translate/bower-angular-translate-handler-log/master/angular-translate-handler-log.min.js"></script> 
<script src="app.js"></script> 
</head> 
<body> 
<div ng-controller="Ctrl"> 
<div ng-show="Ctrl.one_point()"> 
    <p translate="POINTS1" translate-values="{ val: {{points}} }"></p> 
</div> 
<div ng-show="!Ctrl.one_point()"> 
    <p translate="POINTSN" translate-values="{ val: {{points}} }"></p> 
</div> 
<button ng-click="changeLanguage('es')" translate="BUTTON_LANG_ES"></button> 
<button ng-click="changeLanguage('en')" translate="BUTTON_LANG_EN"></button> 
</div> 
</body> 
</html> 
相關問題