Skip to content
This repository
Browse code

Update package name and add getPreferredLanguage API

  • Loading branch information...
commit babd132bab7fc747d809343d6381671a60a03bfe 1 parent 7d49b76
mow415 authored
85 Android/Globalization/GlobalizationCommand.java
... ... @@ -1,26 +1,29 @@
1 1 /**
2 2 *
3 3 */
4   -package com.phonegap.plugins.globalization;
5   -
6   -import org.json.JSONArray;
7   -import org.json.JSONException;
8   -import org.json.JSONObject;
9   -
10   -import android.text.format.Time;
11   -
12   -import org.apache.cordova.api.Plugin;
13   -import org.apache.cordova.api.PluginResult;
  4 +package org.apache.cordova.plugins.globalization;
14 5
15 6 import java.text.DateFormat;
16   -import java.text.DateFormatSymbols;
17 7 import java.text.DecimalFormat;
18 8 import java.text.SimpleDateFormat;
  9 +import java.util.ArrayList;
19 10 import java.util.Calendar;
  11 +import java.util.Collections;
  12 +import java.util.Comparator;
20 13 import java.util.Currency;
21 14 import java.util.Date;
22   -import java.util.TimeZone;
  15 +import java.util.List;
23 16 import java.util.Locale;
  17 +import java.util.Map;
  18 +import java.util.TimeZone;
  19 +
  20 +import org.apache.cordova.api.Plugin;
  21 +import org.apache.cordova.api.PluginResult;
  22 +import org.json.JSONArray;
  23 +import org.json.JSONException;
  24 +import org.json.JSONObject;
  25 +
  26 +import android.text.format.Time;
24 27
25 28 /**
26 29 *
@@ -36,7 +39,10 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
36 39 if (action.equals(Resources.GETLOCALENAME)){
37 40 obj = getLocaleName();
38 41 return new PluginResult(status, obj);
39   - }else if(action.equalsIgnoreCase(Resources.DATETOSTRING)){
  42 + }else if (action.equals(Resources.GETPREFERREDLANGUAGE)){
  43 + obj = getPreferredLanguage();
  44 + return new PluginResult(status, obj);
  45 + } else if (action.equalsIgnoreCase(Resources.DATETOSTRING)) {
40 46 obj = getDateToString(data);
41 47 return new PluginResult(PluginResult.Status.OK, obj);
42 48 }else if(action.equalsIgnoreCase(Resources.STRINGTODATE)){
@@ -92,6 +98,23 @@ private JSONObject getLocaleName() throws GlobalizationError{
92 98 }
93 99 }
94 100 /*
  101 + * @Description: Returns the string identifier for the client's current language
  102 + *
  103 + * @Return: JSONObject
  104 + * Object.value {String}: The language identifier
  105 + *
  106 + * @throws: GlobalizationError.UNKNOWN_ERROR
  107 + */
  108 + private JSONObject getPreferredLanguage() throws GlobalizationError {
  109 + JSONObject obj = new JSONObject();
  110 + try {
  111 + obj.put("value", Locale.getDefault().getDisplayLanguage().toString());
  112 + return obj;
  113 + } catch (Exception e) {
  114 + throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
  115 + }
  116 + }
  117 + /*
95 118 * @Description: Returns a date formatted as a string according to the client's user preferences and
96 119 * calendar using the time zone of the client.
97 120 *
@@ -240,10 +263,9 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
240 263 JSONObject obj = new JSONObject();
241 264 //String[] value;
242 265 JSONArray value = new JSONArray();
243   - String[] list;
  266 + List<String> namesList = new ArrayList<String>();
  267 + final Map<String,Integer> namesMap; // final needed for sorting with anonymous comparator
244 268 try{
245   - SimpleDateFormat s = (SimpleDateFormat)android.text.format.DateFormat.getDateFormat(this.ctx.getContext());
246   - DateFormatSymbols ds = s.getDateFormatSymbols();
247 269 int type = 0; //default wide
248 270 int item = 0; //default months
249 271
@@ -262,14 +284,31 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
262 284 }
263 285 //determine return value
264 286 int method = item + type;
265   - if (method == 1){list = ds.getShortMonths();}//months and narrow
266   - else if (method == 10){list = ds.getWeekdays();}//days and wide
267   - else if (method == 11){list = ds.getShortWeekdays();}//days and narrow
268   - else{list = ds.getMonths();}//default: months and wide
  287 + if (method == 1) { //months and narrow
  288 + namesMap = Calendar.getInstance().getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
  289 + } else if (method == 10) { //days and wide
  290 + namesMap = Calendar.getInstance().getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
  291 + } else if (method == 11) { //days and narrow
  292 + namesMap = Calendar.getInstance().getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());
  293 + } else { //default: months and wide
  294 + namesMap = Calendar.getInstance().getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
  295 + }
  296 +
  297 + // save names as a list
  298 + for(String name : namesMap.keySet()) {
  299 + namesList.add(name);
  300 + }
  301 +
  302 + // sort the list according to values in namesMap
  303 + Collections.sort(namesList, new Comparator<String>() {
  304 + public int compare(String arg0, String arg1) {
  305 + return namesMap.get(arg0).compareTo(namesMap.get(arg1));
  306 + }
  307 + });
269 308
270   - //convert String[] into JSONArray of String objects
271   - for (int i = 0; i < list.length; i ++){
272   - value.put(list[i]);
  309 + // convert nameList into JSONArray of String objects
  310 + for (int i = 0; i < namesList.size(); i ++){
  311 + value.put(namesList.get(i));
273 312 }
274 313
275 314 //return array of names
2  Android/Globalization/GlobalizationError.java
... ... @@ -1,7 +1,7 @@
1 1 /**
2 2 * User initiated exception
3 3 */
4   -package com.phonegap.plugins.globalization;
  4 +package org.apache.cordova.plugins.globalization;
5 5
6 6 /**
7 7 * @description Exception class representing defined Globalization error codes
3  Android/Globalization/Resources.java
... ... @@ -1,7 +1,7 @@
1 1 /**
2 2 *
3 3 */
4   -package com.phonegap.plugins.globalization;
  4 +package org.apache.cordova.plugins.globalization;
5 5
6 6 /**
7 7 * @author costanzo
@@ -20,6 +20,7 @@
20 20 public static final String STRINGTONUMBER = "stringToNumber";
21 21 public static final String GETNUMBERPATTERN = "getNumberPattern";
22 22 public static final String GETCURRENCYPATTERN = "getCurrencyPattern";
  23 + public static final String GETPREFERREDLANGUAGE = "getPreferredLanguage";
23 24
24 25 //GlobalizationCommand Option Parameters
25 26 public static final String OPTIONS = "options";
34 Android/Globalization/globalization.js
... ... @@ -1,7 +1,21 @@
1   -
2   -function Globalization()
3   -{
  1 +var Globalization = function() {
  2 +};
4 3
  4 +Globalization.prototype.getPreferredLanguage = function(successCB, failureCB)
  5 +{
  6 + // successCallback required
  7 + if (typeof successCB != "function") {
  8 + console.log("Globalization.getPreferredLanguage Error: successCB is not a function");
  9 + return;
  10 + }
  11 +
  12 + // errorCallback required
  13 + if (typeof failureCB != "function") {
  14 + console.log("Globalization.getPreferredLanguage Error: failureCB is not a function");
  15 + return;
  16 + }
  17 +
  18 + cordova.exec(successCB, failureCB, "GlobalizationCommand","getPreferredLanguage", []);
5 19 };
6 20
7 21 /**
@@ -173,7 +187,7 @@ Globalization.prototype.stringToDate = function(dateString, successCB, failureCB
173 187 * @error GlobalizationError.PATTERN_ERROR
174 188 *
175 189 * Example
176   -* globalization.getDatePattern(new Date(),
  190 +* globalization.getDatePattern(
177 191 * function (date) {alert('pattern:' + date.pattern + '\n');},
178 192 * function () {},
179 193 * {formatLength:'short'});
@@ -506,10 +520,6 @@ Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, f
506 520 console.log("Globalization.getCurrencyPattern Error: currencyCode is not a currency code");
507 521 }
508 522 };
509   -cordova.addConstructor(function()
510   -{
511   - cordova.addPlugin('globalization', new Globalization());
512   -});
513 523
514 524 GlobalizationError = function() {
515 525 this.code = null;
@@ -520,3 +530,11 @@ GlobalizationError.UNKNOWN_ERROR = 0;
520 530 GlobalizationError.FORMATTING_ERROR = 1;
521 531 GlobalizationError.PARSING_ERROR = 2;
522 532 GlobalizationError.PATTERN_ERROR = 3;
  533 +
  534 +
  535 +if(!window.plugins) {
  536 + window.plugins = {};
  537 +}
  538 +if (!window.plugins.globalization) {
  539 + window.plugins.globalization = new Globalization();
  540 +}

0 comments on commit babd132

Please sign in to comment.
Something went wrong with that request. Please try again.