From d7ff9b48279d6b8f94089cf36fde6e8290fb631d Mon Sep 17 00:00:00 2001 From: idfx Date: Sun, 17 Mar 2002 21:32:33 +0000 Subject: [PATCH] i18n-feature based on kellans proposal implemented. the resource-bundles are located in the directory mir/bundles and build.xml copies them to WEB-INF/classes/. to get a localized message just type ${message(key)} into a admintemplate. in the properties-file the hash look like key=value. the properties-files are named in the following way: admin_??.properties ( ?? stands for the languagecode ) . the admin.properties without languagecode is the standard-bundle --- build.xml | 3 + build.xml-new | 3 + bundles/admin.properties | 1 + bundles/admin_de.properties | 1 + source/Mir.java | 28 +++--- source/OpenMir.java | 21 ++-- source/mir/misc/HTMLTemplateProcessor.java | 43 +++++---- source/mir/misc/MessageMethodModel.java | 106 ++++++++++----------- source/mir/servlet/ServletModule.java | 8 +- source/mircoders/producer/Producer.java | 2 +- source/mircoders/servlet/ServletModuleComment.java | 3 +- source/mircoders/servlet/ServletModuleImages.java | 3 +- .../mircoders/servlet/ServletModuleLinksImcs.java | 6 +- source/mircoders/servlet/ServletModuleMessage.java | 3 +- 14 files changed, 131 insertions(+), 100 deletions(-) create mode 100755 bundles/admin.properties create mode 100755 bundles/admin_de.properties diff --git a/build.xml b/build.xml index 11c97f75..2b325ec1 100755 --- a/build.xml +++ b/build.xml @@ -71,6 +71,9 @@ + + + diff --git a/build.xml-new b/build.xml-new index 6dd605e6..1608988d 100755 --- a/build.xml-new +++ b/build.xml-new @@ -110,6 +110,9 @@ + + + diff --git a/bundles/admin.properties b/bundles/admin.properties new file mode 100755 index 00000000..b400b0c7 --- /dev/null +++ b/bundles/admin.properties @@ -0,0 +1 @@ +hello=Hello \ No newline at end of file diff --git a/bundles/admin_de.properties b/bundles/admin_de.properties new file mode 100755 index 00000000..cdf9a496 --- /dev/null +++ b/bundles/admin_de.properties @@ -0,0 +1 @@ +hello=halloli \ No newline at end of file diff --git a/source/Mir.java b/source/Mir.java index 123f32a6..abd442ab 100755 --- a/source/Mir.java +++ b/source/Mir.java @@ -159,10 +159,11 @@ public class Mir extends AbstractServlet { ServletModuleDispatch.dispatch(smod,req,res); } catch (ServletModuleException e) { - handleError(res, res.getWriter(), "ServletException in Module " + moduleName + " -- " + e.toString()); + handleError(req, res, res.getWriter(), + "ServletException in Module " + moduleName + " -- " + e.toString()); } catch (ServletModuleUserException e) { - handleUserError(res,res.getWriter(), "User error" + e.toString()); + handleUserError(req,res,res.getWriter(), "User error" + e.toString()); } // timing... @@ -207,14 +208,16 @@ public class Mir extends AbstractServlet { } - private void handleError(HttpServletResponse res, PrintWriter out, String errorString) { + private void handleError(HttpServletRequest req, HttpServletResponse res, + PrintWriter out, String errorString) { try { theLog.printError(errorString); SimpleHash modelRoot = new SimpleHash(); modelRoot.put("errorstring", new SimpleScalar(errorString)); modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); - HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"),modelRoot,out); + HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"), + modelRoot,out,req.getLocale()); out.close(); } catch (Exception e) { @@ -222,14 +225,15 @@ public class Mir extends AbstractServlet { } } - private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) { - + private void handleUserError(HttpServletRequest req, HttpServletResponse res, + PrintWriter out, String errorString) { try { theLog.printError(errorString); SimpleHash modelRoot = new SimpleHash(); modelRoot.put("errorstring", new SimpleScalar(errorString)); modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); - HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out); + HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"), + modelRoot,out, req.getLocale()); out.close(); } catch (Exception e) { @@ -263,9 +267,10 @@ public class Mir extends AbstractServlet { } SimpleHash mergeData = new SimpleHash(); mergeData.put("session",sessionUrl); - HTMLTemplateProcessor.process(res,lang+"/"+loginTemplate, mergeData, out); + HTMLTemplateProcessor.process(res, lang+"/"+loginTemplate, mergeData, + out, req.getLocale()); } catch(HTMLParseException e) { - handleError(res, out, "Error in logintemplate."); + handleError(req, res, out, "Error in logintemplate."); } } @@ -279,10 +284,11 @@ public class Mir extends AbstractServlet { mergeData.put("login_user", userEntity); if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance()); mergeData.put("messages", messageModule.getByWhereClause(null, "webdb_create desc",0,10)); - HTMLTemplateProcessor.process(res,getLanguage(req,session)+"/"+startTemplate, mergeData,out); + HTMLTemplateProcessor.process(res, getLanguage(req,session)+"/"+startTemplate, + mergeData, out, req.getLocale()); } catch(Exception e) { - handleError( res,out, "error while trying to send startpage. " + e.toString()); + handleError(req, res, out, "error while trying to send startpage. " + e.toString()); } } diff --git a/source/OpenMir.java b/source/OpenMir.java index 96820b2d..1b10a98d 100755 --- a/source/OpenMir.java +++ b/source/OpenMir.java @@ -41,8 +41,6 @@ public class OpenMir extends AbstractServlet { long startTime = (new java.util.Date()).getTime(); long sessionConnectTime=0; - - // get the configration if(getServletContext().getAttribute("mir.confed") == null) { getConfig(req); @@ -58,25 +56,26 @@ public class OpenMir extends AbstractServlet { ServletModuleDispatch.dispatch(ServletModuleOpenIndy.getInstance(),req,res); } catch (ServletModuleUserException e) { - handleUserError(res,res.getWriter(), e.getMsg()); + handleUserError(req,res,res.getWriter(), e.getMsg()); } catch (ServletModuleException e){ e.printStackTrace(); - handleError(res,res.getWriter(), "OpenIndy :: ServletException in Module ServletModule -- " + e.toString()); + handleError(req,res,res.getWriter(), "OpenIndy :: ServletException in Module ServletModule -- " + e.toString()); } // timing... sessionConnectTime = new java.util.Date().getTime() - startTime; theLog.printInfo("EXECTIME (ServletModuleOpenIndy): " + sessionConnectTime + " ms"); } - private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) { - + private void handleUserError(HttpServletRequest req, HttpServletResponse res, + PrintWriter out, String errorString) { try { theLog.printError(errorString); SimpleHash modelRoot = new SimpleHash(); modelRoot.put("errorstring", new SimpleScalar(errorString)); modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); - HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out); + HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"), + modelRoot, out, req.getLocale() ); out.close(); } catch (Exception e) { @@ -85,14 +84,16 @@ public class OpenMir extends AbstractServlet { } - private void handleError(HttpServletResponse res,PrintWriter out, String errorString) { + private void handleError(HttpServletRequest req, HttpServletResponse res,PrintWriter out, String errorString) { try { theLog.printError(errorString); SimpleHash modelRoot = new SimpleHash(); modelRoot.put("errorstring", new SimpleScalar(errorString)); - modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); - HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"),modelRoot,out); + modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime( + new GregorianCalendar()))); + HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"), + modelRoot,out, req.getLocale()); out.close(); } catch (Exception e) { diff --git a/source/mir/misc/HTMLTemplateProcessor.java b/source/mir/misc/HTMLTemplateProcessor.java index bfa7558b..681f063a 100755 --- a/source/mir/misc/HTMLTemplateProcessor.java +++ b/source/mir/misc/HTMLTemplateProcessor.java @@ -13,6 +13,7 @@ import freemarker.template.*; import mir.entity.*; import mir.storage.*; import javax.servlet.http.*; +import org.apache.struts.util.MessageResources; /** @@ -30,9 +31,10 @@ public final class HTMLTemplateProcessor { private static String videoHost; private static String imageHost; private static String openAction; - protected static String producerDocRoot = MirConfig.getProp("Producer.DocRoot"); - protected static String producerStorageRoot = MirConfig.getProp("Producer.StorageRoot"); - + protected static String producerDocRoot = + MirConfig.getProp("Producer.DocRoot"); + protected static String producerStorageRoot = + MirConfig.getProp("Producer.StorageRoot"); // // init @@ -104,9 +106,10 @@ public final class HTMLTemplateProcessor { * @param out * @exception HTMLParseException */ - public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out) - throws HTMLParseException { - process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out); + public static void process(HttpServletResponse res,String templateFilename, + EntityList entList, PrintWriter out, Locale locale) + throws HTMLParseException { + process(res, templateFilename, entList, (String)null, (TemplateModelRoot)null, out, locale); } /** @@ -122,14 +125,16 @@ public final class HTMLTemplateProcessor { * @param out * @exception HTMLParseException */ - public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName, - TemplateModelRoot additionalModel, PrintWriter out) + public static void process(HttpServletResponse res,String templateFilename, + EntityList entList, String additionalModelName, + TemplateModelRoot additionalModel, PrintWriter out, + Locale locale) throws HTMLParseException { SimpleHash modelRoot = new SimpleHash(); if (entList == null) { - process(null,templateFilename, modelRoot, out); + process(null,templateFilename, modelRoot, out, locale); } else { try { modelRoot = makeSimpleHashWithEntitylistInfos(entList); @@ -138,7 +143,7 @@ public final class HTMLTemplateProcessor { if (additionalModelName != null && additionalModel != null) modelRoot.put(additionalModelName, additionalModel); - process(res,templateFilename, modelRoot, out); + process(res,templateFilename, modelRoot, out, locale); } catch (StorageObjectException e) { throw new HTMLParseException(e.toString()); } @@ -155,9 +160,10 @@ public final class HTMLTemplateProcessor { * @param out * @exception HTMLParseException */ - public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out) + public static void process(HttpServletResponse res,String templateFilename, + HashMap mergeData, PrintWriter out, Locale locale) throws HTMLParseException { - process(res,templateFilename, makeSimpleHash(mergeData), out); + process(res,templateFilename, makeSimpleHash(mergeData), out, locale); } /** @@ -169,9 +175,10 @@ public final class HTMLTemplateProcessor { * @param out * @exception HTMLParseException */ - public static void process(String templateFilename, PrintWriter out) + public static void process(String templateFilename, PrintWriter out, + Locale locale) throws HTMLParseException { - process(null,templateFilename, (TemplateModelRoot)null, out); + process(null,templateFilename, (TemplateModelRoot)null, out, locale); } @@ -185,7 +192,8 @@ public final class HTMLTemplateProcessor { * @param out * @exception HTMLParseException */ - public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out) + public static void process(HttpServletResponse res,String templateFilename, + TemplateModelRoot tmr, PrintWriter out, Locale locale) throws HTMLParseException { if (out==null) throw new HTMLParseException("no outputstream"); Template tmpl = getTemplateFor(templateFilename); @@ -222,7 +230,10 @@ public final class HTMLTemplateProcessor { SimpleHash outPutHash = new SimpleHash(); outPutHash.put("data",tmr); outPutHash.put("config", configHash); - + + MessageResources messages = MessageResources.getMessageResources("admin"); + outPutHash.put("message", new MessageMethodModel(locale, messages) ); + tmpl.process(outPutHash,out); } diff --git a/source/mir/misc/MessageMethodModel.java b/source/mir/misc/MessageMethodModel.java index 5a9322be..3abedd77 100755 --- a/source/mir/misc/MessageMethodModel.java +++ b/source/mir/misc/MessageMethodModel.java @@ -5,10 +5,10 @@ import freemarker.template.*; import org.apache.struts.util.*; /** - * A FreeMarker TemplateMethodModel that provides access to a + * A FreeMarker TemplateMethodModel that provides access to a * Struts MessageResources, for use in Interantionalized templates. * - * TODO: we probably want to be doing some memoizing or something with this class* + * TODO: we probably want to be doing some memoizing or something with this class* * @author Kellan * */ @@ -17,37 +17,37 @@ import org.apache.struts.util.*; /** * The perferred locale for this instance of MessageMethod. */ - private Locale locale; + private Locale locale; /** * The MessageResources to query, a single instances shared for * the lifetime of servlet. */ - private MessageResources messages; + private MessageResources messages; - /** - * Construct a MessageMethod that uses the JVM's default locale. - * - * @param message The MessageResources object to query - */ - public MessageMethodModel(MessageResources messages) { - this(null, messages); - } - - /** - * Construct a MessageMethod - * - * @param locale a Locale object, persumably initialized - * from users Accept-Language header field - * - * @param message The MessageResources object to query - */ - public MessageMethodModel(Locale locale, MessageResources messages) { - this.locale = locale; - this.messages = messages; - } + /** + * Construct a MessageMethod that uses the JVM's default locale. + * + * @param message The MessageResources object to query + */ + public MessageMethodModel(MessageResources messages) { + this(null, messages); + } + /** + * Construct a MessageMethod + * + * @param locale a Locale object, persumably initialized + * from users Accept-Language header field + * + * @param message The MessageResources object to query + */ + public MessageMethodModel(Locale locale, MessageResources messages) { + this.locale = locale; + this.messages = messages; + } + /** * Takes the first argument as a resource key, then looks up @@ -55,34 +55,34 @@ import org.apache.struts.util.*; * TODO: this is an alpha implementation. We should then check if we have * any keys left over, and attempt to apply MessageFormat.format. * TODO: error messages should be i18n :) - * - * @param arguments List passed in by FM, first arguement is a string used as the key + * + * @param arguments List passed in by FM, first arguement is a string used as the key */ - public TemplateModel exec(List arguments) { - if ( arguments != null ) { - String key = (String)arguments.get(0); - String mesg = messages.getMessage(locale, key); - if ( mesg == null ) { - return unknownTagScalar; - } - return new SimpleScalar(mesg); - } else { - return missingKeyScalar; - } - } - - // i'm not real clear on how this is used - kellan :) - public boolean isEmpty() { - if (messages == null) - return true; - else - return false; - } + public TemplateModel exec(List arguments) { + if ( arguments != null ) { + String key = (String)arguments.get(0); + String mesg = messages.getMessage(locale, key); + if ( mesg == null ) { + return unknownTagScalar; + } + return new SimpleScalar(mesg); + } else { + return missingKeyScalar; + } + } + + // i'm not real clear on how this is used - kellan :) + public boolean isEmpty() { + if (messages == null) + return true; + else + return false; + } - private static String errUnknownTag = "TAG NOT FOUND"; - private static String missingKey = "MESSAGE CALL WITHOUT KEY"; - private static SimpleScalar unknownTagScalar = - new SimpleScalar(errUnknownTag); - private static SimpleScalar missingKeyScalar = - new SimpleScalar(missingKey); + private static String errUnknownTag = "TAG NOT FOUND"; + private static String missingKey = "MESSAGE CALL WITHOUT KEY"; + private static SimpleScalar unknownTagScalar = + new SimpleScalar(errUnknownTag); + private static SimpleScalar missingKeyScalar = + new SimpleScalar(missingKey); } diff --git a/source/mir/servlet/ServletModule.java b/source/mir/servlet/ServletModule.java index eab5dc53..2355920a 100755 --- a/source/mir/servlet/ServletModule.java +++ b/source/mir/servlet/ServletModule.java @@ -88,7 +88,7 @@ public abstract class ServletModule { theList = mainModule.getByWhereClause(null, offset); //theList = mainModule.getByWhereClause((String)null, offset); if (theList == null || theList.getCount() == 0 || theList.getCount()>1){ - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, theList, out); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, theList, out,req.getLocale()); } else { deliver(req, res, theList.elementAt(0), templateObjektString); } @@ -268,7 +268,7 @@ public abstract class ServletModule { try { //PrintWriter out = new LineFilterWriter(res.getWriter()); PrintWriter out = res.getWriter(); - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out, req.getLocale()); out.close(); } catch (HTMLParseException e) { throw new ServletModuleException(e.toString()); @@ -293,7 +293,7 @@ public abstract class ServletModule { try { PrintWriter out = new LineFilterWriter(res.getWriter()); //PrintWriter out = res.getWriter(); - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out, req.getLocale()); out.close(); } catch (HTMLParseException e) { throw new ServletModuleException(e.toString()); @@ -315,7 +315,7 @@ public abstract class ServletModule { */ private void deliver(HttpServletResponse res,HttpServletRequest req, PrintWriter out, TemplateModelRoot rtm, String templateFilename) throws HTMLParseException { - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out, req.getLocale()); } /** diff --git a/source/mircoders/producer/Producer.java b/source/mircoders/producer/Producer.java index c8759582..2d4d638d 100755 --- a/source/mircoders/producer/Producer.java +++ b/source/mircoders/producer/Producer.java @@ -82,7 +82,7 @@ abstract public class Producer { outStream = new PrintWriter(outputFile); } - HTMLTemplateProcessor.process(null,template, model, outStream); + HTMLTemplateProcessor.process(null,template, model, outStream,null); outputFile.close(); outStream.close(); diff --git a/source/mircoders/servlet/ServletModuleComment.java b/source/mircoders/servlet/ServletModuleComment.java index 03d42350..5478e20d 100755 --- a/source/mircoders/servlet/ServletModuleComment.java +++ b/source/mircoders/servlet/ServletModuleComment.java @@ -117,7 +117,8 @@ public class ServletModuleComment extends ServletModule } } // raus damit - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter()); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, + mergeData, res.getWriter(), req.getLocale()); } catch (ModuleException e) {throw new ServletModuleException(e.toString());} catch (IOException e) {throw new ServletModuleException(e.toString());} diff --git a/source/mircoders/servlet/ServletModuleImages.java b/source/mircoders/servlet/ServletModuleImages.java index 89df034e..75cf2009 100755 --- a/source/mircoders/servlet/ServletModuleImages.java +++ b/source/mircoders/servlet/ServletModuleImages.java @@ -284,7 +284,8 @@ public class ServletModuleImages extends mir.servlet.ServletModule } } // raus damit - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter()); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, + mergeData, res.getWriter(),req.getLocale()); } catch (ModuleException e) {throw new ServletModuleException(e.toString());} catch (IOException e) {throw new ServletModuleException(e.toString());} diff --git a/source/mircoders/servlet/ServletModuleLinksImcs.java b/source/mircoders/servlet/ServletModuleLinksImcs.java index 2a5b0517..f100489b 100755 --- a/source/mircoders/servlet/ServletModuleLinksImcs.java +++ b/source/mircoders/servlet/ServletModuleLinksImcs.java @@ -66,7 +66,8 @@ public class ServletModuleLinksImcs extends ServletModule modelRoot.put("languagelist",HTMLTemplateProcessor.makeSimpleList(theLanguageList)); if (theParentList == null || theParentList.getCount() == 0 || theParentList.getCount()>1){ - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateObjektString, modelRoot, res.getWriter()); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateObjektString, + modelRoot, res.getWriter(), req.getLocale()); } else { deliver(req, res, modelRoot, templateObjektString); } @@ -151,7 +152,8 @@ public class ServletModuleLinksImcs extends ServletModule if (theImcsList.hasPrevBatch()) modelRoot.put("prev", (new Integer(theImcsList.getPrevBatch())).toString()); - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, modelRoot, res.getWriter()); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, + modelRoot, res.getWriter(), req.getLocale()); } catch (Exception e) { throw new ServletModuleException(e.toString()); diff --git a/source/mircoders/servlet/ServletModuleMessage.java b/source/mircoders/servlet/ServletModuleMessage.java index ec117036..65da2f1e 100755 --- a/source/mircoders/servlet/ServletModuleMessage.java +++ b/source/mircoders/servlet/ServletModuleMessage.java @@ -68,7 +68,8 @@ public class ServletModuleMessage extends ServletModule mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString()); // raus damit - HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter()); + HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, + mergeData, res.getWriter(), req.getLocale()); } catch (ModuleException e) {throw new ServletModuleException(e.toString());} -- 2.11.0