From: Hendrik <nh...@us...> - 2006-12-23 18:31:19
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/server/util In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv8167/src/games/stendhal/server/util Modified Files: Translate.java Log Message: added velocity support Index: Translate.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/server/util/Translate.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Translate.java 23 Dec 2006 18:08:22 -0000 1.2 --- Translate.java 23 Dec 2006 18:31:15 -0000 1.3 *************** *** 3,10 **** import games.stendhal.common.Grammar; ! import java.io.IOException; import java.util.Properties; import org.apache.log4j.Logger; /** --- 3,13 ---- import games.stendhal.common.Grammar; ! import java.io.StringWriter; ! import java.io.Writer; import java.util.Properties; import org.apache.log4j.Logger; + import org.apache.velocity.VelocityContext; + import org.apache.velocity.app.Velocity; /** *************** *** 16,20 **** private static Logger logger = Logger.getLogger(Translate.class); private static Properties dictionary = new Properties(); ! private static Grammar grammar = null; /** --- 19,23 ---- private static Logger logger = Logger.getLogger(Translate.class); private static Properties dictionary = new Properties(); ! private static Grammar grammar = new Grammar(); /** *************** *** 24,33 **** */ public static void initLanguage(String language) { try { dictionary.load(Translate.class.getResourceAsStream("../properties/" + language + ".txt")); ! grammar = (Grammar) Class.forName(dictionary.getProperty("_grammar.class")).newInstance(); } catch (Exception e) { logger.error(e, e); } } --- 27,40 ---- */ public static void initLanguage(String language) { + Grammar tempGrammar = null; try { dictionary.load(Translate.class.getResourceAsStream("../properties/" + language + ".txt")); ! tempGrammar = (Grammar) Class.forName(dictionary.getProperty("_grammar.class")).newInstance(); } catch (Exception e) { logger.error(e, e); } + if (tempGrammar != null) { + grammar = tempGrammar; + } } *************** *** 40,49 **** */ public static String _(String text, String... args) { String translated = text; if (dictionary.get(text) != null) { translated = dictionary.getProperty(text); } ! // TODO: parse arguments ! return translated; } } --- 47,76 ---- */ public static String _(String text, String... args) { + + // use translated text, if one was specified String translated = text; if (dictionary.get(text) != null) { translated = dictionary.getProperty(text); } ! ! // extract arguments ! VelocityContext context = new VelocityContext(); ! context.put("grammar", grammar); ! int i = 0; ! for (String arg : args) { ! i++; ! context.put(Integer.toString(i), arg); ! } ! ! // parse arguments and invoke scripts ! Writer writer = new StringWriter(); ! try { ! Velocity.evaluate(context, writer, "errors", translated); ! } catch (Exception e) { ! logger.error(e, e); ! } ! ! // return result ! return writer.toString(); } } |