我如何本地化jQuery UI Datepicker?

我真的需要一个本地化的下拉日历。 英文日历在挪威网站上并不完全expression出色;-)

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

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

ajax工具包日历是可以接受的,只要它也能显示挪威的名字。

更新:太棒了! 我看到我错过了语言文件,一个不那么简单的细节:-)

对于那些仍然有问题的人,你必须从这里下载你想要的语言文件:

https://github.com/jquery/jquery-ui/tree/master/ui/i18n

然后将其包含在您的网页中,例如(意大利语):

<script type="text/javascript" src="/scripts/jquery.ui.datepicker-it.js"></script> 

然后使用zilverdistel的代码 :D

我想出了演示,并以下列方式实现它:

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

我也需要设置dateformat的默认值…

string$.datepicker.regional['it']不能翻译所有的单词。

为了翻译dateselect器,你必须指定一些variables:

 $.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(); 

在这种情况下,你的datepicker被正确翻译。

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

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

2.使用$.datepicker.setDefaults函数为所有dateselect器设置默认值。

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

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

这里是一个例子,你可以如何做本地化没有一些额外的库。

 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> 

如果您在同一个项目中使用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']); 

只是为了防止任何人仍然坚持这一点,尽pipe其他答案,我解决这个问题:

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

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