简体   繁体   English

如何根据浏览器语言设置Jquery UI Datepicker日期格式?

[英]How to set Jquery UI Datepicker date format based on browser language?

I want to set Jquery UI DatePicker date format from browser language, for example if "English (United Kingdom)" in Google Chrome is on top in Language and input settings... (chrome://settings/languages) then date format would be dd/mm/yyyy and if "English (United States)" is on the top of the list then date format should be mm/dd/yyyy . 我想从浏览器语言设置Jquery UI DatePicker日期格式,例如,如果Google Chrome中的“英语(英国)”位于语言和输入设置的顶部... (chrome:// settings / languages),那么日期格式会是dd / mm / yyyy ,如果“英语(美国)”位于列表的顶部,则日期格式应为mm / dd / yyyy Is there any way to set Date Format from Browser Language in Chrome, Firefox and IE...? 有没有办法在Chrome,Firefox和IE浏览器语言中设置日期格式......?

In your case I would suggest to check the language like 在你的情况下,我建议检查语言

var userLang = navigator.language || navigator.userLanguage; 
// detect browser language but not a decent one 
if (userLang === "en-US") {
  $( ".selector" ).datepicker( "option", "dateFormat", "mm/dd/yyyy" );
} else {
  $( ".selector" ).datepicker( "option", "dateFormat", "dd/mm/yyyy" );
}

Update: 更新:

I was looking into localization of jQuery datepicker, and here is a nice discussion and a nice plugin for it. 我正在研究jQuery datepicker的本地化,这是一个很好的讨论和一个很好的插件。

jQuery-localization , which detects the language and loads the respective regional language for it jQuery-localization ,它检测语言并为其加载相应的区域语言

$.localise('js/jquery.ui.datepicker');

Note: By default, it looks at the locale set for the browser and requests localisations of the specified package based on that . 注意: 默认情况下,它会查看为浏览器设置的区域设置,并根据该区域设置请求指定包的本地化 For example, if the locale is Portuguese/Brazil (pt-BR), it will load js/jquery.ui.datepicker-pt.js and js/jquery.ui.datepicker-pt-BR.js. 例如,如果语言环境是葡萄牙语/巴西语(pt-BR),则它将加载js / jquery.ui.datepicker-pt.js和js / jquery.ui.datepicker-pt-BR.js。

This is how it's done in the current project I'm working on. 这就是我正在进行的当前项目中的工作方式。 Not sure if it's the best approach as I believe there are language files available in jQuery UI, however ... 不确定这是否是最好的方法,因为我相信jQuery UI中有语言文件,但是......

var cfg = {
    container: '.datepicker',
    regional: {
        nl: {
            closeText: 'Sluiten',
            prevText: '←',
            nextText: '→',
            currentText: 'Vandaag',
            monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
            monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
            dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
            dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
            dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
            weekHeader: 'Wk',
            dateFormat: 'dd-mm-yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        }
    }
};

// extracted activation function
$.datepicker.regional = $.extend({}, $.datepicker.regional, cfg.regional);
$.datepicker.setDefaults($.datepicker.regional.nl);
this.datepicker.datepicker();

So you can add languages in the cfg object and choose a custom format. 因此,您可以在cfg对象中添加语言并选择自定义格式。 Only thing left to do is make the language, passed to datepicker() , dynamic. 剩下要做的就是将语言传递给datepicker() ,动态。 Perhaps from the URL, cookie, session, server variable, ... depending on how your project uses languages. 也许来自URL,cookie,会话,服务器变量,......取决于您的项目如何使用语言。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM