froehlich 02/04/20 02:37:06 Modified: src/java/org/apache/cocoon/transformation I18nTransformer.java Log: applied patch from [EMAIL PROTECTED] (Michael Enke) patch for I18nTransformer for some number attributes Revision Changes Path 1.14 +53 -32 xml-cocoon2/src/java/org/apache/cocoon/transformation/I18nTransformer.java Index: I18nTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/I18nTransformer.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- I18nTransformer.java 22 Feb 2002 07:03:56 -0000 1.13 +++ I18nTransformer.java 20 Apr 2002 09:37:06 -0000 1.14 @@ -225,7 +225,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a> * @author <a href="mailto:[EMAIL PROTECTED]">Michael Enke</a> * @author <a href="mailto:[EMAIL PROTECTED]">Lassi Immonen</a> - * @version CVS $Id: I18nTransformer.java,v 1.13 2002/02/22 07:03:56 cziegeler Exp $ + * @version CVS $Id: I18nTransformer.java,v 1.14 2002/04/20 09:37:06 froehlich Exp $ * * @todo Move all formatting/parsing routines to I18nUtils */ @@ -270,12 +270,9 @@ * The param can have i18n:text as its value to provide multilungual value. * Parameters can have additional attributes to be used for formatting: * <ul> - * <li><code>type</code> - can be <code>date, date-time, time or - * number</code>. Used to format params before substitution. - * </li> - * <li><code>sub-type</code> - can be <code>currency, percent</code> - * and used with <code>number</code> type to format a given number - * value as a currency or percent. + * <li><code>type</code> - can be <code>date, date-time, time, + * number, currency, currency-no-unit or percent</code>. + * Used to format params before substitution. * </li> * <li><code>value</code> - the value of the param. If no value is * specified then the text inside of the param element will be used. @@ -385,7 +382,7 @@ /** * i18n:number is used to provide a localized number string. Allowed - * attributes are: <code>pattern, src-pattern, locale, src-locale, sub-type + * attributes are: <code>pattern, src-pattern, locale, src-locale, type * </code> * Usage examples: * <pre> @@ -393,7 +390,7 @@ * 1000.0 * </i18n:number> * - * <i18n:number sub-type="currency" /> + * <i18n:number type="currency" /> * </pre> * * If no value is specifies then 0 will be used. @@ -405,21 +402,21 @@ */ public static final String I18N_NUMBER_ELEMENT = "number"; - /** @todo Implement currency element */ + /** currency element */ public static final String I18N_CURRENCY_ELEMENT = "currency"; - /** @todo Implement percent element */ + /** percent element */ public static final String I18N_PERCENT_ELEMENT = "percent"; - /** @todo Implemement integer currency element */ + /** integer currency element */ public static final String I18N_INT_CURRENCY_ELEMENT = "int-currency"; - /** @todo Implement currency without unit element */ + /** currency without unit element */ public static final String I18N_CURRENCY_NO_UNIT_ELEMENT = "currency-no-unit"; - /** @todo Implement integer currency without unit element */ + /** integer currency without unit element */ public static final String I18N_INT_CURRENCY_NO_UNIT_ELEMENT = "int-currency-no-unit"; @@ -516,14 +513,7 @@ */ public static final String I18N_TYPE_ATTRIBUTE = "type"; - /** - * This attribute is used with <code>i18:number</code> to - * indicate a sub-type: <code>currency</code>, <code>int-currency</code> - * or <code>percent</code>. - */ - public static final String I18N_SUB_TYPE_ATTRIBUTE = "sub-type"; - - // Ņonfiguration parameters + // Configuration parameters /** * This configuration parameter specifies the message catalog name. @@ -563,6 +553,13 @@ // FIXME (KP): Why should it be a file? It can be any resource! private static final String FILE = "file:"; + /** + * <code>fraction-digits</code> attribute is used with + * <code>i18:number</code> to + * indicate the number of digits behind the fraction + */ + public static final String I18N_FRACTION_DIGITS_ATTRIBUTE = "fraction-digits"; + // States of the transformer private static final int STATE_OUTSIDE = 0; private static final int STATE_INSIDE_TEXT = 1; @@ -1031,9 +1028,9 @@ formattingParams.put(I18N_TYPE_ATTRIBUTE, attr_value); } - attr_value = attr.getValue(I18N_SUB_TYPE_ATTRIBUTE); - if (attr_value != null) { - formattingParams.put(I18N_SUB_TYPE_ATTRIBUTE, attr_value); + attr_value = attr.getValue( I18N_FRACTION_DIGITS_ATTRIBUTE ); + if ( attr_value != null ) { + formattingParams.put( I18N_FRACTION_DIGITS_ATTRIBUTE, attr_value ); } } @@ -1405,9 +1402,15 @@ String pattern = (String)params.get(I18N_PATTERN_ATTRIBUTE); // the number value String value = (String)params.get(I18N_VALUE_ATTRIBUTE); - // sub-type - String subType = (String)params.get(I18N_SUB_TYPE_ATTRIBUTE); - + // type + String type = (String)params.get(I18N_TYPE_ATTRIBUTE); + // fraction-digits + int fractionDigits = -1; + try { + fractionDigits = Integer.parseInt((String) params.get + ( I18N_FRACTION_DIGITS_ATTRIBUTE )); + } + catch(NumberFormatException nfe) {} // parsed number Number numberValue = null; @@ -1429,7 +1432,7 @@ char dec = from_fmt.getDecimalFormatSymbols().getDecimalSeparator(); int decAt = 0; boolean appendDec = false; - if (subType == null) { + if (type == null) { to_fmt = (DecimalFormat)NumberFormat.getInstance(loc); to_fmt.setMaximumFractionDigits(309); for (int i = value.length() - 1; @@ -1449,16 +1452,34 @@ if (value.charAt(value.length() - 1) == dec) { appendDec = true; } - } else if (subType.equals("currency")) { + } else if (type.equals( I18N_CURRENCY_ELEMENT )) { to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc); - } else if (subType.equals("int-currency")) { + } else if (type.equals( I18N_INT_CURRENCY_ELEMENT )) { to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc); int_currency = 1; for (int i = 0; i < to_fmt.getMaximumFractionDigits(); i++) { int_currency *= 10; } - } else if (subType.equals("percent")) { + } else if ( type.equals( I18N_CURRENCY_NO_UNIT_ELEMENT ) ) { + DecimalFormat tmp = (DecimalFormat) NumberFormat.getCurrencyInstance( loc ); + to_fmt = (DecimalFormat) NumberFormat.getInstance( loc ); + to_fmt.setMinimumFractionDigits(tmp.getMinimumFractionDigits()); + to_fmt.setMaximumFractionDigits(tmp.getMaximumFractionDigits()); + } else if ( type.equals( I18N_INT_CURRENCY_NO_UNIT_ELEMENT ) ) { + DecimalFormat tmp = (DecimalFormat) NumberFormat.getCurrencyInstance( loc ); + int_currency = 1; + for ( int i = 0; i < tmp.getMaximumFractionDigits(); i++ ) + int_currency *= 10; + to_fmt = (DecimalFormat) NumberFormat.getInstance( loc ); + to_fmt.setMinimumFractionDigits(tmp.getMinimumFractionDigits()); + to_fmt.setMaximumFractionDigits(tmp.getMaximumFractionDigits()); + } else if (type.equals( I18N_PERCENT_ELEMENT )) { to_fmt = (DecimalFormat)NumberFormat.getPercentInstance(loc); + } + + if(fractionDigits > -1) { + to_fmt.setMinimumFractionDigits(fractionDigits); + to_fmt.setMaximumFractionDigits(fractionDigits); } // pattern overwrites locale format
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]