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 )){
@@ -91,6 +97,23 @@ private JSONObject getLocaleName() throws GlobalizationError{
91
97
throw new GlobalizationError (GlobalizationError .UNKNOWN_ERROR );
92
98
}
93
99
}
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
+ }
94
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.
@@ -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
0 commit comments