jQuery Globalize(原型專案 jquery-glob)讓 JavaScript 輕鬆處理 350+ 種文化的數字、貨幣、日期格式化與解析。支援公曆、回曆、日皇紀年等多種曆法系統。
GitHub:github.com/nje/jquery-glob
同一數字在不同文化顯示不同貨幣符號、小數點、千分位:
日期順序、月份名稱、甚至年份(回曆年)自動轉換:
阿拉伯文化下年份顯示為 1431(回曆年)。
印度泰盧固語 (te-IN) 數字分組為 3-2-2:1,00,00,000(一億)。部分文化不分組。插件自動處理這些細節。
遵循 RFC 4646/5646,格式如 en-US、zh-CN、de-DE。查詢工具:rishida.net/utils/subtags/
jquery.glob.js、目標文化檔(如 jquery.glob.de-DE.js)。$.preferCulture("de-DE")。$.format(value, "c")(c=貨幣, d=短日期, D=長日期, n=數字, p=百分比...)。<script src="jquery.js"></script>
<script src="jquery.glob.js"></script>
<script src="jquery.glob.de-DE.js"></script>
<script>
$.preferCulture("de-DE");
// 價格
$(".price").each(function() {
$(this).text($.format(parseFloat($(this).text()), "c"));
});
// 日期
$(".date").each(function() {
$(this).text($.format(new Date($(this).text()), "D"));
});
// 數字
$(".qty").each(function() {
$(this).text($.format(parseInt($(this).text()), "n"));
});
</script>
輸出:貨幣用 €、日期用德文月份、數字千分位用點號(1.234,56)。