globalization.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var argscheck = require('cordova/argscheck'),
  22. exec = require('cordova/exec');
  23. var globalization = {
  24. /**
  25. * Returns the string identifier for the client's current language.
  26. * It returns the language identifier string to the successCB callback with a
  27. * properties object as a parameter. If there is an error getting the language,
  28. * then the errorCB callback is invoked.
  29. *
  30. * @param {Function} successCB
  31. * @param {Function} errorCB
  32. *
  33. * @return Object.value {String}: The language identifier
  34. *
  35. * @error GlobalizationError.UNKNOWN_ERROR
  36. *
  37. * Example
  38. * globalization.getPreferredLanguage(function (language) {alert('language:' + language.value + '\n');},
  39. * function () {});
  40. */
  41. getPreferredLanguage:function(successCB, failureCB) {
  42. argscheck.checkArgs('fF', 'Globalization.getPreferredLanguage', arguments);
  43. exec(successCB, failureCB, "Globalization","getPreferredLanguage", []);
  44. },
  45. /**
  46. * Returns the string identifier for the client's current locale setting.
  47. * It returns the locale identifier string to the successCB callback with a
  48. * properties object as a parameter. If there is an error getting the locale,
  49. * then the errorCB callback is invoked.
  50. *
  51. * @param {Function} successCB
  52. * @param {Function} errorCB
  53. *
  54. * @return Object.value {String}: The locale identifier
  55. *
  56. * @error GlobalizationError.UNKNOWN_ERROR
  57. *
  58. * Example
  59. * globalization.getLocaleName(function (locale) {alert('locale:' + locale.value + '\n');},
  60. * function () {});
  61. */
  62. getLocaleName:function(successCB, failureCB) {
  63. argscheck.checkArgs('fF', 'Globalization.getLocaleName', arguments);
  64. exec(successCB, failureCB, "Globalization","getLocaleName", []);
  65. },
  66. /**
  67. * Returns a date formatted as a string according to the client's user preferences and
  68. * calendar using the time zone of the client. It returns the formatted date string to the
  69. * successCB callback with a properties object as a parameter. If there is an error
  70. * formatting the date, then the errorCB callback is invoked.
  71. *
  72. * The defaults are: formatLenght="short" and selector="date and time"
  73. *
  74. * @param {Date} date
  75. * @param {Function} successCB
  76. * @param {Function} errorCB
  77. * @param {Object} options {optional}
  78. * formatLength {String}: 'short', 'medium', 'long', or 'full'
  79. * selector {String}: 'date', 'time', or 'date and time'
  80. *
  81. * @return Object.value {String}: The localized date string
  82. *
  83. * @error GlobalizationError.FORMATTING_ERROR
  84. *
  85. * Example
  86. * globalization.dateToString(new Date(),
  87. * function (date) {alert('date:' + date.value + '\n');},
  88. * function (errorCode) {alert(errorCode);},
  89. * {formatLength:'short'});
  90. */
  91. dateToString:function(date, successCB, failureCB, options) {
  92. argscheck.checkArgs('dfFO', 'Globalization.dateToString', arguments);
  93. var dateValue = date.valueOf();
  94. exec(successCB, failureCB, "Globalization", "dateToString", [{"date": dateValue, "options": options}]);
  95. },
  96. /**
  97. * Parses a date formatted as a string according to the client's user
  98. * preferences and calendar using the time zone of the client and returns
  99. * the corresponding date object. It returns the date to the successCB
  100. * callback with a properties object as a parameter. If there is an error
  101. * parsing the date string, then the errorCB callback is invoked.
  102. *
  103. * The defaults are: formatLength="short" and selector="date and time"
  104. *
  105. * @param {String} dateString
  106. * @param {Function} successCB
  107. * @param {Function} errorCB
  108. * @param {Object} options {optional}
  109. * formatLength {String}: 'short', 'medium', 'long', or 'full'
  110. * selector {String}: 'date', 'time', or 'date and time'
  111. *
  112. * @return Object.year {Number}: The four digit year
  113. * Object.month {Number}: The month from (0 - 11)
  114. * Object.day {Number}: The day from (1 - 31)
  115. * Object.hour {Number}: The hour from (0 - 23)
  116. * Object.minute {Number}: The minute from (0 - 59)
  117. * Object.second {Number}: The second from (0 - 59)
  118. * Object.millisecond {Number}: The milliseconds (from 0 - 999),
  119. * not available on all platforms
  120. *
  121. * @error GlobalizationError.PARSING_ERROR
  122. *
  123. * Example
  124. * globalization.stringToDate('4/11/2011',
  125. * function (date) { alert('Month:' + date.month + '\n' +
  126. * 'Day:' + date.day + '\n' +
  127. * 'Year:' + date.year + '\n');},
  128. * function (errorCode) {alert(errorCode);},
  129. * {selector:'date'});
  130. */
  131. stringToDate:function(dateString, successCB, failureCB, options) {
  132. argscheck.checkArgs('sfFO', 'Globalization.stringToDate', arguments);
  133. exec(successCB, failureCB, "Globalization", "stringToDate", [{"dateString": dateString, "options": options}]);
  134. },
  135. /**
  136. * Returns a pattern string for formatting and parsing dates according to the client's
  137. * user preferences. It returns the pattern to the successCB callback with a
  138. * properties object as a parameter. If there is an error obtaining the pattern,
  139. * then the errorCB callback is invoked.
  140. *
  141. * The defaults are: formatLength="short" and selector="date and time"
  142. *
  143. * @param {Function} successCB
  144. * @param {Function} errorCB
  145. * @param {Object} options {optional}
  146. * formatLength {String}: 'short', 'medium', 'long', or 'full'
  147. * selector {String}: 'date', 'time', or 'date and time'
  148. *
  149. * @return Object.pattern {String}: The date and time pattern for formatting and parsing dates.
  150. * The patterns follow Unicode Technical Standard #35
  151. * http://unicode.org/reports/tr35/tr35-4.html
  152. * Object.timezone {String}: The abbreviated name of the time zone on the client
  153. * Object.utc_offset {Number}: The current difference in seconds between the client's
  154. * time zone and coordinated universal time.
  155. * Object.dst_offset {Number}: The current daylight saving time offset in seconds
  156. * between the client's non-daylight saving's time zone
  157. * and the client's daylight saving's time zone.
  158. *
  159. * @error GlobalizationError.PATTERN_ERROR
  160. *
  161. * Example
  162. * globalization.getDatePattern(
  163. * function (date) {alert('pattern:' + date.pattern + '\n');},
  164. * function () {},
  165. * {formatLength:'short'});
  166. */
  167. getDatePattern:function(successCB, failureCB, options) {
  168. argscheck.checkArgs('fFO', 'Globalization.getDatePattern', arguments);
  169. exec(successCB, failureCB, "Globalization", "getDatePattern", [{"options": options}]);
  170. },
  171. /**
  172. * Returns an array of either the names of the months or days of the week
  173. * according to the client's user preferences and calendar. It returns the array of names to the
  174. * successCB callback with a properties object as a parameter. If there is an error obtaining the
  175. * names, then the errorCB callback is invoked.
  176. *
  177. * The defaults are: type="wide" and item="months"
  178. *
  179. * @param {Function} successCB
  180. * @param {Function} errorCB
  181. * @param {Object} options {optional}
  182. * type {String}: 'narrow' or 'wide'
  183. * item {String}: 'months', or 'days'
  184. *
  185. * @return Object.value {Array{String}}: The array of names starting from either
  186. * the first month in the year or the
  187. * first day of the week.
  188. * @error GlobalizationError.UNKNOWN_ERROR
  189. *
  190. * Example
  191. * globalization.getDateNames(function (names) {
  192. * for(var i = 0; i < names.value.length; i++) {
  193. * alert('Month:' + names.value[i] + '\n');}},
  194. * function () {});
  195. */
  196. getDateNames:function(successCB, failureCB, options) {
  197. argscheck.checkArgs('fFO', 'Globalization.getDateNames', arguments);
  198. exec(successCB, failureCB, "Globalization", "getDateNames", [{"options": options}]);
  199. },
  200. /**
  201. * Returns whether daylight savings time is in effect for a given date using the client's
  202. * time zone and calendar. It returns whether or not daylight savings time is in effect
  203. * to the successCB callback with a properties object as a parameter. If there is an error
  204. * reading the date, then the errorCB callback is invoked.
  205. *
  206. * @param {Date} date
  207. * @param {Function} successCB
  208. * @param {Function} errorCB
  209. *
  210. * @return Object.dst {Boolean}: The value "true" indicates that daylight savings time is
  211. * in effect for the given date and "false" indicate that it is not.
  212. *
  213. * @error GlobalizationError.UNKNOWN_ERROR
  214. *
  215. * Example
  216. * globalization.isDayLightSavingsTime(new Date(),
  217. * function (date) {alert('dst:' + date.dst + '\n');}
  218. * function () {});
  219. */
  220. isDayLightSavingsTime:function(date, successCB, failureCB) {
  221. argscheck.checkArgs('dfF', 'Globalization.isDayLightSavingsTime', arguments);
  222. var dateValue = date.valueOf();
  223. exec(successCB, failureCB, "Globalization", "isDayLightSavingsTime", [{"date": dateValue}]);
  224. },
  225. /**
  226. * Returns the first day of the week according to the client's user preferences and calendar.
  227. * The days of the week are numbered starting from 1 where 1 is considered to be Sunday.
  228. * It returns the day to the successCB callback with a properties object as a parameter.
  229. * If there is an error obtaining the pattern, then the errorCB callback is invoked.
  230. *
  231. * @param {Function} successCB
  232. * @param {Function} errorCB
  233. *
  234. * @return Object.value {Number}: The number of the first day of the week.
  235. *
  236. * @error GlobalizationError.UNKNOWN_ERROR
  237. *
  238. * Example
  239. * globalization.getFirstDayOfWeek(function (day)
  240. * { alert('Day:' + day.value + '\n');},
  241. * function () {});
  242. */
  243. getFirstDayOfWeek:function(successCB, failureCB) {
  244. argscheck.checkArgs('fF', 'Globalization.getFirstDayOfWeek', arguments);
  245. exec(successCB, failureCB, "Globalization", "getFirstDayOfWeek", []);
  246. },
  247. /**
  248. * Returns a number formatted as a string according to the client's user preferences.
  249. * It returns the formatted number string to the successCB callback with a properties object as a
  250. * parameter. If there is an error formatting the number, then the errorCB callback is invoked.
  251. *
  252. * The defaults are: type="decimal"
  253. *
  254. * @param {Number} number
  255. * @param {Function} successCB
  256. * @param {Function} errorCB
  257. * @param {Object} options {optional}
  258. * type {String}: 'decimal', "percent", or 'currency'
  259. *
  260. * @return Object.value {String}: The formatted number string.
  261. *
  262. * @error GlobalizationError.FORMATTING_ERROR
  263. *
  264. * Example
  265. * globalization.numberToString(3.25,
  266. * function (number) {alert('number:' + number.value + '\n');},
  267. * function () {},
  268. * {type:'decimal'});
  269. */
  270. numberToString:function(number, successCB, failureCB, options) {
  271. argscheck.checkArgs('nfFO', 'Globalization.numberToString', arguments);
  272. exec(successCB, failureCB, "Globalization", "numberToString", [{"number": number, "options": options}]);
  273. },
  274. /**
  275. * Parses a number formatted as a string according to the client's user preferences and
  276. * returns the corresponding number. It returns the number to the successCB callback with a
  277. * properties object as a parameter. If there is an error parsing the number string, then
  278. * the errorCB callback is invoked.
  279. *
  280. * The defaults are: type="decimal"
  281. *
  282. * @param {String} numberString
  283. * @param {Function} successCB
  284. * @param {Function} errorCB
  285. * @param {Object} options {optional}
  286. * type {String}: 'decimal', "percent", or 'currency'
  287. *
  288. * @return Object.value {Number}: The parsed number.
  289. *
  290. * @error GlobalizationError.PARSING_ERROR
  291. *
  292. * Example
  293. * globalization.stringToNumber('1234.56',
  294. * function (number) {alert('Number:' + number.value + '\n');},
  295. * function () { alert('Error parsing number');});
  296. */
  297. stringToNumber:function(numberString, successCB, failureCB, options) {
  298. argscheck.checkArgs('sfFO', 'Globalization.stringToNumber', arguments);
  299. exec(successCB, failureCB, "Globalization", "stringToNumber", [{"numberString": numberString, "options": options}]);
  300. },
  301. /**
  302. * Returns a pattern string for formatting and parsing numbers according to the client's user
  303. * preferences. It returns the pattern to the successCB callback with a properties object as a
  304. * parameter. If there is an error obtaining the pattern, then the errorCB callback is invoked.
  305. *
  306. * The defaults are: type="decimal"
  307. *
  308. * @param {Function} successCB
  309. * @param {Function} errorCB
  310. * @param {Object} options {optional}
  311. * type {String}: 'decimal', "percent", or 'currency'
  312. *
  313. * @return Object.pattern {String}: The number pattern for formatting and parsing numbers.
  314. * The patterns follow Unicode Technical Standard #35.
  315. * http://unicode.org/reports/tr35/tr35-4.html
  316. * Object.symbol {String}: The symbol to be used when formatting and parsing
  317. * e.g., percent or currency symbol.
  318. * Object.fraction {Number}: The number of fractional digits to use when parsing and
  319. * formatting numbers.
  320. * Object.rounding {Number}: The rounding increment to use when parsing and formatting.
  321. * Object.positive {String}: The symbol to use for positive numbers when parsing and formatting.
  322. * Object.negative: {String}: The symbol to use for negative numbers when parsing and formatting.
  323. * Object.decimal: {String}: The decimal symbol to use for parsing and formatting.
  324. * Object.grouping: {String}: The grouping symbol to use for parsing and formatting.
  325. *
  326. * @error GlobalizationError.PATTERN_ERROR
  327. *
  328. * Example
  329. * globalization.getNumberPattern(
  330. * function (pattern) {alert('Pattern:' + pattern.pattern + '\n');},
  331. * function () {});
  332. */
  333. getNumberPattern:function(successCB, failureCB, options) {
  334. argscheck.checkArgs('fFO', 'Globalization.getNumberPattern', arguments);
  335. exec(successCB, failureCB, "Globalization", "getNumberPattern", [{"options": options}]);
  336. },
  337. /**
  338. * Returns a pattern string for formatting and parsing currency values according to the client's
  339. * user preferences and ISO 4217 currency code. It returns the pattern to the successCB callback with a
  340. * properties object as a parameter. If there is an error obtaining the pattern, then the errorCB
  341. * callback is invoked.
  342. *
  343. * @param {String} currencyCode
  344. * @param {Function} successCB
  345. * @param {Function} errorCB
  346. *
  347. * @return Object.pattern {String}: The currency pattern for formatting and parsing currency values.
  348. * The patterns follow Unicode Technical Standard #35
  349. * http://unicode.org/reports/tr35/tr35-4.html
  350. * Object.code {String}: The ISO 4217 currency code for the pattern.
  351. * Object.fraction {Number}: The number of fractional digits to use when parsing and
  352. * formatting currency.
  353. * Object.rounding {Number}: The rounding increment to use when parsing and formatting.
  354. * Object.decimal: {String}: The decimal symbol to use for parsing and formatting.
  355. * Object.grouping: {String}: The grouping symbol to use for parsing and formatting.
  356. *
  357. * @error GlobalizationError.FORMATTING_ERROR
  358. *
  359. * Example
  360. * globalization.getCurrencyPattern('EUR',
  361. * function (currency) {alert('Pattern:' + currency.pattern + '\n');}
  362. * function () {});
  363. */
  364. getCurrencyPattern:function(currencyCode, successCB, failureCB) {
  365. argscheck.checkArgs('sfF', 'Globalization.getCurrencyPattern', arguments);
  366. exec(successCB, failureCB, "Globalization", "getCurrencyPattern", [{"currencyCode": currencyCode}]);
  367. }
  368. };
  369. module.exports = globalization;