01: package freemarker.template;
02:
03: import freemarker.core.Environment;
04:
05: import java.util.Locale;
06:
07: /**
08:
09: * An abstract base class for scalars that vary by locale.
10:
11: * Here is a silly usage example.
12:
13: * <code>
14:
15: * TemplateScalarModel localizedYes = new LocalizedString() {
16:
17: * public String getLocalizedString(java.util.Locale locale) {
18:
19: * String lang = locale.getLanguage();
20:
21: * if "fr".equals(lang)
22:
23: * return "oui";
24:
25: * else if "de".equals(lang)
26:
27: * return "sí";
28:
29: * else
30:
31: * return "yes";
32:
33: * }
34:
35: * };
36:
37: * </code>
38:
39: * @author Jonathan Revusky
40:
41: */
42:
43: abstract public class LocalizedString implements TemplateScalarModel {
44:
45: public String getAsString() throws TemplateModelException {
46:
47: Environment env = Environment.getCurrentEnvironment();
48:
49: Locale locale = env.getLocale();
50:
51: return getLocalizedString(locale);
52:
53: }
54:
55: abstract public String getLocalizedString(Locale locale)
56: throws TemplateModelException;
57:
58: }
|