Permalink
Browse files

Update package name and add getPreferredLanguage API

  • Loading branch information...
1 parent 7d49b76 commit babd132bab7fc747d809343d6381671a60a03bfe mow415 committed Aug 8, 2012
@@ -1,26 +1,29 @@
/**
*
*/
-package com.phonegap.plugins.globalization;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import android.text.format.Time;
-
-import org.apache.cordova.api.Plugin;
-import org.apache.cordova.api.PluginResult;
+package org.apache.cordova.plugins.globalization;
import java.text.DateFormat;
-import java.text.DateFormatSymbols;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
-import java.util.TimeZone;
+import java.util.List;
import java.util.Locale;
+import java.util.Map;
+import java.util.TimeZone;
+
+import org.apache.cordova.api.Plugin;
+import org.apache.cordova.api.PluginResult;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.text.format.Time;
/**
*
@@ -36,7 +39,10 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
if (action.equals(Resources.GETLOCALENAME)){
obj = getLocaleName();
return new PluginResult(status, obj);
- }else if(action.equalsIgnoreCase(Resources.DATETOSTRING)){
+ }else if (action.equals(Resources.GETPREFERREDLANGUAGE)){
+ obj = getPreferredLanguage();
+ return new PluginResult(status, obj);
+ } else if (action.equalsIgnoreCase(Resources.DATETOSTRING)) {
obj = getDateToString(data);
return new PluginResult(PluginResult.Status.OK, obj);
}else if(action.equalsIgnoreCase(Resources.STRINGTODATE)){
@@ -92,6 +98,23 @@ private JSONObject getLocaleName() throws GlobalizationError{
}
}
/*
+ * @Description: Returns the string identifier for the client's current language
+ *
+ * @Return: JSONObject
+ * Object.value {String}: The language identifier
+ *
+ * @throws: GlobalizationError.UNKNOWN_ERROR
+ */
+ private JSONObject getPreferredLanguage() throws GlobalizationError {
+ JSONObject obj = new JSONObject();
+ try {
+ obj.put("value", Locale.getDefault().getDisplayLanguage().toString());
+ return obj;
+ } catch (Exception e) {
+ throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
+ }
+ }
+ /*
* @Description: Returns a date formatted as a string according to the client's user preferences and
* calendar using the time zone of the client.
*
@@ -240,10 +263,9 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
JSONObject obj = new JSONObject();
//String[] value;
JSONArray value = new JSONArray();
- String[] list;
+ List<String> namesList = new ArrayList<String>();
+ final Map<String,Integer> namesMap; // final needed for sorting with anonymous comparator
try{
- SimpleDateFormat s = (SimpleDateFormat)android.text.format.DateFormat.getDateFormat(this.ctx.getContext());
- DateFormatSymbols ds = s.getDateFormatSymbols();
int type = 0; //default wide
int item = 0; //default months
@@ -262,14 +284,31 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
}
//determine return value
int method = item + type;
- if (method == 1){list = ds.getShortMonths();}//months and narrow
- else if (method == 10){list = ds.getWeekdays();}//days and wide
- else if (method == 11){list = ds.getShortWeekdays();}//days and narrow
- else{list = ds.getMonths();}//default: months and wide
+ if (method == 1) { //months and narrow
+ namesMap = Calendar.getInstance().getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
+ } else if (method == 10) { //days and wide
+ namesMap = Calendar.getInstance().getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
+ } else if (method == 11) { //days and narrow
+ namesMap = Calendar.getInstance().getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());
+ } else { //default: months and wide
+ namesMap = Calendar.getInstance().getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
+ }
+
+ // save names as a list
+ for(String name : namesMap.keySet()) {
+ namesList.add(name);
+ }
+
+ // sort the list according to values in namesMap
+ Collections.sort(namesList, new Comparator<String>() {
+ public int compare(String arg0, String arg1) {
+ return namesMap.get(arg0).compareTo(namesMap.get(arg1));
+ }
+ });
- //convert String[] into JSONArray of String objects
- for (int i = 0; i < list.length; i ++){
- value.put(list[i]);
+ // convert nameList into JSONArray of String objects
+ for (int i = 0; i < namesList.size(); i ++){
+ value.put(namesList.get(i));
}
//return array of names
@@ -1,7 +1,7 @@
/**
* User initiated exception
*/
-package com.phonegap.plugins.globalization;
+package org.apache.cordova.plugins.globalization;
/**
* @description Exception class representing defined Globalization error codes
@@ -1,7 +1,7 @@
/**
*
*/
-package com.phonegap.plugins.globalization;
+package org.apache.cordova.plugins.globalization;
/**
* @author costanzo
@@ -20,6 +20,7 @@
public static final String STRINGTONUMBER = "stringToNumber";
public static final String GETNUMBERPATTERN = "getNumberPattern";
public static final String GETCURRENCYPATTERN = "getCurrencyPattern";
+ public static final String GETPREFERREDLANGUAGE = "getPreferredLanguage";
//GlobalizationCommand Option Parameters
public static final String OPTIONS = "options";
@@ -1,7 +1,21 @@
-
-function Globalization()
-{
+var Globalization = function() {
+};
+Globalization.prototype.getPreferredLanguage = function(successCB, failureCB)
+{
+ // successCallback required
+ if (typeof successCB != "function") {
+ console.log("Globalization.getPreferredLanguage Error: successCB is not a function");
+ return;
+ }
+
+ // errorCallback required
+ if (typeof failureCB != "function") {
+ console.log("Globalization.getPreferredLanguage Error: failureCB is not a function");
+ return;
+ }
+
+ cordova.exec(successCB, failureCB, "GlobalizationCommand","getPreferredLanguage", []);
};
/**
@@ -173,7 +187,7 @@ Globalization.prototype.stringToDate = function(dateString, successCB, failureCB
* @error GlobalizationError.PATTERN_ERROR
*
* Example
-* globalization.getDatePattern(new Date(),
+* globalization.getDatePattern(
* function (date) {alert('pattern:' + date.pattern + '\n');},
* function () {},
* {formatLength:'short'});
@@ -506,10 +520,6 @@ Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, f
console.log("Globalization.getCurrencyPattern Error: currencyCode is not a currency code");
}
};
-cordova.addConstructor(function()
-{
- cordova.addPlugin('globalization', new Globalization());
-});
GlobalizationError = function() {
this.code = null;
@@ -520,3 +530,11 @@ GlobalizationError.UNKNOWN_ERROR = 0;
GlobalizationError.FORMATTING_ERROR = 1;
GlobalizationError.PARSING_ERROR = 2;
GlobalizationError.PATTERN_ERROR = 3;
+
+
+if(!window.plugins) {
+ window.plugins = {};
+}
+if (!window.plugins.globalization) {
+ window.plugins.globalization = new Globalization();
+}

0 comments on commit babd132

Please sign in to comment.