Angular-Translate与自定义过滤器

时间:2016-02-17 13:37:45

标签: angularjs filter translate angular-translate

如何使用我自己的自定义过滤器进行格式设置,比如说星期几不是星期日,星期一......而是使用我的$translate服务。例如

app.filter('formatDate', ['$translate', function ($translate) {
return function (date) {
    var now = new Date();
    var date = new Date(date);
    var monthNames = [
        "Jan", "Feb", "March",
        "Apr", "May", "June", "July",
        "Aug", "Sept", "Oct",
        "Nov", "Dec"
    ];
    dayNames = [
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    ];

    var formattedDate;
    ...
    ...

    if (now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getFullYear() == date.getFullYear()) {
        if (diffSeconds < 120) {
            formattedDate = 'few seconds ago';
        }
        else {
            if (diffSeconds <= 300) {
                formattedDate = Math.round(diffSeconds / 60) + ' minutes ago';
            }
            else {
                formattedDate ='at '+hours + ':' + minutes + ampm;
            }
        }
    }
    else {
        if(diffDays == 1)
            formattedDate = 'yesterday';
        else {
            if (diffDays < 7){
                formattedDate = 'on ' + dayNames[date.getDay()];
            }
            else{
                formattedDate = monthNames[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();
                }
            }
        }

    return formattedDate;
};
}]);

如何让数组dayNames在几周内使用我的翻译天数?

1 个答案:

答案 0 :(得分:1)

我用$translate.instant()来解决这个问题......

dayNames = [
    $translate.instant("Sunday"), 
    $translate.instant("Monday"), 
    $translate.instant("Tuesday"), 
    $translate.instant("Wednesday"), 
    $translate.instant("Thursday"), 
    $translate.instant("Friday"), 
    $translate.instant("Saturday")
];