[stringtemplate-interest] Comments and questions from i18n document
Kay Roepke
kroepke at classdump.org
Fri Sep 22 13:11:29 PDT 2006
Hi John!
On 22. Sep 2006, at 17:37 Uhr, John Snyders wrote:
> I have a question on the following example from Section 3.
>
> Locale locale = Locale.getDefault();
> String language = locale.getLanguage();
> String root = "/var/data/templates/";
> StringTemplateGroup templates =
> new StringTemplateGroup(language, root+language);”
>
> Can this be done when the templates come from the class path? If so
> how?
Yes, it can. ANTLRv3 does something like this for l10n of its
diagnostic messages. Below is the code responsible for finding
the .stg file:
public static void setLocale(Locale locale) {
ErrorManager.locale = locale;
String language = locale.getLanguage();
String fileName = "org/antlr/tool/templates/messages/"+language
+".stg";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream(fileName);
if ( is==null ) {
cl = ErrorManager.class.getClassLoader();
is = cl.getResourceAsStream(fileName);
}
if ( is==null && language.equals(Locale.US.getLanguage()) ) {
rawError("ANTLR installation corrupted; cannot find English
messages file "+fileName);
panic();
}
else if ( is==null ) {
rawError("no such locale file "+fileName+" retrying with English
locale");
setLocale(Locale.US); // recurse on this rule, trying the US locale
return;
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(is));
messages = new StringTemplateGroup(br,
AngleBracketTemplateLexer.class,
initSTListener);
br.close();
}
catch (IOException ioe) {
rawError("cannot close message file "+fileName, ioe);
}
messages.setErrorListener(blankSTListener);
boolean messagesOK = verifyMessages();
if ( !messagesOK && language.equals(Locale.US.getLanguage()) ) {
rawError("ANTLR installation corrupted; English messages file
"+language+".stg incomplete");
panic();
}
else if ( !messagesOK ) {
setLocale(Locale.US); // try US to see if that will work
}
}
> A comment:
> Section 3 (Site design per locale) does not seem well motivated to
> me. The example given is text direction left-to-right vs. right-to-
> left. I have done i18n desktop and web apps before but I have not
> yet had to deal with text direction yet. Perhaps there are some
> issues with text direction that I am not aware of. I wish this
> section were expanded to give a more concrete example including
> text direction. What issues are there that group inheritance solves
> that can’t be solved with parameterized setting of the HTML dir
> attribute or css direction property.
>
> It seems to me that group inheritance is more about skinning and
> less important for localization. Even for skinning I think that
> methods involving only css should be used first because (I think)
> designers will have an easier time working with css.
>
> Still, I like the group inheritance language feature.
I think this an issue of code hygiene ;) I an ideal world all you'd
have to do is to supply the proper CSS attributes for text direction.
Most probably, though, you'd have to tweak some other layout bits to
accomodate for different lengths of strings, maybe you'd even like to
do some heavier layout changes for right-to-left locales. This would
lead to heavy usage of <if()> constructs in your templates. By using
group inheritance you can override certain templates completely to
specify radical different layouts without having to change any of the
common layout, thus increasing template sharing and maintability.
Using regions in addition to that can make for immensely powerful
adaptibility. Again, ANTLR v3 uses this approach to incorporate tree
building and debugging code into the generated parsers.
What you gain is not having to respecify any common template content
and not littering your templates with unnecessary 'if's.
Text direction support can get *really* messy if you want to present
high-quality pages that feel good for both directions. It's more
often than not not a simple matter if the direction. Sadly ;)
HTH,
-k
--
Kay Röpke <kroepke at classdump.org>
classdump Software
Key fingerprint = A849 0F2C C322 4022 379E 8661 7E1B FE0D 4CD2 A6D0
More information about the stringtemplate-interest
mailing list