1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.integration.commons.lang;
17
18 import net.sf.morph.Defaults;
19 import net.sf.morph.lang.DecoratedLanguage;
20
21 import org.apache.commons.lang.text.StrLookup;
22
23 /**
24 * Implement StrLookup using a Morph Language and a source object.
25 *
26 * @author Matt Benson
27 * @since Morph 1.1
28 */
29 public class LanguageStrLookup extends StrLookup {
30 private Object source;
31 private DecoratedLanguage language;
32
33 /**
34 * Create a new LanguageStrLookup.
35 * @param source Object
36 */
37 public LanguageStrLookup(Object source) {
38 this.source = source;
39 }
40
41 /**
42 * Create a new LanguageStrLookup.
43 * @param source Object
44 * @param language DecoratedLanguage
45 */
46 public LanguageStrLookup(Object source, DecoratedLanguage language) {
47 this(source);
48 this.language = language;
49 }
50
51 /**
52 * Resolve the specified "property".
53 * @param key String
54 */
55 public String lookup(String key) {
56 return (String) getLanguage().get(source, key, String.class);
57 }
58
59 /**
60 * Get the language.
61 * @return the language
62 */
63 public synchronized DecoratedLanguage getLanguage() {
64 if (language == null) {
65 setLanguage(Defaults.createLanguage());
66 }
67 return language;
68 }
69
70 /**
71 * Set the language.
72 * @param language the language to set
73 */
74 public synchronized void setLanguage(DecoratedLanguage language) {
75 this.language = language;
76 }
77
78 }