Cordova-globalisation

提供:Dev Guides
移動先:案内検索

Cordova-グローバリゼーション

このプラグインは、ユーザーのロケール言語、日付と時間帯、通貨などに関する情報を取得するために使用されます。

手順1-グローバリゼーションプラグインのインストール

  • コマンドプロンプト*を開き、次のコードを入力してプラグインをインストールします
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-globalization

ステップ2-ボタンを追加する

後で作成するさまざまなメソッドを呼び出せるように、いくつかのボタンを indexl に追加します。

<button id = "getLanguage">LANGUAGE</button>
<button id = "getLocaleName">LOCALE NAME</button>
<button id = "getDate">DATE</button>
<button id = "getCurrency">CURRENCY</button>

手順3-イベントリスナーの追加

イベントリスナーは、 index.js ファイルの getDeviceReady 関数内に追加され、使用を開始する前にアプリと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 の2つのパラメーターがあります。 この関数を index.js に追加しています。

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

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

   function onError(){
      alert('Error getting language');
   }
}
*LANGUAGE* ボタンを押すと、画面にアラートが表示されます。

Cordova Globalization 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* ボタンをクリックすると、アラートにロケールタグが表示されます。

Cordova Globalization Locale

ステップ4C-日付関数

この関数は、クライアントのロケールとタイムゾーン設定に従って日付を返すために使用されます。 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 ボタンを押して現在の日付を確認できます。

Cordova Globalization Date

最後に示す関数は、クライアントのデバイス設定とISO 4217通貨コードに従って通貨値を返すことです。 概念が同じであることがわかります。

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* ボタンは、ユーザーの通貨パターンを示すアラートをトリガーします。

コルドバグローバリゼーション通貨

このプラグインは他の方法を提供します。 以下の表ですべて見ることができます。

method parameters details
getPreferredLanguage onSuccess, onError Returns client’s current language.
getLocaleName onSuccess, onError Returns client’s current locale settings.
dateToString date, onSuccess, onError, options Returns date according to client’s locale and timezone.
stringToDate dateString, onSuccess, onError, options Parses a date according to client’s settings.
getCurrencyPattern currencyCode, onSuccess, onError Returns client’s currency pattern.
getDatePattern onSuccess, onError, options Returns client’s date pattern.
getDateNames onSuccess, onError, options Returns an array of names of the months, weeks or days according to client’s settings.
isDayLightSavingsTime date, successCallback, errorCallback Used to determine if the daylight saving time is active according to client’s time zone and calendar.
getFirstDayOfWeek onSuccess, onError Returns the first day of the week according to client settings.
numberToString number, onSuccess, onError, options Returns number according to client’s settings.
stringToNumber string, onSuccess, onError, options Parses a number according to client’s settings.
getNumberPattern onSuccess, onError, options Returns the number pattern according to client’s settings.