如何本地化jQuery UI Datepicker?


121

我真的需要一个本地化的下拉日历。英文日历无法在挪威网站上准确传达卓越;-)

我已经对jQuery DatePicker进行了实验,他们的网站说它可以本地化,但是似乎不起作用。

我正在使用ASPNET.MVC,我真的想坚持使用一个JavaScript库。在这种情况下,jQuery。

ajax工具包日历可以接受,只要它也可以显示挪威名称即可。

更新:太棒了!我看到我缺少语言文件,一个不太重要的细节:-)


查看演示。资料来源提供了一个很好的例子。
Thomas Watnedal,2009年

Answers:



79

我找出了演示并通过以下方式实现了该演示:

$.datepicker.setDefaults(
  $.extend(
    {'dateFormat':'dd-mm-yy'},
    $.datepicker.regional['nl']
  )
);

我也需要为dateformat设置默认值...


11
这对我$.datepicker.setDefaults($.datepicker.regional["uk"]);
有用

26

该字符串$.datepicker.regional['it']不能翻译所有单词。

为了转换日期选择器,您必须指定一些变量:

$.datepicker.regional['it'] = {
    closeText: 'Chiudi', // set a close button text
    currentText: 'Oggi', // set today text
    monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',   'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], // set month names
    monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic'], // set short month names
    dayNames: ['Domenica','Luned&#236','Marted&#236','Mercoled&#236','Gioved&#236','Venerd&#236','Sabato'], // set days names
    dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'], // set short day names
    dayNamesMin: ['Do','Lu','Ma','Me','Gio','Ve','Sa'], // set more short days names
    dateFormat: 'dd/mm/yy' // set format date
};

$.datepicker.setDefaults($.datepicker.regional['it']);

$(".datepicker").datepicker();

在这种情况下,您的日期选择器已正确翻译。


此答案显示了如何直接添加本地化而不导入库。这是我需要的方法,因为我正在利用jQuery $.getScript()来获取Google托管API上的主库。
亚历山大·迪克森

11
  $.datepicker.setDefaults({
    closeText: "关闭",
    prevText: "<上月",
    nextText: "下月>",
    currentText: "今天",
    monthNames: [ "一月","二月","三月","四月","五月","六月",
    "七月","八月","九月","十月","十一月","十二月" ],
    monthNamesShort: [ "一月","二月","三月","四月","五月","六月",
    "七月","八月","九月","十月","十一月","十二月" ],
    dayNames: [ "星期日","星期一","星期二","星期三","星期四","星期五","星期六" ],
    dayNamesShort: [ "周日","周一","周二","周三","周四","周五","周六" ],
    dayNamesMin: [ "日","一","二","三","四","五","六" ],
    weekHeader: "周",
    dateFormat: "yy-mm-dd",
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: true,
    yearSuffix: "年"
  });

可以从https://github.com/jquery/jquery-ui/tree/master/ui/i18n复制i18n代码


8

1.您需要加载jQuery UI i18n文件:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/i18n/jquery-ui-i18n.min.js">
</script>

2.使用$.datepicker.setDefaults功能设置所有日期选择器的默认值。

3.如果要在设置默认值之前覆盖设置,可以使用以下方法:

var options = $.extend(
    {},                                  // empty object
    $.datepicker.regional["fr"],         // fr regional
    { dateFormat: "d MM, y" /*, ... */ } // your custom options
);
$.datepicker.setDefaults(options);

由于工作方式,参数的顺序很重要jQuery.extend。两个不正确的示例:

/*
 * This overwrites the global variable itself instead of creating a
 * customized copy of french regional settings
 */
$.extend($.datepicker.regional["fr"], { dateFormat: "d MM, y"});

/*
 * The desired dateFormat is overwritten by french regional 
 * settings' date format
 */
$.extend({ dateFormat: "d MM, y"}, $.datepicker.regional["fr"]);

5

这是没有一些额外库就可以进行本地化的示例。

jQuery(function($) {
  $('input.datetimepicker').datepicker({
    duration: '',
    changeMonth: false,
    changeYear: false,
    yearRange: '2010:2020',
    showTime: false,
    time24h: true
  });

  $.datepicker.regional['cs'] = {
    closeText: 'Zavřít',
    prevText: '&#x3c;Dříve',
    nextText: 'Později&#x3e;',
    currentText: 'Nyní',
    monthNames: ['leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen',
      'září', 'říjen', 'listopad', 'prosinec'
    ],
    monthNamesShort: ['led', 'úno', 'bře', 'dub', 'kvě', 'čer', 'čvc', 'srp', 'zář', 'říj', 'lis', 'pro'],
    dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
    dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
    dayNamesMin: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
    weekHeader: 'Týd',
    dateFormat: 'dd/mm/yy',
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: false,
    yearSuffix: ''
  };

  $.datepicker.setDefaults($.datepicker.regional['cs']);
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link data-require="jqueryui@*" data-semver="1.10.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" />
  <script data-require="jqueryui@*" data-semver="1.10.0" src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/jquery-ui.js"></script>
  <script src="datepicker-cs.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      console.log("test");
      $("#test").datepicker({
        dateFormat: "dd.m.yy",
        minDate: 0,
        showOtherMonths: true,
        firstDay: 1
      });
    });
  </script>
</head>

<body>
  <h1>Here is your datepicker</h1>
  <input id="test" type="text" />
</body>
</html>


5

如果您在同一项目上使用jQuery UI的datepicker和moment.js,则应该使用moment.js的语言环境数据:

// Finnish. you need to include separate locale file for each locale: https://github.com/moment/moment/tree/develop/locale
moment.locale('fi'); 

// fetch locale data internal structure, so we can shove it inside jQuery UI
var momentLocaleData = moment.localeData(); 

$.datepicker.regional['user'] = {
    monthNames: momentLocaleData._months,
    monthNamesShort: momentLocaleData._monthsShort,
    dayNames: momentLocaleData._weekdays,
    dayNamesShort: momentLocaleData._weekdaysMin,
    dayNamesMin: momentLocaleData._weekdaysMin,
    firstDay: momentLocaleData._week.dow,
    dateFormat: 'yy-mm-dd' // "2016-11-22". date formatting tokens are not easily interchangeable between momentjs and jQuery UI (https://github.com/moment/moment/issues/890)
};

$.datepicker.setDefaults($.datepicker.regional['user']);

3

以防万一有人仍然卡在这上,尽管有其他答案,我还是用以下方法解决了:

$.datepicker.setDefaults($.datepicker.regional['en-GB']);

注意额外的“ GB”。之后,它工作正常。


1

此解决方案可能会有所帮助。

$(document).ready(function () {
  var userLang = navigator.language || navigator.userLanguage;

  var options = $.extend({},
    $.datepicker.regional["ja"], {
      dateFormat: "yy/mm/dd",
      changeMonth: true,
      changeYear: true,
      highlightWeek: true
    }
  );

  $("#japaneseCalendar").datepicker(options);
});
#ui-datepicker-div {
  font-size: 14px;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css"
          href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js"></script>
</head>
<body>
<h3>Japanese JQuery UI Datepicker</h3>
<input type="text" id="japaneseCalendar"/>

</body>
</html>


1

我通过添加属性来解决它data-date-language="it"

$(document).ready(function() {
  $('#TxtDaDataDoc_Val').datepicker();
});
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="form-group col-xs-2 col-sm-2 col-md-2">
    <div class="input-group input-append date form-group" 
        id="TxtDaDataDoc" data-date-language="it">
        <input type="text" class="form-control" name="date" 
               id="TxtDaDataDoc_Val" runat="server" />
        <span class="input-group-addon add-on">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>


0
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src=">/js/datepicker-fr.js"></script>
<script>
jQuery(function() {
jQuery( "#datepicker" ).datepicker({minDate: 0 , dateFormat: 'mm/dd/yy'});
});

</script>

<script type="text/javascript">
$(document).ready(function(){
$('#datepicker').datepicker($.datepicker.regional['fr']);
});
</script>

0

$ .datepicker.regional [“ vi-VN”] = {closeText:“Đóng”,prevText:“Trước”,nextText:“ Sau”,currentText:“Hômnay”,monthNames:[“Thángmột”,“Thánghai ”,“Thángba”,“Thángtư”,“Thángnăm”,“Thángsáu”,“Thángbảy”,“Thángtám”,“Thángchín”,“Thángmười”,“Thángmườimột”,“Tháng mườihai“],monthNamesShort:[”Một“,” Hai“,” Ba“,”Bốn“,”Năm“,”Sáu“,”Bảy“,”Tám“,”Chín“,”Mười“,”Mười một“,”Mườihai“],dayNames:[”Chủnhật”,“Thứhai”,“Thứba”,“Thứtư”,“Thứnăm”,“Thứsáu”,“Thứbảy”],dayNamesShort:[“ CN”,“ Hai”,“ Ba”, “Tư”,“Năm”,“Sáu”,“Bảy”],dayNamesMin:[“ CN”,“ T2”,“ T3”,“ T4”,“ T5”,“ T6”,“ T7”]],weekHeader :“Tuần”,dateFormat:“ dd / mm / yy”,firstDay:1,isRTL:false,showMonthAfterYear:false,yearSuffix:“”};dayNamesShort:[“ CN”,“ Hai”,“ Ba”,“Tư”,“Năm”,“Sáu”,“Bảy”],dayNamesMin:[“ CN”,“ T2”,“ T3”,“ T4” ,“ T5”,“ T6”,“ T7”],weekHeader:“Tuần”,dateFormat:“ dd / mm / yy”,firstDay:1,isRTL:false,showMonthAfterYear:false,yearSuffix:“”};dayNamesShort:[“ CN”,“ Hai”,“ Ba”,“Tư”,“Năm”,“Sáu”,“Bảy”],dayNamesMin:[“ CN”,“ T2”,“ T3”,“ T4” ,“ T5”,“ T6”,“ T7”],weekHeader:“Tuần”,dateFormat:“ dd / mm / yy”,firstDay:1,isRTL:false,showMonthAfterYear:false,yearSuffix:“”};weekHeader:“Tuần”,dateFormat:“ dd / mm / yy”,firstDay:1,isRTL:false,showMonthAfterYear:false,yearSuffix:“”};weekHeader:“Tuần”,dateFormat:“ dd / mm / yy”,firstDay:1,isRTL:false,showMonthAfterYear:false,yearSuffix:“”};

        $.datepicker.setDefaults($.datepicker.regional["vi-VN"]);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.