Path: blob/trunk/third_party/closure/goog/i18n/currency.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview A utility to get better currency format pattern.17*18* This module implements a new currency format representation model. It19* provides 3 currency representation forms: global, portable and local. Local20* format is the most popular format people use to represent currency in its21* circulating country without worrying about how it should be distinguished22* from other currencies. Global format is a formal representation in context23* of multiple currencies in same page, it is ISO 4217 currency code. Portable24* format is a compromise between global and local. It looks similar to how25* people would like to see how their currency is being represented in other26* media. While at the same time, it should be distinguishable to world's27* popular currencies (like USD, EUR) and currencies somewhat relevant in the28* area (like CNY in HK, though native currency is HKD). There is no guarantee29* of uniqueness.30*31*/323334goog.provide('goog.i18n.currency');35goog.provide('goog.i18n.currency.CurrencyInfo');36goog.provide('goog.i18n.currency.CurrencyInfoTier2');373839/**40* The mask of precision field.41* @private42*/43goog.i18n.currency.PRECISION_MASK_ = 0x07;444546/**47* Whether the currency sign should be positioned after the number.48* @private49*/50goog.i18n.currency.POSITION_FLAG_ = 0x10;515253/**54* Whether a space should be inserted between the number and currency sign.55* @private56*/57goog.i18n.currency.SPACE_FLAG_ = 0x20;585960/**61* Whether tier2 was enabled already by calling addTier2Support().62* @private63*/64goog.i18n.currency.tier2Enabled_ = false;656667/**68* Tests if currency is available.69*70* Note: If the currency is not available it might be in the tier2 currency set:71* {@link goog.i18n.currency.CurrencyInfoTier2}. If that is the case call72* {@link goog.i18n.currency.addTier2Support} before calling any other function73* in this namespace.74*75* @param {string} currencyCode Currency code to tested.76* @return {boolean} If the currency is available.77*/78goog.i18n.currency.isAvailable = function(currencyCode) {79return currencyCode in goog.i18n.currency.CurrencyInfo;80};8182/**83* This function will add tier2 currency support. Be default, only tier184* (most popular currencies) are supported. If an application really needs85* to support some of the rarely used currencies, it should call this function86* before any other functions in this namespace.87*/88goog.i18n.currency.addTier2Support = function() {89// Protection from executing this these again and again.90if (!goog.i18n.currency.tier2Enabled_) {91for (var key in goog.i18n.currency.CurrencyInfoTier2) {92goog.i18n.currency.CurrencyInfo[key] =93goog.i18n.currency.CurrencyInfoTier2[key];94}95goog.i18n.currency.tier2Enabled_ = true;96}97};9899100/**101* Deprecated.102* Global currency pattern always uses ISO-4217 currency code as prefix. Local103* currency sign is added if it is different from currency code. Each currency104* is unique in this form. The negative side is that ISO code looks weird in105* some countries as people normally do not use it. Local currency sign106* alleviates the problem, but also makes it a little verbose.107*108* @param {string} currencyCode ISO-4217 3-letter currency code.109* @return {string} Global currency pattern string for given currency.110* @deprecated Format numbers using {@link goog.i18n.NumberFormat} with111* {@link goog.i18n.NumberFormat.Format.CURRENCY} and112* {@link goog.i18n.NumberFormat.CurrencyStyle.GLOBAL}113*/114goog.i18n.currency.getGlobalCurrencyPattern = function(currencyCode) {115var info = goog.i18n.currency.CurrencyInfo[currencyCode];116var patternNum = info[0];117if (currencyCode == info[1]) {118return goog.i18n.currency.getCurrencyPattern_(patternNum, info[1]);119}120return currencyCode + ' ' +121goog.i18n.currency.getCurrencyPattern_(patternNum, info[1]);122};123124125/**126* Return global currency sign string for those applications127* that want to handle currency sign themselves.128*129* @param {string} currencyCode ISO-4217 3-letter currency code.130* @return {string} Global currency sign for given currency.131*/132goog.i18n.currency.getGlobalCurrencySign = function(currencyCode) {133var info = goog.i18n.currency.CurrencyInfo[currencyCode];134return (currencyCode == info[1]) ? currencyCode :135currencyCode + ' ' + info[1];136};137138139/**140* Deprecated.141* Local currency pattern is the most frequently used pattern in currency's142* native region. It does not care about how it is distinguished from other143* currencies.144*145* @param {string} currencyCode ISO-4217 3-letter currency code.146* @return {string} Local currency pattern string for given currency.147* @deprecated Format numbers using {@link goog.i18n.NumberFormat} with148* {@link goog.i18n.NumberFormat.Format.CURRENCY} and149* {@link goog.i18n.NumberFormat.CurrencyStyle.LOCAL}150*/151goog.i18n.currency.getLocalCurrencyPattern = function(currencyCode) {152var info = goog.i18n.currency.CurrencyInfo[currencyCode];153return goog.i18n.currency.getCurrencyPattern_(info[0], info[1]);154};155156157/**158* Returns local currency sign string for those applications that need to159* handle currency sign separately.160*161* @param {string} currencyCode ISO-4217 3-letter currency code.162* @return {string} Local currency sign for given currency.163*/164goog.i18n.currency.getLocalCurrencySign = function(currencyCode) {165return goog.i18n.currency.CurrencyInfo[currencyCode][1];166};167168169/**170* Deprecated.171* Portable currency pattern is a compromise between local and global. It is172* not a mere blend or mid-way between the two. Currency sign is chosen so that173* it looks familiar to native users. It also has enough information to174* distinguish itself from other popular currencies in its native region.175* In this pattern, currency sign symbols that has availability problem in176* popular fonts are also avoided.177*178* @param {string} currencyCode ISO-4217 3-letter currency code.179* @return {string} Portable currency pattern string for given currency.180* @deprecated Format numbers using {@link goog.i18n.NumberFormat} with181* {@link goog.i18n.NumberFormat.Format.CURRENCY} and182* {@link goog.i18n.NumberFormat.CurrencyStyle.PORTABLE}183*/184goog.i18n.currency.getPortableCurrencyPattern = function(currencyCode) {185var info = goog.i18n.currency.CurrencyInfo[currencyCode];186return goog.i18n.currency.getCurrencyPattern_(info[0], info[2]);187};188189190/**191* Return portable currency sign string for those applications that need to192* handle currency sign themselves.193*194* @param {string} currencyCode ISO-4217 3-letter currency code.195* @return {string} Portable currency sign for given currency.196*/197goog.i18n.currency.getPortableCurrencySign = function(currencyCode) {198return goog.i18n.currency.CurrencyInfo[currencyCode][2];199};200201202/**203* This function returns the default currency sign's position. Some applications204* may want to handle currency sign and currency amount separately. This205* function can be used in such situations to correctly position the currency206* sign relative to the amount.207*208* Use {@link goog.i18n.NumberFormat#isCurrencyCodeBeforeValue} for a locale209* aware version of this API (recommended). isPrefixSignPosition() returns the210* default currency sign's position in the currency's default locale (e.g. 'en'211* for 'USD'), but most commonly the position is needed for the locale in which212* the number is going to be displayed. For example, in 'fr' 10.10 USD would be213* displayed as '10,10 $'.214*215* @param {string} currencyCode ISO-4217 3-letter currency code.216* @return {boolean} true if currency should be positioned before amount field.217*/218goog.i18n.currency.isPrefixSignPosition = function(currencyCode) {219return (goog.i18n.currency.CurrencyInfo[currencyCode][0] &220goog.i18n.currency.POSITION_FLAG_) == 0;221};222223224/**225* This function constructs the currency pattern. Currency sign is provided. The226* pattern information is encoded in patternNum.227*228* @param {number} patternNum Encoded pattern number that has229* currency pattern information.230* @param {string} sign The currency sign that will be used in pattern.231* @return {string} currency pattern string.232* @private233*/234goog.i18n.currency.getCurrencyPattern_ = function(patternNum, sign) {235var strParts = ['#,##0'];236var precision = patternNum & goog.i18n.currency.PRECISION_MASK_;237if (precision > 0) {238strParts.push('.');239for (var i = 0; i < precision; i++) {240strParts.push('0');241}242}243if ((patternNum & goog.i18n.currency.POSITION_FLAG_) == 0) {244strParts.unshift(245(patternNum & goog.i18n.currency.SPACE_FLAG_) ? "' " : "'");246strParts.unshift(sign);247strParts.unshift("'");248} else {249strParts.push(250(patternNum & goog.i18n.currency.SPACE_FLAG_) ? " '" : "'", sign, "'");251}252return strParts.join('');253};254255256/**257* Modify currency pattern string by adjusting precision for given currency.258* Standard currency pattern will have 2 digit after decimal point.259* Examples:260* $#,##0.00 -> $#,##0 (precision == 0)261* $#,##0.00 -> $#,##0.0 (precision == 1)262* $#,##0.00 -> $#,##0.000 (precision == 3)263*264* @param {string} pattern currency pattern string.265* @param {string} currencyCode 3-letter currency code.266* @return {string} modified currency pattern string.267*/268goog.i18n.currency.adjustPrecision = function(pattern, currencyCode) {269var strParts = ['0'];270var info = goog.i18n.currency.CurrencyInfo[currencyCode];271var precision = info[0] & goog.i18n.currency.PRECISION_MASK_;272if (precision > 0) {273strParts.push('.');274for (var i = 0; i < precision; i++) {275strParts.push('0');276}277}278return pattern.replace(/0.00/g, strParts.join(''));279};280281282/**283* Tier 1 currency information.284*285* The first number in the array is a combination of the precision mask and286* other flags. The precision mask indicates how many decimal places to show for287* the currency. Valid values are [0..7]. The position flag indicates whether288* the currency sign should be positioned after the number. Valid values are 0289* (before the number) or 16 (after the number). The space flag indicates290* whether a space should be inserted between the currency sign and number.291* Valid values are 0 (no space) and 32 (space).292*293* The number in the array is calculated by adding together the mask and flag294* values. For example:295*296* 0: no precision (0), currency sign first (0), no space (0)297* 2: two decimals precision (2), currency sign first (0), no space (0)298* 18: two decimals precision (2), currency sign last (16), no space (0)299* 50: two decimals precision (2), currency sign last (16), space (32)300*301* It's not recommended to read this data directly. Format numbers using302* {@link goog.i18n.NumberFormat} with303* {@link goog.i18n.NumberFormat.Format.CURRENCY} instead.304*305* @const {!Object<!Array<?>>}306*/307goog.i18n.currency.CurrencyInfo = {308'AED': [2, 'dh', '\u062f.\u0625.', 'DH'],309'ALL': [0, 'Lek', 'Lek'],310'AUD': [2, '$', 'AU$'],311'BDT': [2, '\u09F3', 'Tk'],312'BGN': [2, 'lev', 'lev'],313'BRL': [2, 'R$', 'R$'],314'CAD': [2, '$', 'C$'],315'CDF': [2, 'FrCD', 'CDF'],316'CHF': [2, 'CHF', 'CHF'],317'CLP': [0, '$', 'CL$'],318'CNY': [2, '¥', 'RMB¥'],319'COP': [32, '$', 'COL$'],320'CRC': [0, '\u20a1', 'CR\u20a1'],321'CZK': [50, 'K\u010d', 'K\u010d'],322'DKK': [50, 'kr.', 'kr.'],323'DOP': [2, 'RD$', 'RD$'],324'EGP': [2, '£', 'LE'],325'ETB': [2, 'Birr', 'Birr'],326'EUR': [2, '€', '€'],327'GBP': [2, '£', 'GB£'],328'HKD': [2, '$', 'HK$'],329'HRK': [2, 'kn', 'kn'],330'HUF': [34, 'Ft', 'Ft'],331'IDR': [0, 'Rp', 'Rp'],332'ILS': [34, '\u20AA', 'IL\u20AA'],333'INR': [2, '\u20B9', 'Rs'],334'IRR': [0, 'Rial', 'IRR'],335'ISK': [0, 'kr', 'kr'],336'JMD': [2, '$', 'JA$'],337'JPY': [0, '¥', 'JP¥'],338'KRW': [0, '\u20A9', 'KR₩'],339'LKR': [2, 'Rs', 'SLRs'],340'LTL': [2, 'Lt', 'Lt'],341'MNT': [0, '\u20AE', 'MN₮'],342'MVR': [2, 'Rf', 'MVR'],343'MXN': [2, '$', 'Mex$'],344'MYR': [2, 'RM', 'RM'],345'NOK': [50, 'kr', 'NOkr'],346'PAB': [2, 'B/.', 'B/.'],347'PEN': [2, 'S/.', 'S/.'],348'PHP': [2, '\u20B1', 'PHP'],349'PKR': [0, 'Rs', 'PKRs.'],350'PLN': [50, 'z\u0142', 'z\u0142'],351'RON': [2, 'RON', 'RON'],352'RSD': [0, 'din', 'RSD'],353'RUB': [50, '\u20bd', 'RUB'],354'SAR': [2, 'Rial', 'Rial'],355'SEK': [50, 'kr', 'kr'],356'SGD': [2, '$', 'S$'],357'THB': [2, '\u0e3f', 'THB'],358'TRY': [2, 'TL', 'YTL'],359'TWD': [2, 'NT$', 'NT$'],360'TZS': [0, 'TSh', 'TSh'],361'UAH': [2, 'грн.', 'UAH'],362'USD': [2, '$', 'US$'],363'UYU': [2, '$', '$U'],364'VND': [48, '\u20AB', 'VN\u20AB'],365'YER': [0, 'Rial', 'Rial'],366'ZAR': [2, 'R', 'ZAR']367};368369370/**371* Tier 2 currency information.372*373* It's not recommended to read this data directly. Format numbers using374* {@link goog.i18n.NumberFormat} with375* {@link goog.i18n.NumberFormat.Format.CURRENCY} instead.376*377* @const {!Object<!Array<?>>}378*/379goog.i18n.currency.CurrencyInfoTier2 = {380'AFN': [48, 'Af.', 'AFN'],381'AMD': [32, 'Dram', 'dram'],382'ANG': [2, 'NAf.', 'ANG'],383'AOA': [2, 'Kz', 'Kz'],384'ARS': [34, '$', 'AR$'],385'AWG': [2, 'Afl.', 'Afl.'],386'AZN': [34, '\u20bc', 'AZN'],387'BAM': [2, 'KM', 'KM'],388'BBD': [2, '$', 'Bds$'],389'BHD': [3, 'din', 'din'],390'BIF': [0, 'FBu', 'FBu'],391'BMD': [2, '$', 'BD$'],392'BND': [2, '$', 'B$'],393'BOB': [2, 'Bs', 'Bs'],394'BSD': [2, '$', 'BS$'],395'BTN': [2, 'Nu.', 'Nu.'],396'BWP': [2, 'P', 'pula'],397'BYR': [48, 'p.', 'BYR'],398'BZD': [2, '$', 'BZ$'],399'CUC': [1, '$', 'CUC$'],400'CUP': [2, '$', 'CU$'],401'CVE': [2, 'CVE', 'Esc'],402'DJF': [0, 'Fdj', 'Fdj'],403'DZD': [2, 'din', 'din'],404'ERN': [2, 'Nfk', 'Nfk'],405'FJD': [2, '$', 'FJ$'],406'FKP': [2, '£', 'FK£'],407'GEL': [2, 'GEL', 'GEL'],408'GHS': [2, 'GHS', 'GHS'],409'GIP': [2, '£', 'GI£'],410'GMD': [2, 'GMD', 'GMD'],411'GNF': [0, 'FG', 'FG'],412'GTQ': [2, 'Q', 'GTQ'],413'GYD': [0, '$', 'GY$'],414'HNL': [2, 'L', 'HNL'],415'HTG': [2, 'HTG', 'HTG'],416'IQD': [0, 'din', 'IQD'],417'JOD': [3, 'din', 'JOD'],418'KES': [2, 'Ksh', 'Ksh'],419'KGS': [2, 'KGS', 'KGS'],420'KHR': [2, 'Riel', 'KHR'],421'KMF': [0, 'CF', 'KMF'],422'KPW': [0, '\u20A9KP', 'KPW'],423'KWD': [3, 'din', 'KWD'],424'KYD': [2, '$', 'KY$'],425'KZT': [2, '\u20B8', 'KZT'],426'LAK': [0, '\u20AD', '\u20AD'],427'LBP': [0, 'L£', 'LBP'],428'LRD': [2, '$', 'L$'],429'LSL': [2, 'LSL', 'LSL'],430'LYD': [3, 'din', 'LD'],431'MAD': [2, 'dh', 'MAD'],432'MDL': [2, 'MDL', 'MDL'],433'MGA': [0, 'Ar', 'MGA'],434'MKD': [2, 'din', 'MKD'],435'MMK': [0, 'K', 'MMK'],436'MOP': [2, 'MOP', 'MOP$'],437'MRO': [0, 'MRO', 'MRO'],438'MUR': [0, 'MURs', 'MURs'],439'MWK': [2, 'MWK', 'MWK'],440'MZN': [2, 'MTn', 'MTn'],441'NAD': [2, '$', 'N$'],442'NGN': [2, '\u20A6', 'NG\u20A6'],443'NIO': [2, 'C$', 'C$'],444'NPR': [2, 'Rs', 'NPRs'],445'NZD': [2, '$', 'NZ$'],446'OMR': [3, 'Rial', 'OMR'],447'PGK': [2, 'PGK', 'PGK'],448'PYG': [16, 'Gs.', 'PYG'],449'QAR': [2, 'Rial', 'QR'],450'RWF': [0, 'RF', 'RF'],451'SBD': [2, '$', 'SI$'],452'SCR': [2, 'SCR', 'SCR'],453'SDG': [2, 'SDG', 'SDG'],454'SHP': [2, '£', 'SH£'],455'SLL': [0, 'SLL', 'SLL'],456'SOS': [0, 'SOS', 'SOS'],457'SRD': [2, '$', 'SR$'],458'SSP': [2, '£', 'SSP'],459'STD': [0, 'Db', 'Db'],460'SYP': [0, '£', 'SY£'],461'SZL': [2, 'SZL', 'SZL'],462'TJS': [2, 'Som', 'TJS'],463'TND': [3, 'din', 'DT'],464'TOP': [2, 'T$', 'T$'],465'TTD': [2, '$', 'TT$'],466'UGX': [0, 'UGX', 'UGX'],467'UZS': [0, 'so\u02bcm', 'UZS'],468'VEF': [2, 'Bs', 'Bs'],469'VUV': [0, 'VUV', 'VUV'],470'WST': [2, 'WST', 'WST'],471'XAF': [0, 'FCFA', 'FCFA'],472'XCD': [2, '$', 'EC$'],473'XOF': [0, 'CFA', 'CFA'],474'XPF': [48, 'FCFP', 'FCFP'],475'ZMW': [0, 'ZMW', 'ZMW'],476'ZWD': [0, '$', 'Z$']477};478479480