Skip to content

Commit babd132

Browse files
author
mow415
committed
Update package name and add getPreferredLanguage API
1 parent 7d49b76 commit babd132

File tree

4 files changed

+91
-33
lines changed

4 files changed

+91
-33
lines changed

Android/Globalization/GlobalizationCommand.java

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
/**
22
*
33
*/
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;
145

156
import java.text.DateFormat;
16-
import java.text.DateFormatSymbols;
177
import java.text.DecimalFormat;
188
import java.text.SimpleDateFormat;
9+
import java.util.ArrayList;
1910
import java.util.Calendar;
11+
import java.util.Collections;
12+
import java.util.Comparator;
2013
import java.util.Currency;
2114
import java.util.Date;
22-
import java.util.TimeZone;
15+
import java.util.List;
2316
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;
2427

2528
/**
2629
*
@@ -36,7 +39,10 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
3639
if (action.equals(Resources.GETLOCALENAME)){
3740
obj = getLocaleName();
3841
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)) {
4046
obj = getDateToString(data);
4147
return new PluginResult(PluginResult.Status.OK, obj);
4248
}else if(action.equalsIgnoreCase(Resources.STRINGTODATE)){
@@ -91,6 +97,23 @@ private JSONObject getLocaleName() throws GlobalizationError{
9197
throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
9298
}
9399
}
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+
}
94117
/*
95118
* @Description: Returns a date formatted as a string according to the client's user preferences and
96119
* calendar using the time zone of the client.
@@ -240,10 +263,9 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
240263
JSONObject obj = new JSONObject();
241264
//String[] value;
242265
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
244268
try{
245-
SimpleDateFormat s = (SimpleDateFormat)android.text.format.DateFormat.getDateFormat(this.ctx.getContext());
246-
DateFormatSymbols ds = s.getDateFormatSymbols();
247269
int type = 0; //default wide
248270
int item = 0; //default months
249271

@@ -262,14 +284,31 @@ private JSONObject getDateNames(JSONArray options) throws GlobalizationError{
262284
}
263285
//determine return value
264286
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+
});
269308

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));
273312
}
274313

275314
//return array of names

Android/Globalization/GlobalizationError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* User initiated exception
33
*/
4-
package com.phonegap.plugins.globalization;
4+
package org.apache.cordova.plugins.globalization;
55

66
/**
77
* @description Exception class representing defined Globalization error codes

Android/Globalization/Resources.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
*/
4-
package com.phonegap.plugins.globalization;
4+
package org.apache.cordova.plugins.globalization;
55

66
/**
77
* @author costanzo
@@ -20,6 +20,7 @@ public class Resources {
2020
public static final String STRINGTONUMBER = "stringToNumber";
2121
public static final String GETNUMBERPATTERN = "getNumberPattern";
2222
public static final String GETCURRENCYPATTERN = "getCurrencyPattern";
23+
public static final String GETPREFERREDLANGUAGE = "getPreferredLanguage";
2324

2425
//GlobalizationCommand Option Parameters
2526
public static final String OPTIONS = "options";

Android/Globalization/globalization.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
2-
function Globalization()
3-
{
1+
var Globalization = function() {
2+
};
43

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", []);
519
};
620

721
/**
@@ -173,7 +187,7 @@ Globalization.prototype.stringToDate = function(dateString, successCB, failureCB
173187
* @error GlobalizationError.PATTERN_ERROR
174188
*
175189
* Example
176-
* globalization.getDatePattern(new Date(),
190+
* globalization.getDatePattern(
177191
* function (date) {alert('pattern:' + date.pattern + '\n');},
178192
* function () {},
179193
* {formatLength:'short'});
@@ -506,10 +520,6 @@ Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, f
506520
console.log("Globalization.getCurrencyPattern Error: currencyCode is not a currency code");
507521
}
508522
};
509-
cordova.addConstructor(function()
510-
{
511-
cordova.addPlugin('globalization', new Globalization());
512-
});
513523

514524
GlobalizationError = function() {
515525
this.code = null;
@@ -520,3 +530,11 @@ GlobalizationError.UNKNOWN_ERROR = 0;
520530
GlobalizationError.FORMATTING_ERROR = 1;
521531
GlobalizationError.PARSING_ERROR = 2;
522532
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 commit comments

Comments
 (0)