[483] | 1 | define([ |
---|
| 2 | "./_base/array", |
---|
| 3 | "./_base/lang", |
---|
| 4 | /*===== "./_base/declare", =====*/ |
---|
| 5 | "./number", |
---|
| 6 | "./i18n", "./i18n!./cldr/nls/currency", |
---|
| 7 | "./cldr/monetary" |
---|
| 8 | ], function(darray, lang, /*===== declare, =====*/ dnumber, i18n, nlsCurrency, cldrMonetary){ |
---|
| 9 | |
---|
| 10 | // module: |
---|
| 11 | // dojo/currency |
---|
| 12 | |
---|
| 13 | var currency = { |
---|
| 14 | // summary: |
---|
| 15 | // localized formatting and parsing routines for currencies |
---|
| 16 | // description: |
---|
| 17 | // extends dojo.number to provide culturally-appropriate formatting of values |
---|
| 18 | // in various world currencies, including use of a currency symbol. The currencies are specified |
---|
| 19 | // by a three-letter international symbol in all uppercase, and support for the currencies is |
---|
| 20 | // provided by the data in `dojo.cldr`. The scripts generating dojo.cldr specify which |
---|
| 21 | // currency support is included. A fixed number of decimal places is determined based |
---|
| 22 | // on the currency type and is not determined by the 'pattern' argument. The fractional |
---|
| 23 | // portion is optional, by default, and variable length decimals are not supported. |
---|
| 24 | }; |
---|
| 25 | lang.setObject("dojo.currency", currency); |
---|
| 26 | |
---|
| 27 | currency._mixInDefaults = function(options){ |
---|
| 28 | options = options || {}; |
---|
| 29 | options.type = "currency"; |
---|
| 30 | |
---|
| 31 | // Get locale-dependent currency data, like the symbol |
---|
| 32 | var bundle = i18n.getLocalization("dojo.cldr", "currency", options.locale) || {}; |
---|
| 33 | |
---|
| 34 | // Mixin locale-independent currency data, like # of places |
---|
| 35 | var iso = options.currency; |
---|
| 36 | var data = cldrMonetary.getData(iso); |
---|
| 37 | |
---|
| 38 | darray.forEach(["displayName","symbol","group","decimal"], function(prop){ |
---|
| 39 | data[prop] = bundle[iso+"_"+prop]; |
---|
| 40 | }); |
---|
| 41 | |
---|
| 42 | data.fractional = [true, false]; |
---|
| 43 | |
---|
| 44 | // Mixin with provided options |
---|
| 45 | return lang.mixin(data, options); |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | /*===== |
---|
| 49 | currency.__FormatOptions = declare([dnumber.__FormatOptions], { |
---|
| 50 | // type: String? |
---|
| 51 | // Should not be set. Value is assumed to be "currency". |
---|
| 52 | // symbol: String? |
---|
| 53 | // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr` |
---|
| 54 | // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found. |
---|
| 55 | // currency: String? |
---|
| 56 | // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD". |
---|
| 57 | // For use with dojo.currency only. |
---|
| 58 | // places: Number? |
---|
| 59 | // number of decimal places to show. Default is defined based on which currency is used. |
---|
| 60 | type: "", |
---|
| 61 | symbol: "", |
---|
| 62 | currency: "", |
---|
| 63 | places: "" |
---|
| 64 | }); |
---|
| 65 | =====*/ |
---|
| 66 | |
---|
| 67 | currency.format = function(/*Number*/ value, /*__FormatOptions?*/ options){ |
---|
| 68 | // summary: |
---|
| 69 | // Format a Number as a currency, using locale-specific settings |
---|
| 70 | // |
---|
| 71 | // description: |
---|
| 72 | // Create a string from a Number using a known, localized pattern. |
---|
| 73 | // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements) |
---|
| 74 | // appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr) |
---|
| 75 | // as well as the appropriate symbols and delimiters and number of decimal places. |
---|
| 76 | // |
---|
| 77 | // value: |
---|
| 78 | // the number to be formatted. |
---|
| 79 | |
---|
| 80 | return dnumber.format(value, currency._mixInDefaults(options)); |
---|
| 81 | }; |
---|
| 82 | |
---|
| 83 | currency.regexp = function(/*dnumber.__RegexpOptions?*/ options){ |
---|
| 84 | // |
---|
| 85 | // summary: |
---|
| 86 | // Builds the regular needed to parse a currency value |
---|
| 87 | // |
---|
| 88 | // description: |
---|
| 89 | // Returns regular expression with positive and negative match, group and decimal separators |
---|
| 90 | // Note: the options.places default, the number of decimal places to accept, is defined by the currency type. |
---|
| 91 | return dnumber.regexp(currency._mixInDefaults(options)); // String |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | /*===== |
---|
| 95 | var __ParseOptions = currency.__ParseOptions = declare(dnumber.__ParseOptions, { |
---|
| 96 | // type: String? |
---|
| 97 | // Should not be set. Value is assumed to be currency. |
---|
| 98 | // currency: String? |
---|
| 99 | // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD". |
---|
| 100 | // For use with dojo.currency only. |
---|
| 101 | // symbol: String? |
---|
| 102 | // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr` |
---|
| 103 | // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found. |
---|
| 104 | // places: Number? |
---|
| 105 | // fixed number of decimal places to accept. The default is determined based on which currency is used. |
---|
| 106 | // fractional: Boolean|Array? |
---|
| 107 | // Whether to include the fractional portion, where the number of decimal places are implied by the currency |
---|
| 108 | // or explicit 'places' parameter. The value [true,false] makes the fractional portion optional. |
---|
| 109 | // By default for currencies, it the fractional portion is optional. |
---|
| 110 | }); |
---|
| 111 | =====*/ |
---|
| 112 | |
---|
| 113 | currency.parse = function(/*String*/ expression, /*__ParseOptions?*/ options){ |
---|
| 114 | // |
---|
| 115 | // summary: |
---|
| 116 | // Convert a properly formatted currency string to a primitive Number, |
---|
| 117 | // using locale-specific settings. |
---|
| 118 | // description: |
---|
| 119 | // Create a Number from a string using a known, localized pattern. |
---|
| 120 | // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns) |
---|
| 121 | // are chosen appropriate to the locale, as well as the appropriate symbols and delimiters |
---|
| 122 | // and number of decimal places. |
---|
| 123 | // expression: |
---|
| 124 | // A string representation of a currency value |
---|
| 125 | |
---|
| 126 | return dnumber.parse(expression, currency._mixInDefaults(options)); |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | return currency; |
---|
| 130 | }); |
---|