Author: bpoussin Date: 2008-08-01 14:22:01 +0000 (Fri, 01 Aug 2008) New Revision: 957 Added: trunk/lutinutil/src/main/java/org/codelutin/i18n/I18nf.java Log: ajout d'une nouvelle classe qui utilise String.format au lieu de MessageFormat Added: trunk/lutinutil/src/main/java/org/codelutin/i18n/I18nf.java =================================================================== --- trunk/lutinutil/src/main/java/org/codelutin/i18n/I18nf.java (rev 0) +++ trunk/lutinutil/src/main/java/org/codelutin/i18n/I18nf.java 2008-08-01 14:22:01 UTC (rev 957) @@ -0,0 +1,84 @@ +/* *##% + * Copyright (C) 2002-2008 Code Lutin, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + *##%*/ + +package org.codelutin.i18n; + + +import java.util.Arrays; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Transition class during project switch between MessageFormat.format syntax + * to String.format syntax. + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class I18nf extends I18n { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(I18nf.class); + /** + * Retourne la chaine traduite si possible. + * + * @param message message formate avec la meme syntaxe que {@link String#format} + * @param args les parametres pour le message. + * @return la traduction si possible ou la chaine passee en parametre + * sinon. + */ + public static String _(String message, Object... args) { + String result = message; + Language language = loader == null ? null : loader.getLanguage(); + if (language != null) { + result = language.translate(message); + } + try { + return applyFilter(String.format(result, args)); + } catch (Exception eee) { + try { + return applyFilter(String.format(message, args)); + } catch (Exception zzz) { + log.warn(I18n._("lutinutil.error.i18n.untranslated.message", message)); + return applyFilter(message); + } + } + } + + /** + * Retourne la chaine passée en argument. + * + * @param message message formate avec la meme syntaxe que {@link + * MessageFormat} + * @param args les parametres pour le message. + * @return le message passe en argument mais formatte + * avec les parametres + */ + public static String n_(String message, Object... args) { + try { + return String.format(message, args); + } catch (Exception eee) { + log.warn(I18n._("lutinutil.error.i18n.unformated.message", message, Arrays.toString(args))); + return message; + } + } +}