1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- package org .ocpsoft .rewrite .transposition ;
16+ package org .ocpsoft .rewrite .i18n ;
1717
1818import java .util .Locale ;
1919import java .util .Map ;
3333import org .ocpsoft .rewrite .param .Transposition ;
3434
3535/**
36- * A {@link Transposition} responsible for translating values from their matching translation from a
37- * {@link ResourceBundle}. The lookup in the properties file is case-sensitive. The properties file should use
36+ * A {@link Transposition} and/or {@link Constraint} responsible for translating values from their matching translation
37+ * from a {@link ResourceBundle}. The lookup in the properties file is case-sensitive. The properties file should use
3838 * ISO-8859-1 encoding as defined by {@link PropertyResourceBundle}.
3939 *
4040 * Requires inverted properties files in the form of translated_name=name_to_bind.
4444 *
4545 * @author Christian Gendreau
4646 */
47- public class LocaleTransposition implements Transposition <String >, Constraint <String >
47+ public class I18nSupport implements Transposition <String >, Constraint <String >
4848{
4949 // shared thread safe map between a String(representing the language) and a ResourceBundle.
5050 private static Map <String , ResourceBundle > bundleMap = new ConcurrentHashMap <String , ResourceBundle >();
@@ -54,7 +54,7 @@ public class LocaleTransposition implements Transposition<String>, Constraint<St
5454
5555 private Operation onFailureOperation ;
5656
57- private LocaleTransposition (final String languageParam , final String bundleName )
57+ private I18nSupport (final String languageParam , final String bundleName )
5858 {
5959 Assert .notNull (languageParam , "Language must not be null." );
6060 Assert .notNull (bundleName , "Bundle must not be null." );
@@ -66,6 +66,48 @@ private LocaleTransposition(final String languageParam, final String bundleName)
6666
6767 }
6868
69+ /**
70+ * Translate a value into the matching one from a resource bundle in specified language.
71+ *
72+ * @param lang
73+ * @param value
74+ * @return translated value or null if no bundle can be found for the specified language or no entries in the bundle
75+ * match the given value.
76+ */
77+ private String translate (String lang , String value )
78+ {
79+ String translatation = null ;
80+ if (value != null )
81+ {
82+ if (!bundleMap .containsKey (lang ))
83+ {
84+ Locale locale = new Locale (lang );
85+ try
86+ {
87+ ResourceBundle loadedBundle = ResourceBundle .getBundle (bundleName , locale ,
88+ ResourceBundle .Control .getNoFallbackControl (ResourceBundle .Control .FORMAT_DEFAULT ));
89+
90+ bundleMap .put (lang , loadedBundle );
91+ }
92+ catch (MissingResourceException e )
93+ {
94+ return null ;
95+ }
96+ }
97+
98+ try
99+ {
100+ // can we received more than one path section? e.g./search/service/
101+ translatation = bundleMap .get (lang ).getString (value );
102+ }
103+ catch (MissingResourceException mrEx )
104+ {
105+ // ignore
106+ }
107+ }
108+ return translatation ;
109+ }
110+
69111 /**
70112 * Create a {@link Binding} to a {@link ResourceBundle}, where the initial bound {@link Parameter} value is used as
71113 * the bundle lookup key. The resultant value of the bundle lookup is stored as the new bound {@link Parameter}
@@ -87,7 +129,7 @@ private LocaleTransposition(final String languageParam, final String bundleName)
87129 * <p>
88130 * When this example is applied to a URL of: "/de/bibliotek", assuming a bundle called "org.example.Paths_de" exists
89131 * and contains the an entry "bibliotek=library", the rule will forward to the new URL: "/library", because the value
90- * of "path" has been transposed by {@link LocaleTransposition }.
132+ * of "path" has been transposed by {@link I18nSupport }.
91133 *
92134 *
93135 * @param bundleName Fully qualified name of the {@link ResourceBundle}
@@ -96,9 +138,9 @@ private LocaleTransposition(final String languageParam, final String bundleName)
96138 *
97139 * @return new instance of LocaleBinding
98140 */
99- public static LocaleTransposition bundle (final String bundleName , final String localeParam )
141+ public static I18nSupport bundle (final String bundleName , final String localeParam )
100142 {
101- return new LocaleTransposition (localeParam , bundleName );
143+ return new I18nSupport (localeParam , bundleName );
102144 }
103145
104146 /**
@@ -109,7 +151,7 @@ public static LocaleTransposition bundle(final String bundleName, final String l
109151 * @param onFailureOperation
110152 * @return
111153 */
112- public LocaleTransposition onTranspositionFailed (Operation onFailureOperation )
154+ public I18nSupport onTranspositionFailed (Operation onFailureOperation )
113155 {
114156 this .onFailureOperation = onFailureOperation ;
115157 return this ;
@@ -118,98 +160,35 @@ public LocaleTransposition onTranspositionFailed(Operation onFailureOperation)
118160 @ Override
119161 public String transpose (Rewrite event , EvaluationContext context , String value )
120162 {
121- String transposedValue = null ;
122- boolean transpositionFailed = false ;
123-
124163 // Retrieve the value of lang from the context
125164 String targetLang = (String ) Parameters .retrieve (context , this .languageParam );
126- if (value != null )
127- {
128- if (!bundleMap .containsKey (targetLang ))
129- {
130- Locale locale = new Locale (targetLang );
131- try
132- {
133- ResourceBundle loadedBundle = ResourceBundle .getBundle (bundleName , locale ,
134- ResourceBundle .Control .getNoFallbackControl (ResourceBundle .Control .FORMAT_DEFAULT ));
135-
136- bundleMap .put (targetLang , loadedBundle );
137- }
138- catch (MissingResourceException e )
139- {
140- transpositionFailed = true ;
141- }
142- }
143-
144- if (!transpositionFailed )
145- {
146- try
147- {
148- // can we received more than one path section? e.g./search/service/
149- transposedValue = bundleMap .get (targetLang ).getString (value );
150- }
151- catch (MissingResourceException mrEx )
152- {
153- transpositionFailed = true ;
154- }
155- }
165+ String transposedValue = translate (targetLang , value );
156166
157- if (transpositionFailed )
167+ if (transposedValue == null )
168+ {
169+ if (onFailureOperation != null )
158170 {
159- if (onFailureOperation != null )
160- {
161- context .addPreOperation (onFailureOperation );
162- }
163- // if language is not defined, do not translate and keep original value.
164- transposedValue = value ;
171+ context .addPreOperation (onFailureOperation );
165172 }
173+ // if language is not defined, do not translate and keep original value.
174+ transposedValue = value ;
166175 }
167176 return transposedValue ;
168177 }
169178
170179 @ Override
171180 public boolean isSatisfiedBy (Rewrite event , EvaluationContext context , String value )
172181 {
173- boolean transpositionFailed = false ;
174-
175182 // Retrieve the value of lang from the context
176183 String targetLang = (String ) Parameters .retrieve (context , this .languageParam );
177- if (value != null )
178- {
179- if (!bundleMap .containsKey (targetLang ))
180- {
181- Locale locale = new Locale (targetLang );
182- try
183- {
184- ResourceBundle loadedBundle = ResourceBundle .getBundle (bundleName , locale ,
185- ResourceBundle .Control .getNoFallbackControl (ResourceBundle .Control .FORMAT_DEFAULT ));
184+ String translation = translate (targetLang , value );
186185
187- bundleMap .put (targetLang , loadedBundle );
188- }
189- catch (MissingResourceException e )
190- {
191- transpositionFailed = true ;
192- }
193- }
194-
195- if (!transpositionFailed )
196- {
197- try
198- {
199- // can we received more than one path section? e.g./search/service/
200- bundleMap .get (targetLang ).getString (value );
201- }
202- catch (MissingResourceException mrEx )
203- {
204- transpositionFailed = true ;
205- }
206- }
186+ // Should it be triggered only in transpose(...) ?
187+ // if (translation == null && onFailureOperation != null)
188+ // {
189+ // context.addPreOperation(onFailureOperation);
190+ // }
207191
208- if (transpositionFailed )
209- {
210- return false ;
211- }
212- }
213- return true ;
192+ return (translation != null );
214193 }
215194}
0 commit comments