98 lines
3.4 KiB
JavaScript
98 lines
3.4 KiB
JavaScript
|
if(!dojo._hasResource["dojo.currency"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
||
|
dojo._hasResource["dojo.currency"] = true;
|
||
|
dojo.provide("dojo.currency");
|
||
|
|
||
|
dojo.require("dojo.number");
|
||
|
dojo.require("dojo.i18n");
|
||
|
dojo.requireLocalization("dojo.cldr", "currency", null, "zh,en-ca,pt,en-us,de,ja,en,en-au,ROOT,fr,es,ko,zh-tw,it");
|
||
|
dojo.require("dojo.cldr.monetary");
|
||
|
|
||
|
/*=====
|
||
|
dojo.currency = {
|
||
|
// summary: localized formatting and parsing routines for currencies
|
||
|
}
|
||
|
=====*/
|
||
|
|
||
|
dojo.currency._mixInDefaults = function(options){
|
||
|
options = options || {};
|
||
|
options.type = "currency";
|
||
|
|
||
|
// Get locale-depenent currency data, like the symbol
|
||
|
var bundle = dojo.i18n.getLocalization("dojo.cldr", "currency", options.locale) || {};
|
||
|
|
||
|
// Mixin locale-independent currency data, like # of places
|
||
|
var iso = options.currency;
|
||
|
var data = dojo.cldr.monetary.getData(iso);
|
||
|
|
||
|
dojo.forEach(["displayName","symbol","group","decimal"], function(prop){
|
||
|
data[prop] = bundle[iso+"_"+prop];
|
||
|
});
|
||
|
|
||
|
data.fractional = [true, false];
|
||
|
|
||
|
// Mixin with provided options
|
||
|
return dojo.mixin(data, options);
|
||
|
}
|
||
|
|
||
|
dojo.currency.format = function(/*Number*/value, /*dojo.number.__FormatOptions?*/options){
|
||
|
// summary:
|
||
|
// Format a Number as a currency, using locale-specific settings
|
||
|
//
|
||
|
// description:
|
||
|
// Create a string from a Number using a known, localized pattern.
|
||
|
// [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements) appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr)
|
||
|
// as well as the appropriate symbols and delimiters.
|
||
|
//
|
||
|
// value:
|
||
|
// the number to be formatted.
|
||
|
|
||
|
return dojo.number.format(value, dojo.currency._mixInDefaults(options));
|
||
|
}
|
||
|
|
||
|
dojo.currency.regexp = function(/*dojo.number.__RegexpOptions?*/options){
|
||
|
//
|
||
|
// summary:
|
||
|
// Builds the regular needed to parse a currency value
|
||
|
//
|
||
|
// description:
|
||
|
// Returns regular expression with positive and negative match, group and decimal separators
|
||
|
// Note: the options.places default, the number of decimal places to accept, is defined by the currency type.
|
||
|
return dojo.number.regexp(dojo.currency._mixInDefaults(options)); // String
|
||
|
}
|
||
|
|
||
|
/*=====
|
||
|
dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
|
||
|
// type: String?
|
||
|
// currency, set by default.
|
||
|
// symbol: String?
|
||
|
// override currency symbol. Normally, will be looked up in table of supported currencies,
|
||
|
// and ISO currency code will be used if not found. See dojo.i18n.cldr.nls->currency.js
|
||
|
// places: Number?
|
||
|
// number of decimal places to accept. Default is defined by currency.
|
||
|
// fractional: Boolean?|Array?
|
||
|
// where places are implied by pattern or explicit 'places' parameter, whether to include the fractional portion.
|
||
|
// By default for currencies, it the fractional portion is optional.
|
||
|
type: "",
|
||
|
symbol: "",
|
||
|
places: "",
|
||
|
fractional: ""
|
||
|
});
|
||
|
=====*/
|
||
|
|
||
|
dojo.currency.parse = function(/*String*/expression, /*dojo.currency.__ParseOptions?*/options){
|
||
|
//
|
||
|
// summary:
|
||
|
// Convert a properly formatted currency string to a primitive Number,
|
||
|
// using locale-specific settings.
|
||
|
//
|
||
|
// description:
|
||
|
// Create a Number from a string using a known, localized pattern.
|
||
|
// [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns) are chosen appropriate to the locale.
|
||
|
//
|
||
|
// expression: A string representation of a Number
|
||
|
|
||
|
return dojo.number.parse(expression, dojo.currency._mixInDefaults(options));
|
||
|
}
|
||
|
|
||
|
}
|