You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gc...@apache.org on 2014/07/24 22:16:06 UTC

svn commit: r1613267 - in /myfaces/trinidad/trunk: trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/ trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/

Author: gcrawford
Date: Thu Jul 24 20:16:06 2014
New Revision: 1613267

URL: http://svn.apache.org/r1613267
Log:
TRINIDAD-2495
af:convertnumber: currencysymbol and currencycode support is broken

thanks to ashwin

Modified:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/NumberConverter.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
    myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/NumberConverter.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/NumberConverter.java?rev=1613267&r1=1613266&r2=1613267&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/NumberConverter.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/convert/NumberConverter.java Thu Jul 24 20:16:06 2014
@@ -222,6 +222,12 @@ public class NumberConverter extends jav
     df.setParseBigDecimal(true); // TODO What does this do?
     DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
     
+    if("currency".equals(type))
+    {
+      // Setup custom currency code/symbol if any
+      _setCurrencyFormattingProperties(reqCtx, fmt);
+    }
+    
     // We change the grouping_separator b/c TRINIDAD-849
     // source is this JDK bug: 4510618.
     boolean changed = false;

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js?rev=1613267&r1=1613266&r2=1613267&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js Thu Jul 24 20:16:06 2014
@@ -58,7 +58,7 @@ function TrNumberConverter(
     this._groupingUsed = true;
     
   //init the TrNumberFormat
-  this._initNumberFormat(locale);
+  this._initNumberFormat(locale, currencyCode, currencySymbol);
   
   // for debugging
   this._class = "TrNumberConverter";
@@ -177,21 +177,7 @@ TrNumberConverter.prototype.getAsString 
   {
     if(this._type=="percent" || this._type=="currency")
     {
-      var string = this._numberFormat.format(number);
-      if(this._type=="currency")
-      {
-        //In Trinidad the currencyCode gets preference over currencySymbol
-        //this is similar on the server-side
-        if(this._currencyCode)
-        {
-          string = string.replace(getLocaleSymbols().getCurrencyCode(), this._currencyCode);
-        }
-        else if(this._currencySymbol)
-        {
-          string = string.replace(getLocaleSymbols().getCurrencySymbol(), this._currencySymbol);
-        }
-      }
-      return string;
+      return this._numberFormat.format(number);
     }
     else
     {
@@ -362,8 +348,17 @@ TrNumberConverter.prototype._isConvertib
 
 /**
  * runs the creation of the used TrNumberFormat class
+ * @param locale Locale object
+ * @param currencyCode The ISO 4217 currency code, applied when formatting currencies. 
+ * This currency code will substitute the locale's default currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencyCode is determined by the locale.
+ * @param currencySymbol Currency symbol applied when formatting currencies.
+ * If currency code is set then symbol will be ignored. This currency sybmol will substitute the locale's default 
+ * currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencySymbol is determined by the locale.
+
  */
-TrNumberConverter.prototype._initNumberFormat = function(locale)
+TrNumberConverter.prototype._initNumberFormat = function(locale, currencyCode, currencySymbol)
 {
   if(this._type=="percent")
   {
@@ -373,7 +368,7 @@ TrNumberConverter.prototype._initNumberF
   else if(this._type=="currency")
   {
     this._example = 10250;
-    this._numberFormat = TrNumberFormat.getCurrencyInstance(locale);
+    this._numberFormat = TrNumberFormat.getCurrencyInstance(locale, currencyCode, currencySymbol);
   }
   else if(this._type=="number")
   {

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js?rev=1613267&r1=1613266&r2=1613267&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js Thu Jul 24 20:16:06 2014
@@ -16,28 +16,37 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-
+ 
 /**
- * constructor for TrNumberFormat.
+ * Constructor for TrNumberFormat. 
+ * @param type Can be one of: 'number', 'percent' or 'currency'
+ * @param locale Locale object
+ * @param currencyCode The ISO 4217 currency code, applied when formatting currencies. 
+ * This currency code will substitute the locale's default currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencyCode is determined by the locale.
+ * @param currencySymbol Currency symbol applied when formatting currencies.
+ * If currency code is set then symbol will be ignored. This currency sybmol will substitute the locale's default 
+ * currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencySymbol is determined by the locale.
  */
- function TrNumberFormat(type, locale)
+function TrNumberFormat(type, locale, currencyCode, currencySymbol)
 {
   if(!type)
     alert("type for TrNumberFormat not defined!");
   this._type = type;
-  
-  this._localeSymbols = getLocaleSymbols(locale);
-  this._pPre = this._localeSymbols.getPositivePrefix();
-  this._pSuf = this._localeSymbols.getPositiveSuffix();
-  this._nPre = this._localeSymbols.getNegativePrefix();
-  this._nSuf = this._localeSymbols.getNegativeSuffix();
 
   //default values, similar to JDK (values from Apache Harmony)
   if(this._type=="percent")
+  {
     this._maxFractionDigits = 0;
+  }
   else
+  {
     this._maxFractionDigits = 3;
+  }
+    
   this._maxIntegerDigits  = 40;
+  
   if(this._type=="currency")
   {
     this._minFractionDigits = 2;
@@ -46,9 +55,11 @@
   {
     this._minFractionDigits = 0;
   }
+  
   this._minIntegerDigits  = 1;
-  this._groupingUsed = true;
+  this._groupingUsed = true;  
   
+  this._updateLocaleAndCurrencySymbols(locale, currencyCode, currencySymbol);
 }
 //***********************
 // static
@@ -63,11 +74,19 @@ TrNumberFormat.getNumberInstance = funct
 }
 
 /**
- * Returns a currency formater.
+ * Returns a currency formatter
+ * @param locale Locale object
+ * @param currencyCode The ISO 4217 currency code, applied when formatting currencies. 
+ * This currency code will substitute the locale's default currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencyCode is determined by the locale.
+ * @param currencySymbol Currency symbol applied when formatting currencies.
+ * If currency code is set then symbol will be ignored. This currency sybmol will substitute the locale's default 
+ * currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencySymbol is determined by the locale.
  */
-TrNumberFormat.getCurrencyInstance = function(locale)
+TrNumberFormat.getCurrencyInstance = function(locale, currencyCode, currencySymbol)
 {
-  return new TrNumberFormat("currency", locale);
+  return new TrNumberFormat("currency", locale, currencyCode, currencySymbol);
 }
 
 /**
@@ -654,6 +673,70 @@ TrNumberFormat.prototype.getZeros = func
 //***********************
 
 /**
+ * Updates the locale symbols and Currency prefix and suffixes
+ * @param locale Locale object
+ * @param currencyCode The ISO 4217 currency code, applied when formatting currencies. 
+ * This currency code will substitute the locale's default currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencyCode is determined by the locale.
+ * @param currencySymbol Currency symbol applied when formatting currencies.
+ * If currency code is set then symbol will be ignored. This currency sybmol will substitute the locale's default 
+ * currency symbol for number formatting, provided type is set to 'currency'.
+ * However the placement of the currencySymbol is determined by the locale.
+ */
+TrNumberFormat.prototype._updateLocaleAndCurrencySymbols = function(locale, currencyCode, currencySymbol) 
+{
+  this._localeSymbols = getLocaleSymbols(locale);
+  
+  // prefix and suffix format for positive currency data
+  // This will mostly contain just the currency symbol of the specified locale,
+  // however presence of additional formatting characters like + cannot be ruled out
+  this._pPre = this._localeSymbols.getPositivePrefix();
+  this._pSuf = this._localeSymbols.getPositiveSuffix();
+  
+  // prefix and suffix format for negative currency data
+  // In addition to the currency symbol of the specified locale,
+  // the prefix and suffix can contain some additioanl formatting characters like (,),- 
+  this._nPre = this._localeSymbols.getNegativePrefix();
+  this._nSuf = this._localeSymbols.getNegativeSuffix();
+
+  var localeDefaultCurSymb = this._localeSymbols.getCurrencySymbol();
+  var localeDefaultCurCode = this._localeSymbols.getCurrencyCode();
+  
+  if (currencyCode) 
+  {
+    // First check if the locale default currency code matches the custom currency code 
+    // If so, we want to retain the localized currency symbol
+    // Example: If locale is en_US and currency code is "INR", then we use INR cas currency prefix/suffix (positioning 
+    // is determined by the formatting locale in_IN). So "INR 1,000.00" is okay
+    // But if the locale is in_IN, if the currency code is "INR", then we have the ability to display "Rs." prefix
+    // instead of generic INR prefix. So in this case we display "Rs. 1,000.00"
+    if (localeDefaultCurCode != currencyCode)
+    {
+      // if currencyCode is set we honour currency code.
+      // Replaces all occurrences of locale default currency symbol with currencyCode
+      this._replaceCurrencyPrefixAndSuffix(localeDefaultCurSymb, currencyCode);
+    }
+  } 
+  else if (currencySymbol) 
+  {
+    // if only currencySymbol is (currencyCode is null) set we honour currency symbol.
+    // Replaces all occurrences of locale default currency symbol with custom currencySymbol
+    this._replaceCurrencyPrefixAndSuffix(localeDefaultCurSymb, currencySymbol);
+  }    
+}
+
+/**
+ * Replaces the locale default currency prefix/suffix with custom currency prefix and suffix 
+ */
+TrNumberFormat.prototype._replaceCurrencyPrefixAndSuffix = function(localeDefault, customCurrency) 
+{
+  this._pPre = this._pPre.replace(localeDefault, customCurrency);
+  this._pSuf = this._pSuf.replace(localeDefault, customCurrency);
+  this._nPre = this._nPre.replace(localeDefault, customCurrency);
+  this._nSuf = this._nSuf.replace(localeDefault, customCurrency);   
+}
+
+/**
  * Formats the integer part of a number
  */
 TrNumberFormat.prototype._formatIntegers = function(ints)