Cordova国际化

这个插件是用于获取关于用户区域设置语言,日期和时区,货币等的信息。

第1步- 安装全球化插件

打开命令提示符窗口,键入如下所示的代码来安装这个插件。
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-globalization

第2步 - 添加按钮

这里需要添加几个按钮的index.html才能够调用,我们将在稍后创建不同的方法。
<button id = "getLanguage">LANGUAGE</button>
<button id = "getLocaleName">LOCALE NAME</button>
<button id = "getDate">DATE</button>
<button id = "getCurrency">CURRENCY</button>

第3步 - 添加事件监听器

事件侦听器将被添加到index.js文件的DeviceReady函数里面,以确保我们开始使用它之前,应用程序被Cordova 加载。
document.getElementById("getLanguage").addEventListener("click", getLanguage);
document.getElementById("getLocaleName").addEventListener("click", getLocaleName);
document.getElementById("getDate").addEventListener("click", getDate);
document.getElementById("getCurrency").addEventListener("click", getCurrency);

步骤4A - 语言函数

我们使用第一个函数返回户端设备的BCP客47语言标记。这里使用的是 getPreferredLanguage 方法. 该函数有两个参数:onSuccess和onError。我们在 index.js 中使用此函数。

function getLanguage() {
   navigator.globalization.getPreferredLanguage(onSuccess, onError);

   function onSuccess(language) {
      alert('language: ' + language.value + '\n');
   }

   function onError(){
      alert('Error getting language');
   }
	
}
当我们按 LANGUAGE 按钮,弹出框就会显示在屏幕上。

步骤4B - 区域设置函数

该函数返回客户端的区域设置的 BCP 47标记。此函数和我们之前创建的相似。唯一的区别是,我们这次使用的是 getLocaleName 方法。

function getLocaleName() {
   navigator.globalization.getLocaleName(onSuccess, onError);

   function onSuccess(locale) {
      alert('locale: ' + locale.value);
   }

   function onError(){
      alert('Error getting locale');
   }
	
}
当我们点击LOCALE按钮,弹出框将显示我们的区域标记。

步骤4C - Date函数

此功能用于根据用户的语言环境和时区设置返回日期。date 参数是当前日期以及 options参数是可选的。

function getDate() {
   var date = new Date();

   var options = {
      formatLength:'short',
      selector:'date and time'
   }

   navigator.globalization.dateToString(date, onSuccess, onError, options);

   function onSuccess(date) {
      alert('date: ' + date.value);
   }

   function onError(){
      alert('Error getting dateString');
   }
	
}
现在,我们运行应用程序,并按下DATE 按钮来看当前的日期。

最后一个函数功能,将是根据客户设备的设置和 ISO4217 货币代码返回货币值。 

function getCurrency() {
   var currencyCode = 'EUR';
   navigator.globalization.getCurrencyPattern(currencyCode, onSuccess, onError);

   function onSuccess(pattern) {
      alert('pattern: '  + pattern.pattern  + '\n' +
         'code: '     + pattern.code     + '\n' +
         'fraction: ' + pattern.fraction + '\n' +
         'rounding: ' + pattern.rounding + '\n' +
         'decimal: '  + pattern.decimal  + '\n' +
         'grouping: ' + pattern.grouping);
   }

   function onError(){
      alert('Error getting pattern');
   }
	
}
当点击 CURRENCY 按钮会触发弹出框,显示用户的货币模式。

这个插件提供了其他方法。可以看到如下面的表中。
方法 参数 描述
getPreferredLanguage onSuccess, onError
返回客户端的当前语言
getLocaleName onSuccess, onError
返回客户端的当前区域设置
dateToString date, onSuccess, onError, options
根据客户的语言环境和时区返回日期
stringToDate dateString, onSuccess, onError, options
根据客户的设置解析日期
getCurrencyPattern currencyCode, onSuccess, onError
返回客户端的货币模式
getDatePattern onSuccess, onError, options
返回客户端的日期模式
getDateNames onSuccess, onError, options
返回根据客户设置几个月份,几个星期甚至几天的名称数组
isDayLightSavingsTime date, successCallback, errorCallback
用于确定是否夏令时根据客户的时区和日历处于活动状态
getFirstDayOfWeek onSuccess, onError
根据客户端设置返回一周的第一天
numberToString number, onSuccess, onError, options
根据客户端的设置返回数值
stringToNumber string, onSuccess, onError, options
根据客户端设置解析一个数值
getNumberPattern onSuccess, onError, options
根据客户端的设置,返回数值的模式

上一篇: Cordova地理位置 下一篇: Cordova InAppBrowser打开Web浏览器