Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E1EB9100CA for ; Sat, 8 Jun 2013 03:05:15 +0000 (UTC) Received: (qmail 18662 invoked by uid 500); 8 Jun 2013 03:05:15 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 18582 invoked by uid 500); 8 Jun 2013 03:05:10 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 18564 invoked by uid 99); 8 Jun 2013 03:05:08 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Jun 2013 03:05:08 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id C034C8A0ACF; Sat, 8 Jun 2013 03:05:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: bennmapes@apache.org To: commits@cordova.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: first pass at wp7 support Date: Sat, 8 Jun 2013 03:05:07 +0000 (UTC) Updated Branches: refs/heads/master a4aa44de9 -> 1bafef6df first pass at wp7 support Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/1bafef6d Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/1bafef6d Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/1bafef6d Branch: refs/heads/master Commit: 1bafef6df3403c4a009cd671b7ce344dddba0a7e Parents: a4aa44d Author: Benn Mapes Authored: Fri Jun 7 19:19:19 2013 -0700 Committer: Benn Mapes Committed: Fri Jun 7 19:23:08 2013 -0700 ---------------------------------------------------------------------- plugin.xml | 37 +- src/wp7/Globalization.cs | 1178 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1202 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/1bafef6d/plugin.xml ---------------------------------------------------------------------- diff --git a/plugin.xml b/plugin.xml index bc5abbd..744f30d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ - @@ -22,17 +22,28 @@ id="org.apache.cordova.core.Globalization" - + - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/1bafef6d/src/wp7/Globalization.cs ---------------------------------------------------------------------- diff --git a/src/wp7/Globalization.cs b/src/wp7/Globalization.cs new file mode 100644 index 0000000..1528807 --- /dev/null +++ b/src/wp7/Globalization.cs @@ -0,0 +1,1178 @@ +/* + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System; +using System.Globalization; +using System.Runtime.Serialization; + +namespace WPCordovaClassLib.Cordova.Commands +{ + /// + /// Provides information about system locale, culture settings, number formats, ect. + /// + public class Globalization : BaseCommand + { + + #region Globalization errors + + /// + /// Globalization error codes. + /// + public enum ErrorCode : int + { + UnknownError = 0, + FormattingError = 1, + ParsingError = 2, + PatternError = 3 + } + + /// + /// Represents globalization error object. + /// + [DataContract] + public class GlobalizationError + { + #region Error messages + /// + /// Error messages + /// + public const string UnknownError = "UNKNOWN_ERROR"; + public const string FormattingError = "FORMATTIN_ERROR"; + public const string ParsingError = "PARSING_ERROR"; + public const string PatternError = "PATTERN_ERROR"; + + #endregion + + /// + /// Error code + /// + [DataMember(Name = "code", IsRequired = false)] + public ErrorCode Code { get; set; } + + /// + /// Error message + /// + [DataMember(Name = "message", IsRequired = false)] + public string Message { get; set; } + + /// + /// Default constructor + /// + public GlobalizationError() + { + this.Code = ErrorCode.UnknownError; + this.Message = UnknownError; + } + + /// + /// Constructor setting error code + /// + public GlobalizationError(ErrorCode error) + { + this.Code = error; + + switch (error) + { + case ErrorCode.ParsingError: + { + this.Message = ParsingError; + break; + } + case ErrorCode.FormattingError: + { + this.Message = FormattingError; + break; + } + case ErrorCode.PatternError: + { + this.Message = PatternError; + break; + } + default: + { + this.Message = UnknownError; + break; + } + } + } + } + + #endregion + + #region Globalization options + + /// + /// Represents globalization options. + /// + [DataContract] + public class GlobalizationOptions + { + #region available option values + /// + /// Number pattern types. + /// + public const string Percent = "percent"; + public const string Currency = "currency"; + public const string Decimal = "decimal"; + + /// + /// Format length types + /// + public const string Short = "short"; + public const string Medium = "medium"; + public const string Long = "long"; + public const string Full = "full"; + + /// + /// Selector types + /// + public const string TimeSelector = "time"; + public const string DateSelector = "date"; + public const string DateAndTimeSelector = "date and time"; + + /// + /// Date name types + /// + public const string Narrow = "narrow"; + public const string Wide = "wide"; + + /// + /// Date name items + /// + public const string Months = "months"; + public const string Days = "days"; + + #endregion + + /// + /// Additional options + /// + [DataMember(Name = "options", IsRequired = false)] + public Options AdditionalOptions { get; set; } + + /// + /// Date to convert + /// + [DataMember(Name = "date", IsRequired = false)] + public long Date { get; set; } + + /// + /// Date as stirng + /// + [DataMember(Name = "dateString", IsRequired = false)] + public string DateString { get; set; } + + /// + /// Currency code + /// + [DataMember(Name = "currencyCode", IsRequired = false)] + public string CurrencyCode { get; set; } + + /// + /// Number as string + /// + [DataMember(Name = "numberString", IsRequired = false)] + public string NumberString { get; set; } + + /// + /// Number to convert + /// + [DataMember(Name = "number", IsRequired = false)] + public double Number { get; set; } + } + + /// + /// Represents additional options + /// + [DataContract] + public class Options + { + /// + /// Pattern type + /// + [DataMember(Name = "type", IsRequired = false)] + public string Type { get; set; } + + /// + /// Format length + /// + [DataMember(Name = "formatLength", IsRequired = false)] + public string FormatLength { get; set; } + + /// + /// Selector + /// + [DataMember(Name = "selector", IsRequired = false)] + public string Selector { get; set; } + + /// + /// Date name item + /// + [DataMember(Name = "item", IsRequired = false)] + public string Item { get; set; } + } + + #endregion + + #region returned objects + + #region Number pattern object + + /// + /// Represents number pattern + /// + [DataContract] + public class NumberPattern + { + /// + /// Pattern + /// + [DataMember(Name = "pattern", IsRequired = false)] + public string Pattern { get; set; } + + /// + /// Symbol + /// + [DataMember(Name = "symbol", IsRequired = false)] + public string Symbol { get; set; } + + /// + /// Fraction + /// + [DataMember(Name = "fraction", IsRequired = false)] + public int Fraction { get; set; } + + /// + /// Positive + /// + [DataMember(Name = "positive", IsRequired = false)] + public string Positive { get; set; } + + /// + /// Negative + /// + [DataMember(Name = "negative", IsRequired = false)] + public string Negative { get; set; } + + /// + /// Rounding + /// + [DataMember(Name = "rounding", IsRequired = false)] + public int Rounding { get; set; } + + /// + /// Decimal + /// + [DataMember(Name = "decimal", IsRequired = false)] + public string Decimal { get; set; } + + /// + /// Grouping + /// + [DataMember(Name = "grouping", IsRequired = false)] + public string Grouping { get; set; } + + /// + /// Constructor of the class + /// + /// + /// + /// + /// + /// + /// + /// + /// + public NumberPattern(string pattern, string symbol, int fraction, string positive, string negative, int rounding, string dec, string grouping) + { + this.Pattern = pattern; + this.Symbol = symbol; + this.Fraction = fraction; + this.Positive = positive; + this.Negative = negative; + this.Rounding = rounding; + this.Decimal = dec; + this.Grouping = grouping; + } + } + #endregion + + #region Date format object + + /// + /// Represents date format + /// + [DataContract] + public class DateFormat + { + /// + /// Year + /// + [DataMember(Name = "year", IsRequired = false)] + public int Year { get; set; } + + /// + /// Month + /// + [DataMember(Name = "month", IsRequired = false)] + public int Month { get; set; } + + /// + /// Day + /// + [DataMember(Name = "day", IsRequired = false)] + public int Day { get; set; } + + /// + /// Hour + /// + [DataMember(Name = "hour", IsRequired = false)] + public int Hour { get; set; } + + /// + /// Minute + /// + [DataMember(Name = "minute", IsRequired = false)] + public int Minute { get; set; } + + /// + /// Second + /// + [DataMember(Name = "second", IsRequired = false)] + public int Second { get; set; } + + /// + /// Millisecond + /// + [DataMember(Name = "millisecond", IsRequired = false)] + public int Millisecond { get; set; } + + public DateFormat(int year, int month, int day, int hour, int minute, int second, int millisecond) + { + this.Year = year; + this.Month = month; + this.Day = day; + this.Hour = hour; + this.Minute = minute; + this.Millisecond = millisecond; + } + + } + #endregion + + #region Date pattern object + + /// + /// Represents date pattern object + /// + [DataContract] + public class DatePattern + { + + /// + /// Date pattern + /// + [DataMember(Name = "pattern", IsRequired = false)] + public string Pattern { get; set; } + + /// + /// TimeZone + /// + [DataMember(Name = "timezone", IsRequired = false)] + public string TimeZone { get; set; } + + /// + /// UTC offset + /// + [DataMember(Name = "utc_offset", IsRequired = false)] + public double UtcOffset { get; set; } + + /// + /// Dst offset + /// + [DataMember(Name = "dst_offset", IsRequired = false)] + public double DstOffset { get; set; } + + /// + /// Constructor of the class + /// + /// + /// + /// + /// + public DatePattern(string pattern, string timezone, double utcOffset, double dstOffset) + { + this.Pattern = pattern; + this.TimeZone = timezone; + this.UtcOffset = utcOffset; + this.DstOffset = dstOffset; + } + + } + + #endregion + + #endregion + + #region Locale info + + /// + /// Gets the string identifier for the client's current locale setting. + /// + /// + public void getLocaleName(string options) + { + try + { + var locale = RegionInfo.CurrentRegion.TwoLetterISORegionName; + PluginResult result = new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(locale)); + this.DispatchCommandResult(result); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError())); + } + } + + /// + /// Gets the string identifier for the client's current language. + /// + /// + public void getPreferredLanguage(string options) + { + try + { + var language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; + PluginResult result = new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(language)); + this.DispatchCommandResult(result); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError())); + } + } + + #endregion + + #region Date and time info + + /// + /// Gets whether daylight savings time is in effect for a given date using the client's + /// time zone and calendar. + /// + /// Date to daylight savings check. + public void isDayLightSavingsTime(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + DateTime date = start.AddMilliseconds(globalOptions.Date).ToLocalTime(); + TimeZoneInfo localZone = TimeZoneInfo.Local; + bool isDaylightSavingTime = localZone.IsDaylightSavingTime(date); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(isDaylightSavingTime, "dst"))); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError())); + } + } + + /// + /// Gets the first day of the week according to the client's user preferences and calendar. + /// The days of the week are numbered starting from 1 where 1 is considered to be Sunday. + /// + /// + public void getFirstDayOfWeek(string options) + { + try + { + // DateTimeFormat returns days of the week numbered from zero, so we have to increase returned value by one. + var firstDayOfWeek = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 1; + PluginResult result = new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(firstDayOfWeek)); + this.DispatchCommandResult(result); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError())); + } + } + + #endregion + + #region Formatting + + /// + /// Gets a date formatted as a string according to the client's user preferences and calendar using the time zone of the client. + /// + /// + public void dateToString(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + DateTime date = start.AddMilliseconds(globalOptions.Date).ToLocalTime(); + + string format = "{0:M/dd/yy H:m:s}"; //short datetime by default + int formatLength = 0; //default format + int selector = 0; //default selector + + if (globalOptions.AdditionalOptions != null) + { + if (globalOptions.AdditionalOptions.FormatLength != null) + { + string t = globalOptions.AdditionalOptions.FormatLength; + + if (t.Equals(GlobalizationOptions.Full)) + { + formatLength++; + } + } + + if (globalOptions.AdditionalOptions.Selector != null) + { + string t = globalOptions.AdditionalOptions.Selector; + + if (t.Equals(GlobalizationOptions.DateSelector)) + { + selector += 10; + } + else if (t.Equals(GlobalizationOptions.TimeSelector)) + { + selector += 20; + } + } + + //determine return value + int method = formatLength + selector; + + switch (method) + { + case 1: // full datetime + { + format = "{0:MMMM/dddd/yyyy HH:mm:ss tt}"; + break; + } + case 10: // short date + { + format = "{0:d}"; + break; + } + case 11: // full date + { + format = "{0:D}"; + break; + } + case 20: // short time + { + format = "{0:t}"; + break; + } + case 21: // full time + { + format = "{0:T}"; + break; + } + default: // short datetime + { + format = "{0:M/dd/yy H:m:s}"; + break; + } + } + } + + string formattedValue = string.Format(CultureInfo.CurrentCulture, format, date); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(formattedValue))); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.FormattingError))); + } + } + + /// + /// Parses a date formatted as a string according to the client's user preferences and calendar using the time zone of the client and returns the corresponding date object + /// + /// + public void stringToDate(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + if (string.IsNullOrEmpty(globalOptions.DateString)) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + string format = "M/dd/yy H:m:s"; // short datetime by default + int formatLength = 0; //default format + int selector = 0; //default selector + + if (globalOptions.AdditionalOptions != null) + { + if (globalOptions.AdditionalOptions.FormatLength != null) + { + string t = globalOptions.AdditionalOptions.FormatLength; + + if (t.Equals(GlobalizationOptions.Full)) + { + formatLength++; + } + } + + if (globalOptions.AdditionalOptions.Selector != null) + { + string t = globalOptions.AdditionalOptions.Selector; + + if (t.Equals(GlobalizationOptions.DateSelector)) + { + selector += 10; + } + else if (t.Equals(GlobalizationOptions.TimeSelector)) + { + selector += 20; + } + } + + //determine return value + int method = formatLength + selector; + + switch (method) + { + case 1: // full datetime + { + format = "MMMM/dddd/yyyy HH:mm:ss tt"; + break; + } + case 10: // short date + { + format = "d"; + break; + } + case 11: // full date + { + format = "D"; + break; + } + case 20: // short time + { + format = "t"; + break; + } + case 21: // full time + { + format = "T"; + break; + } + default: // short datetime + { + format = "M/dd/yy H:m:s"; + break; + } + } + } + + DateTime date = DateTime.ParseExact(globalOptions.DateString, format, CultureInfo.CurrentCulture); + DateFormat dateFormat = new DateFormat(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, dateFormat)); + + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.ParsingError))); + } + } + + /// + /// Gets a pattern string for formatting and parsing dates according to the client's user preferences. + /// + /// + public void getDatePattern(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + DateTimeFormatInfo dateFormatInfo = DateTimeFormatInfo.CurrentInfo; + string pattern = dateFormatInfo.FullDateTimePattern; // full datetime by default + int formatLength = 0; //default format + int selector = 0; //default selector + + if (globalOptions.AdditionalOptions != null) + { + if (globalOptions.AdditionalOptions.FormatLength != null) + { + string t = globalOptions.AdditionalOptions.FormatLength; + + if (t.Equals(GlobalizationOptions.Full)) + { + formatLength++; + } + } + + if (globalOptions.AdditionalOptions.Selector != null) + { + string t = globalOptions.AdditionalOptions.Selector; + + if (t.Equals(GlobalizationOptions.DateSelector)) + { + selector += 10; + } + else if (t.Equals(GlobalizationOptions.TimeSelector)) + { + selector += 20; + } + } + + //determine return value + int method = formatLength + selector; + + switch (method) + { + case 1: // full datetime + { + pattern = dateFormatInfo.FullDateTimePattern; + break; + } + case 10: // short date + { + pattern = dateFormatInfo.ShortDatePattern; + break; + } + case 11: // full date + { + pattern = dateFormatInfo.LongDatePattern; + break; + } + case 20: // short time + { + pattern = dateFormatInfo.ShortTimePattern; + break; + } + case 21: // full time + { + pattern = dateFormatInfo.LongTimePattern; + break; + } + default: // short datetime + { + // Seems like C# doesn't support short datetime pattern so we use full format + // http://msdn.microsoft.com/en-us/library/1at0z4ew%28v=vs.71%29.aspx + pattern = dateFormatInfo.FullDateTimePattern; + break; + } + } + } + + TimeZoneInfo localZone = TimeZoneInfo.Local; + DatePattern datePattern = new DatePattern(pattern, localZone.DisplayName, localZone.BaseUtcOffset.TotalSeconds, 0); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, datePattern)); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.PatternError))); + } + } + + /// + /// Gets an array of either the names of the months or days of the week according to the client's user preferences and calendar. + /// + /// + public void getDateNames(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + int type = 0; //default wide + int item = 0; //default months + + if (globalOptions.AdditionalOptions != null) + { + if (globalOptions.AdditionalOptions.Type != null) + { + string t = globalOptions.AdditionalOptions.Type; + + if (t.Equals(GlobalizationOptions.Narrow)) + { + type++; + } + } + + if (globalOptions.AdditionalOptions.Item != null) + { + string t = globalOptions.AdditionalOptions.Item; + + if (t.Equals(GlobalizationOptions.Days)) + { + item += 10; + } + } + } + + //determine return value + int method = item + type; + string[] namesArray; + CultureInfo currentCulture = CultureInfo.CurrentCulture; + + if (method == 1) //months and narrow + { + namesArray = currentCulture.DateTimeFormat.AbbreviatedMonthNames; + } + else if (method == 10) //days and wide + { + namesArray = currentCulture.DateTimeFormat.DayNames; + } + else if (method == 11) //days and narrow + { + namesArray = currentCulture.DateTimeFormat.AbbreviatedDayNames; + } + else //default: months and wide + { + namesArray = currentCulture.DateTimeFormat.MonthNames; + } + + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(namesArray))); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError())); + } + } + + /// + /// Gets a number formatted as a string according to the client's user preferences. + /// + /// + public void numberToString(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + string format = string.Empty; + string numberFormatType = (globalOptions.AdditionalOptions == null || string.IsNullOrEmpty(globalOptions.AdditionalOptions.Type)) ? + GlobalizationOptions.Decimal : globalOptions.AdditionalOptions.Type; + + switch (numberFormatType) + { + case GlobalizationOptions.Percent: + { + format = "{0:p}"; + break; + } + + case GlobalizationOptions.Currency: + { + format = "{0:c}"; + break; + } + + default: + { + format = "{0:f}"; + break; + } + } + + string formattedValue = string.Format(CultureInfo.CurrentCulture, format, globalOptions.Number); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(formattedValue))); + + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.FormattingError))); + } + } + + /// + /// Gets a number formatted as a string according to the client's user preferences and returns the corresponding number. + /// + /// + public void stringToNumber(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + if (string.IsNullOrEmpty(globalOptions.NumberString)) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + string numberString = globalOptions.NumberString; + string numberFormatType = (globalOptions.AdditionalOptions == null || string.IsNullOrEmpty(globalOptions.AdditionalOptions.Type)) ? + GlobalizationOptions.Decimal : globalOptions.AdditionalOptions.Type; + + NumberStyles numberStyle; + + switch (numberFormatType) + { + case GlobalizationOptions.Percent: + { + numberStyle = NumberStyles.Any; + numberString = numberString.Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.PercentSymbol, ""); + break; + } + + case GlobalizationOptions.Currency: + { + numberStyle = NumberStyles.Currency; + break; + } + + default: + { + numberStyle = NumberStyles.Number; + break; + } + } + + double value = double.Parse(numberString, numberStyle, CultureInfo.CurrentCulture); + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, this.WrapIntoJSON(value))); + + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.ParsingError))); + } + } + + + /// + /// Gets a pattern string for formatting and parsing numbers according to the client's user preferences. + /// + /// + public void getNumberPattern(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + CultureInfo cultureInfo = CultureInfo.CurrentCulture; + NumberFormatInfo formatInfo = cultureInfo.NumberFormat; + string numberFormatType = (globalOptions.AdditionalOptions == null || string.IsNullOrEmpty(globalOptions.AdditionalOptions.Type)) ? + GlobalizationOptions.Decimal : globalOptions.AdditionalOptions.Type; + NumberPattern pattern = null; + string symbol; + + // TODO find out how to get format pattern and the number of fraction digits + switch (numberFormatType) + { + case GlobalizationOptions.Percent: + { + symbol = formatInfo.PercentSymbol; + pattern = new NumberPattern("", symbol, 0, formatInfo.PercentPositivePattern.ToString(), formatInfo.PercentNegativePattern.ToString(), 0, formatInfo.PercentDecimalSeparator, formatInfo.PercentGroupSeparator); + break; + } + case GlobalizationOptions.Currency: + { + symbol = formatInfo.CurrencySymbol; + pattern = new NumberPattern("", symbol, 0, formatInfo.CurrencyPositivePattern.ToString(), formatInfo.CurrencyNegativePattern.ToString(), 0, formatInfo.CurrencyDecimalSeparator, formatInfo.CurrencyGroupSeparator); + break; + } + default: + { + symbol = formatInfo.NumberDecimalSeparator; + pattern = new NumberPattern("", symbol, 0, "", formatInfo.NumberNegativePattern.ToString(), 0, formatInfo.NumberDecimalSeparator, formatInfo.NumberGroupSeparator); + break; + } + } + + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, pattern)); + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.PatternError))); + } + } + + /// + /// Gets a pattern string for formatting and parsing currency values according to the client's user preferences and ISO 4217 currency code. + /// + /// + public void getCurrencyPattern(string options) + { + GlobalizationOptions globalOptions; + + try + { + string[] args = JSON.JsonHelper.Deserialize(options); + globalOptions = JSON.JsonHelper.Deserialize(args[0]); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + try + { + if (string.IsNullOrEmpty(globalOptions.CurrencyCode)) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return; + } + + string currencyCode = globalOptions.CurrencyCode; + + // temporary not supported via lack of api required + this.DispatchCommandResult(new PluginResult(PluginResult.Status.INVALID_ACTION, "Not supported")); + return; + + // TODO find the way to get currency info from currency code + // http://stackoverflow.com/questions/12373800/3-digit-currency-code-to-currency-symbol + // http://stackoverflow.com/questions/6924067/how-to-get-specific-culture-currency-pattern + // CultureInfo cultureInfo = new CultureInfo(currencyCode); + // NumberFormatInfo numberFormat = cultureInfo.NumberFormat; + } + catch (Exception e) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new GlobalizationError(ErrorCode.FormattingError))); + } + } + + #endregion + + + #region private methods + + /// + /// Wraps data into JSON format + /// + /// data + /// data formatted as JSON object + private string WrapIntoJSON(T data, string keyName = "value") + { + string param = "{0}"; + string stringifiedData = data.ToString(); + + if (data.GetType() == typeof(string)) + { + param = "\"" + param + "\""; + } + + if (data.GetType() == typeof(bool)) + { + stringifiedData = stringifiedData.ToLower(); + } + + if (data.GetType() == typeof(string[])) + { + stringifiedData = JSON.JsonHelper.Serialize(data); + } + + var formattedData = string.Format("\"" + keyName + "\":" + param, stringifiedData); + formattedData = "{" + formattedData + "}"; + + return formattedData; + } + + #endregion + } +} \ No newline at end of file