如何翻译多语言的jQuery UI datepicker?

时间:2018-08-08 14:46:06

标签: javascript jquery jquery-ui-datepicker

我有多家商店使用多种语言:en,de,nl,fr等。 我需要翻译jQuery datepicker。 这是我的代码:

jQuery(function(){
    region = jQuery('html').attr('lang');// get the lang code
    jQuery("#scheduled_at").datepicker(
        {
        minDate: +3,
        maxDate: "+3M" ,
        dateFormat : 'dd-mm-yy',
        beforeShowDay: jQuery.datepicker.noWeekends,
        firstDay: 1
    });
});

我看到了这个仓库: https://github.com/jquery/jquery-ui/tree/master/ui/i18n包含所有翻译,但我不需要所有翻译。一些。

翻译样本:

/ * jQuery UI日期选择器插件的英语/英国初始化。 * /

/* Written by Stuart. */
( function( factory ) {
    if ( typeof define === "function" && define.amd ) {

        // AMD. Register as an anonymous module.
        define( [ "../widgets/datepicker" ], factory );
    } else {

        // Browser globals
        factory( jQuery.datepicker );
    }
}( function( datepicker ) {

    datepicker.regional[ "en-GB" ] = {
        closeText: "Done",
        prevText: "Prev",
        nextText: "Next",
        currentText: "Today",
        monthNames: [ "January","February","March","April","May","June",
            "July","August","September","October","November","December" ],
        monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
        dayNames: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
        dayNamesShort: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
        dayNamesMin: [ "Su","Mo","Tu","We","Th","Fr","Sa" ],
        weekHeader: "Wk",
        dateFormat: "dd/mm/yy",
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: "" };
    datepicker.setDefaults( datepicker.regional[ "en-GB" ] );

    return datepicker.regional[ "en-GB" ];

} ) );

如何添加: 就我而言,jQuery.datepicker.regional[region]?对于de,nl,fr和en?

我看到了这篇文章:jQuery Datepicker localization,但这仅适用于一种语言。

谢谢

1 个答案:

答案 0 :(得分:0)

然后您可以使用:

$( selector ).datepicker( $.datepicker.regional[ "en" ] );
$( selector ).datepicker( $.datepicker.regional[ "de" ] );
$( selector ).datepicker( $.datepicker.regional[ "nl" ] );
$( selector ).datepicker( $.datepicker.regional[ "fr" ] );

动态的东西可以使用jQuery变量:

var languages = "en","fr";

 $( selector ).datepicker( $.datepicker.regional[ +languages+ ] );