1:
17: package ;
18:
19: import ;
20:
21: import ;
22: import ;
23: import ;
24:
25:
31: public final class ResourceHelper
32: {
33:
34: private static final String PROP_LOCALE = "net.dpml.cli.resource.bundle";
35:
36:
37: private static final String DEFAULT_BUNDLE =
38: "net.dpml.cli.resource.CLIMessageBundle_en_US";
39:
40: private static ResourceHelper m_HELPER;
41:
42:
43: private ResourceBundle m_bundle;
44:
45: private String m_prop;
46:
47:
50: private ResourceHelper()
51: {
52: String bundleName = System.getProperty( PROP_LOCALE );
53:
54: if( bundleName == null )
55: {
56: bundleName = DEFAULT_BUNDLE;
57: }
58:
59: m_prop = bundleName;
60:
61: int firstUnderscore = bundleName.indexOf( '_' );
62: int secondUnderscore = bundleName.indexOf( '_', firstUnderscore + 1 );
63:
64: Locale locale;
65: if( firstUnderscore != -1 )
66: {
67: String language = bundleName.substring( firstUnderscore + 1, secondUnderscore );
68: String country = bundleName.substring( secondUnderscore + 1 );
69: locale = new Locale( language, country );
70: }
71: else
72: {
73: locale = Locale.getDefault();
74: }
75:
76: try
77: {
78: m_bundle = ResourceBundle.getBundle( bundleName, locale );
79: }
80: catch( MissingResourceException exp )
81: {
82: m_bundle = ResourceBundle.getBundle( DEFAULT_BUNDLE, locale );
83: }
84: }
85:
86:
90: public String getBundleName()
91: {
92: return m_prop;
93: }
94:
95:
99: public static ResourceHelper getResourceHelper()
100: {
101: String bundleName = System.getProperty( PROP_LOCALE );
102: if( m_HELPER == null || !m_HELPER.getBundleName().equals( bundleName ) )
103: {
104: m_HELPER = new ResourceHelper();
105: }
106: return m_HELPER;
107: }
108:
109:
115: public String getMessage( final String key )
116: {
117: return getMessage( key, new Object[0] );
118: }
119:
120:
127: public String getMessage( final String key, final Object value )
128: {
129: return getMessage( key, new Object[]{value} );
130: }
131:
132:
140: public String getMessage(
141: final String key, final Object value1, final Object value2 )
142: {
143: return getMessage( key, new Object[]{value1, value2} );
144: }
145:
146:
156: public String getMessage(
157: final String key, final Object value1, final Object value2, final Object value3 )
158: {
159: return getMessage( key, new Object[]{value1, value2, value3} );
160: }
161:
162:
169: public String getMessage( final String key, final Object[] values )
170: {
171: final String msgFormatStr = m_bundle.getString( key );
172: final MessageFormat msgFormat = new MessageFormat( msgFormatStr );
173: return msgFormat.format( values );
174: }
175: }