[Generator-rt-devel] generator_runtime/src/java/generator/runtime/translation TranslationBean.java
Brought to you by:
rickknowles
|
From: Rick K. <ric...@us...> - 2010-04-05 02:31:50
|
Update of /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/translation In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv11801/src/java/generator/runtime/translation Added Files: TranslationBean.java TranslatingVelocityMailRenderController.java Log Message: A bunch of translation tag changes and addons to the velocity mail rendering. A fix to the DBConnectionPool keep alive process also included. --- NEW FILE: TranslatingVelocityMailRenderController.java --- /* * Keystone Development Framework * Copyright (C) 2004-2009 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * Version 2 as published by the Free Software Foundation. * * 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 Version 2 for more details. * * You should have received a copy of the GNU Library General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package generator.runtime.translation; import generator.runtime.automailer.VelocityMailRenderController; import java.util.Map; /** * Controller to render mail messages using the velocity templates in our /WEB-INF/velocity/mail/* * folder. Additionally includes an instance of the translation bean to support translation inside the template. */ public class TranslatingVelocityMailRenderController extends VelocityMailRenderController { protected Map buildVelocityContext(Map attributes) { Map velocityAtts = super.buildVelocityContext(attributes); String language = TranslationBean.getLanguage(getServerSideUserState(), attributes); velocityAtts.put(getTranslationParamName(), new TranslationBean(getServerSideApplicationState(), language)); return velocityAtts; } protected String getTranslationParamName() { return getFlowParameter("translationParamName", "translation"); } } --- NEW FILE: TranslationBean.java --- /* * Generator Runtime Servlet Framework * Copyright (C) 2004 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * Version 2 as published by the Free Software Foundation. * * 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 Version 2 for more details. * * You should have received a copy of the GNU General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package generator.runtime.translation; import generator.runtime.log.Log; import generator.runtime.log.LogManager; import generator.runtime.router.RouterConstants; import generator.runtime.servlet.ContextInitializer; import generator.runtime.session.ServerSideApplicationState; import generator.runtime.session.ServerSideUserState; import generator.runtime.utils.ApplicationProperties; import generator.runtime.utils.ParamUtils; import generator.runtime.utils.StringUtils; import generator.runtime.xml.XMLUtils; import java.io.File; import java.util.Hashtable; import java.util.Map; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class TranslationBean { private final Log log = LogManager.getLog(TranslationBean.class); private Map translations; public TranslationBean(ServerSideApplicationState context, String language) { String contextKey = "translationMap." + language; synchronized (context.getSemaphore(contextKey)) { this.translations = (Map) context.getAttribute(contextKey); if (this.translations == null) { ApplicationProperties props = (ApplicationProperties) context.getAttribute(ContextInitializer.ATTR_PROPS); String translationSheet = props.stringProperty("translationSheet/@" + language, "${application/@webroot}/WEB-INF/translations/" + language + ".xml"); File file = new File(translationSheet); if (!file.isFile()) { log.warning("Translation file not found: " + translationSheet); } Document doc = XMLUtils.parseFileToXML(file); NodeList items = doc.getDocumentElement().getChildNodes(); this.translations = new Hashtable(); for (int n = 0; n < items.getLength(); n++) { Node node = items.item(n); if (node.getNodeType() == Node.ELEMENT_NODE) { String key = XMLUtils.getAttributeByName(node, "key"); String text = XMLUtils.extractStringFromElement(node); if ((key != null) && (text != null)) { translations.put(key, text); } } } log.info("Loaded " + this.translations.size() + " translations for " + language + " from " + translationSheet); context.setAttribute(contextKey, this.translations); } } } public static String getLanguage(ServerSideUserState session, Map attributes) { String sessionDefault = (String) ParamUtils.nvl(session.getAttribute( RouterConstants.PREFERRED_LANGUAGE_PARAM), "english"); return ParamUtils.getParameter(attributes, RouterConstants.PREFERRED_LANGUAGE_PARAM, sessionDefault); } public String translate(String template, Object params[]) { String translated = null; if (translations != null) { // no sync required, read only translated = (String) translations.get(template); } if (translated == null) { translated = template; } if (params != null) { String tokens[][] = new String[params.length][2]; for (int n = 0; n < params.length; n++) { tokens[n][0] = "[#" + (n + 1) + "]"; tokens[n][1] = ParamUtils.nvl(params[n], "").toString(); } translated = StringUtils.stringReplace(translated, tokens); } return translated; } } |