I ended my previous blog with "Isn't there a better way"? Well...
how would I like to use error messages in Java
source code? I would simply want to write something like this:
String msg = msgcat.get("F1231: Could not open file {1} in directory {0}: {2}", dir, file, ex);The advantages are clear:
throw new IOException(msg, ex);
- No switching to a different file to add the error message
- The error message is visible right there in the source code -- easy to review!
- It's easy to see that the arguments in {0}, {1} are correct
How is this source file internationalized? We need a tool! The tool will
- locate all error messages in the code base
- generate properties files for all desired languages if they don't exist
- print out a list of all properties files that need to be localized
msgs_en_US.propertiesand
# AUTOMATICALLY GENERATED# DO NOT EDIT
# com.stc.jms.server.SegmentMgr
F1231 = Could not open file {1} in directory {0}: {2}
msgs_nl_NL.properties
# com.stc.jms.server.SegmentMgr
# F1231 = Could not open file {1} in directory {0}: {2}
# ;;TODO:NEW MESSAGE;;F1231 =
so that a human translator can easily add the translations to the foreign properties files. As you can see, it includes the location (Java class) where the message was encountered.
msgs_nl_NL.propertiesOfcourse the tool would have a way of handling if you would change the error message in the source code. The translated properties file would look like this:
# com.stc.jms.server.SegmentMgr
# F1231 = Could not open file {1} in directory {0}: {2}
F1231 = Bestand {1} in lijst {0} kon niet geopend worden: {2}
msgs_nl_NL.propertiesAny ideas on how to build this tool?
# com.stc.jms.server.SegmentMgr
# F1231 = File {1} in directory {0} could not be opened: {2}
# F1231 = Could not open file {1} in directory {0}: {2}
# ;;TODO: MESSAGE CHANGED;;
F1231 = Bestand {1} in lijst {0} kon niet geopend worden: {2}
2 comments:
Sounds like you want the Netbeans i18n module, or at least should start with that, and build on it if you need to. The Open Language Tools project has much of the smarts for dealing with message files, checking for pre-translated messages, etc. but Netbeans itself already has all the Java source code parsing stuff taken care of - best not reinvent the wheel...
Oops, forgot to add a link to the Open Language Tools project - here you go : http://open-language-tools.dev.java.net
Post a Comment