Skip to content

Commit a1db805

Browse files
committed
renamed LocaleTranspositon to I18nSupport
1 parent cfec0df commit a1db805

3 files changed

Lines changed: 100 additions & 103 deletions

File tree

api-tests/src/test/java/org/ocpsoft/rewrite/transposition/LocaleTranspositionTest.java renamed to api-tests/src/test/java/org/ocpsoft/rewrite/i18n/I18nSupportTest.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
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

1818
import java.io.File;
1919

@@ -37,7 +37,7 @@
3737
*
3838
*/
3939
@RunWith(Arquillian.class)
40-
public class LocaleTranspositionTest extends RewriteTest
40+
public class I18nSupportTest extends RewriteTest
4141
{
4242

4343
@Deployment(testable = false)
@@ -46,7 +46,7 @@ public static WebArchive getDeployment()
4646
WebArchive deployment = RewriteTest
4747
.getDeployment()
4848
.addPackages(true, Root.class.getPackage())
49-
.addAsServiceProvider(ConfigurationProvider.class, LocaleTranspositionConfigurationProvider.class)
49+
.addAsServiceProvider(ConfigurationProvider.class, I18nSuuportConfigurationProvider.class)
5050
.addAsWebResource(new StringAsset("search page"), "search")
5151
.addAsWebResource(new StringAsset("library page"), "library")
5252
.addAsResource(new File("src/test/resources/bundle_fr.properties"))
@@ -60,7 +60,7 @@ public static WebArchive getDeployment()
6060
* @throws Exception
6161
*/
6262
@Test
63-
public void testLocaleTransposition() throws Exception
63+
public void testI18nSupport() throws Exception
6464
{
6565
// 'rechercher' should be translated to 'search' and 'lang' should be joined
6666
HttpAction<HttpGet> action = get("/fr/rechercher");
@@ -69,7 +69,7 @@ public void testLocaleTransposition() throws Exception
6969
}
7070

7171
@Test
72-
public void testLocaleTranspositionWithAccent() throws Exception
72+
public void testI18nSupportWithAccent() throws Exception
7373
{
7474
// 'bibliothèque' should be translated to 'library' and 'lang' should be joined
7575
HttpAction<HttpGet> action = get("/fr/bibliothèque");
@@ -78,14 +78,27 @@ public void testLocaleTranspositionWithAccent() throws Exception
7878
}
7979

8080
/**
81-
* Expected behavior(default) is to keep original value so the address should become /library
81+
* Expected behavior(default) when used as a Transposition and Constraint is to abort the rule so 404 is expected.
8282
*
8383
* @throws Exception
8484
*/
8585
@Test
86-
public void testLocaleTranspositionFailureNoHandling() throws Exception
86+
public void testI18nSupportFailureNoHandling() throws Exception
8787
{
8888
HttpAction<HttpGet> action = get("/zz/library");
89+
Assert.assertEquals(404, action.getResponse().getStatusLine().getStatusCode());
90+
}
91+
92+
/**
93+
* Expected behavior(default) when used as a Transposition only is to keep the original value so the address should
94+
* become /library
95+
*
96+
* @Test
97+
* @throws Exception
98+
*/
99+
public void testI18nSupportTranspositionFailureNoHandling() throws Exception
100+
{
101+
HttpAction<HttpGet> action = get("/zz/library/transposition_only");
89102
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
90103
Assert.assertEquals("library page", action.getResponseContent());
91104
}
@@ -99,7 +112,7 @@ public void testLocaleTranspositionFailureNoHandling() throws Exception
99112
@Test
100113
public void testLocaleTranspositionFailureWithHandling() throws Exception
101114
{
102-
HttpAction<HttpGet> action = get("/zz/library/fail1");
115+
HttpAction<HttpGet> action = get("/zz/library/transposition_failed_1");
103116
Assert.assertEquals(201, action.getResponse().getStatusLine().getStatusCode());
104117
}
105118

@@ -112,7 +125,7 @@ public void testLocaleTranspositionFailureWithHandling() throws Exception
112125
@Test
113126
public void testLocaleTranspositionMissingKeyWithHandling() throws Exception
114127
{
115-
HttpAction<HttpGet> action = get("/en/missinglibrary/fail2");
128+
HttpAction<HttpGet> action = get("/en/missinglibrary/transposition_failed_2");
116129
Assert.assertEquals(202, action.getResponse().getStatusLine().getStatusCode());
117130
}
118131
}

api-tests/src/test/java/org/ocpsoft/rewrite/transposition/LocaleTranspositionConfigurationProvider.java renamed to api-tests/src/test/java/org/ocpsoft/rewrite/i18n/I18nSuuportConfigurationProvider.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
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

1818
import javax.servlet.ServletContext;
1919

@@ -31,7 +31,7 @@
3131
* @author Christian Gendreau
3232
*
3333
*/
34-
public class LocaleTranspositionConfigurationProvider extends HttpConfigurationProvider
34+
public class I18nSuuportConfigurationProvider extends HttpConfigurationProvider
3535
{
3636
@Override
3737
public int priority()
@@ -44,10 +44,14 @@ public Configuration getConfiguration(final ServletContext context)
4444
{
4545
Configuration config = ConfigurationBuilder.begin()
4646
.addRule(Join.path("/{lang}/{path}").to("/{path}"))
47-
.where("path").transposedBy(LocaleTransposition.bundle("bundle", "lang"))
48-
.constrainedBy(LocaleTransposition.bundle("bundle", "lang"))
49-
.addRule(Join.path("/{lang}/{path}/fail1").to("/{path}"))
50-
.where("path").transposedBy(LocaleTransposition.bundle("bundle", "lang").onTranspositionFailed(
47+
.where("path").transposedBy(I18nSupport.bundle("bundle", "lang"))
48+
.constrainedBy(I18nSupport.bundle("bundle", "lang"))
49+
50+
.addRule(Join.path("/{lang}/{path}/transposition_only").to("/{path}"))
51+
.where("path").transposedBy(I18nSupport.bundle("bundle", "lang"))
52+
53+
.addRule(Join.path("/{lang}/{path}/transposition_failed_1").to("/{path}"))
54+
.where("path").transposedBy(I18nSupport.bundle("bundle", "lang").onTranspositionFailed(
5155
new HttpOperation()
5256
{
5357
@Override
@@ -56,8 +60,9 @@ public void performHttp(HttpServletRewrite event, EvaluationContext context)
5660
SendStatus.code(201).performHttp(event, context);
5761
}
5862
}))
59-
.addRule(Join.path("/{lang}/{path}/fail2").to("/{path}"))
60-
.where("path").transposedBy(LocaleTransposition.bundle("bundle", "lang").onTranspositionFailed(
63+
64+
.addRule(Join.path("/{lang}/{path}/transposition_failed_2").to("/{path}"))
65+
.where("path").transposedBy(I18nSupport.bundle("bundle", "lang").onTranspositionFailed(
6166
new HttpOperation()
6267
{
6368
@Override

api/src/main/java/org/ocpsoft/rewrite/transposition/LocaleTransposition.java renamed to api/src/main/java/org/ocpsoft/rewrite/i18n/I18nSupport.java

Lines changed: 65 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
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

1818
import java.util.Locale;
1919
import java.util.Map;
@@ -33,8 +33,8 @@
3333
import 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.
@@ -44,7 +44,7 @@
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

Comments
 (0)