Permalink
Browse files

Updated Globalization plugin for use with Cordova 2.0 and add

getPreferredLanguage API
  • Loading branch information...
mow415
mow415 committed Aug 8, 2012
1 parent babd132 commit 770070ba164d3252987888274d6e9ac79fe56959
Showing with 85 additions and 54 deletions.
  1. +3 −5 iOS/Globalization/Globalization.h
  2. +52 −49 iOS/Globalization/Globalization.js
  3. +30 −0 iOS/Globalization/Globalization.m
@@ -6,12 +6,7 @@
*/
#import <Foundation/Foundation.h>
-#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVPlugin.h>
-#else
-#import "CDVPlugin.h"
-#endif
-
#define FORMAT_SHORT 0
#define FORMAT_MEDIUM 1
@@ -32,6 +27,9 @@ typedef NSUInteger GlobalizationError;
@interface Globalization : CDVPlugin {
CFLocaleRef currentLocale;
}
+
+- (void) getPreferredLanguage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
+
/**
* Returns the string identifier for the client’s current locale setting.
* It returns the locale identifier string to the successCB callback with a
@@ -5,17 +5,27 @@
* Copyright (c) 2010-2011, IBM Corporation
*
*/
-if (!window.plugins.globalization) {
- // shim to work in 1.5 and 1.6
- if (!window.Cordova) {
- window.Cordova = cordova;
- };
+var Globalization = function() {
+};
-function Globalization()
+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;
+ }
+
+ Codova.exec(successCB, failureCB, "Globalization","getPreferredLanguage", []);
};
-
+
/**
* Returns the string identifier for the client's current locale setting.
* It returns the locale identifier string to the successCB callback with a
@@ -40,7 +50,7 @@ Globalization.prototype.getLocaleName = function(successCB, failureCB)
console.log("Globalization.getLocaleName Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getLocaleName Error: failureCB is not a function");
@@ -49,7 +59,7 @@ Globalization.prototype.getLocaleName = function(successCB, failureCB)
Cordova.exec(successCB, failureCB, "Globalization","getLocaleName", []);
};
-
+
/**
* Returns a date formatted as a string according to the client's user preferences and
* calendar using the time zone of the client. It returns the formatted date string to the
@@ -82,14 +92,14 @@ Globalization.prototype.dateToString = function(date, successCB, failureCB, opti
console.log("Globalization.dateToString Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.dateToString Error: failureCB is not a function");
return;
}
-
-
+
+
if (date instanceof Date){
var dateValue;
dateValue = date.valueOf();
@@ -143,13 +153,13 @@ Globalization.prototype.stringToDate = function(dateString, successCB, failureCB
console.log("Globalization.stringToDate Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.stringToDate Error: failureCB is not a function");
return;
}
-
+
if (typeof dateString == "string"){
Cordova.exec(successCB, failureCB, "Globalization", "stringToDate", [{"dateString": dateString, "options": options}]);
}
@@ -158,7 +168,7 @@ Globalization.prototype.stringToDate = function(dateString, successCB, failureCB
}
};
-
+
/**
* Returns a pattern string for formatting and parsing dates according to the client's
* user preferences. It returns the pattern to the successCB callback with a
@@ -198,17 +208,17 @@ Globalization.prototype.getDatePattern = function(successCB, failureCB, options)
console.log("Globalization.getDatePattern Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getDatePattern Error: failureCB is not a function");
return;
}
-
+
Cordova.exec(successCB, failureCB, "Globalization", "getDatePattern", [{"options": options}]);
};
-
+
/**
* Returns an array of either the names of the months or days of the week
* according to the client's user preferences and calendar. It returns the array of names to the
@@ -241,13 +251,13 @@ Globalization.prototype.getDateNames = function(successCB, failureCB, options)
console.log("Globalization.getDateNames Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getDateNames Error: failureCB is not a function");
return;
}
-
+
Cordova.exec(successCB, failureCB, "Globalization", "getDateNames", [{"options": options}]);
};
@@ -278,14 +288,14 @@ Globalization.prototype.isDayLightSavingsTime = function(date, successCB, failur
console.log("Globalization.isDayLightSavingsTime Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.isDayLightSavingsTime Error: failureCB is not a function");
return;
}
-
-
+
+
if (date instanceof Date){
var dateValue;
dateValue = date.valueOf();
@@ -294,7 +304,7 @@ Globalization.prototype.isDayLightSavingsTime = function(date, successCB, failur
else {
console.log("Globalization.isDayLightSavingsTime Error: date is not a Date object");
}
-
+
};
/**
@@ -322,17 +332,17 @@ Globalization.prototype.getFirstDayOfWeek = function(successCB, failureCB)
console.log("Globalization.getFirstDayOfWeek Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getFirstDayOfWeek Error: failureCB is not a function");
return;
}
-
+
Cordova.exec(successCB, failureCB, "Globalization", "getFirstDayOfWeek", []);
};
-
+
/**
* Returns a number formatted as a string according to the client's user preferences.
* It returns the formatted number string to the successCB callback with a properties object as a
@@ -363,13 +373,13 @@ Globalization.prototype.numberToString = function(number, successCB, failureCB,
console.log("Globalization.numberToString Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.numberToString Error: failureCB is not a function");
return;
}
-
+
if(typeof number == "number") {
Cordova.exec(successCB, failureCB, "Globalization", "numberToString", [{"number": number, "options": options}]);
}
@@ -408,13 +418,13 @@ Globalization.prototype.stringToNumber = function(numberString, successCB, failu
console.log("Globalization.stringToNumber Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.stringToNumber Error: failureCB is not a function");
return;
}
-
+
if(typeof numberString == "string") {
Cordova.exec(successCB, failureCB, "Globalization", "stringToNumber", [{"numberString": numberString, "options": options}]);
}
@@ -460,13 +470,13 @@ Globalization.prototype.getNumberPattern = function(successCB, failureCB, option
console.log("Globalization.getNumberPattern Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getNumberPattern Error: failureCB is not a function");
return;
}
-
+
Cordova.exec(successCB, failureCB, "Globalization", "getNumberPattern", [{"options": options}]);
};
@@ -504,13 +514,13 @@ Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, f
console.log("Globalization.getCurrencyPattern Error: successCB is not a function");
return;
}
-
+
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getCurrencyPattern Error: failureCB is not a function");
return;
}
-
+
if(typeof currencyCode == "string") {
Cordova.exec(successCB, failureCB, "Globalization", "getCurrencyPattern", [{"currencyCode": currencyCode}]);
}
@@ -519,18 +529,6 @@ Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, f
}
};
-Globalization.install = function()
-{
- if(!window.plugins)
- {
- window.plugins = {};
- }
- if (!window.plugins.globalization) {
- window.plugins.globalization = new Globalization();
- }
- return window.plugins.globalization;
-};
-
GlobalizationError = function() {
this.code = null;
}
@@ -541,5 +539,10 @@ GlobalizationError.FORMATTING_ERROR = 1;
GlobalizationError.PARSING_ERROR = 2;
GlobalizationError.PATTERN_ERROR = 3;
-Cordova.addConstructor(Globalization.install);
-};
+
+if(!window.plugins) {
+ window.plugins = {};
+}
+if (!window.plugins.globalization) {
+ window.plugins.globalization = new Globalization();
+}
@@ -19,6 +19,36 @@ -(id)initWithWebView:(UIWebView *)theWebView
}
return self;
}
+
+- (void) getPreferredLanguage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ NSString* callbackId = [arguments objectAtIndex:0];
+
+ NSString* jsString = nil; // result string
+ NSLog(@"log1");
+ // Source: http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios
+ // (should be OK)
+ NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
+
+ if(language) {
+ NSDictionary * dictionary = [NSDictionary dictionaryWithObject: language forKey:@"value"];
+
+ CDVPluginResult * result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
+ messageAsDictionary: dictionary];
+
+ jsString = [result toSuccessCallbackString:callbackId];
+ }
+ else {
+ // TBD is this ever expected to happen?
+ CDVPluginResult * result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
+ messageAsString: @"UNKNOWN_ERROR"];
+
+ jsString = [result toErrorCallbackString:callbackId];
+ }
+
+ [self writeJavascript:jsString];
+}
+
- (void) getLocaleName:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
CDVPluginResult* result = nil;

0 comments on commit 770070b

Please sign in to comment.