01: package freemarker.template;
02:
03: import java.util.Locale;
04:
05: import java.util.ResourceBundle;
06:
07: import java.util.MissingResourceException;
08:
09: /**
10:
11: * A concrete implementation of {@link LocalizedString} that gets
12:
13: * a localized string from a {@link java.util.ResourceBundle}
14:
15: * @author Jonathan Revusky
16:
17: */
18:
19: public class ResourceBundleLocalizedString extends LocalizedString {
20:
21: private String resourceKey, resourceBundleLookupKey;
22:
23: /**
24:
25: * @param resourceBundleLookupKey The lookup key for the resource bundle
26:
27: * @param resourceKey the specific resource (assumed to be a string) to fish out of the resource bundle
28:
29: */
30:
31: public ResourceBundleLocalizedString(
32: String resourceBundleLookupKey, String resourceKey) {
33:
34: this .resourceBundleLookupKey = resourceBundleLookupKey;
35:
36: this .resourceKey = resourceKey;
37:
38: }
39:
40: public String getLocalizedString(Locale locale)
41: throws TemplateModelException {
42:
43: try {
44:
45: ResourceBundle rb = ResourceBundle.getBundle(
46: resourceBundleLookupKey, locale);
47:
48: return rb.getString(resourceKey);
49:
50: }
51:
52: catch (MissingResourceException mre) {
53:
54: throw new TemplateModelException("missing resource", mre);
55:
56: }
57:
58: }
59:
60: }
|