Skip to content

Commit f94ce2b

Browse files
committed
Merged LocaleTransposition API to allow i18n-based Parameter configuration in rules
1 parent f70acd0 commit f94ce2b

5 files changed

Lines changed: 267 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2014 Université de Montréal
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transposition;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
23+
import org.ocpsoft.rewrite.servlet.config.rule.Join;
24+
import org.ocpsoft.rewrite.transposition.LocaleTransposition;
25+
26+
/**
27+
*
28+
* @author Christian Gendreau
29+
*
30+
*/
31+
public class LocaleTranspositionConfigurationProvider extends HttpConfigurationProvider
32+
{
33+
@Override
34+
public int priority()
35+
{
36+
return 0;
37+
}
38+
39+
@Override
40+
public Configuration getConfiguration(final ServletContext context)
41+
{
42+
Configuration config = ConfigurationBuilder.begin()
43+
.addRule(Join.path("/{lang}/{path}").to("/{path}"))
44+
.where("path").transposedBy(LocaleTransposition.bundle("bundle", "lang"));
45+
return config;
46+
}
47+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2014 Université de Montréal
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transposition;
17+
18+
import java.io.File;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.asset.StringAsset;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.ocpsoft.rewrite.Root;
29+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
30+
import org.ocpsoft.rewrite.test.HttpAction;
31+
import org.ocpsoft.rewrite.test.RewriteTest;
32+
33+
/**
34+
* This test class requires /src/test/resources/bundle_en.properties and bundle_fr.properties files.
35+
*
36+
* @author Christian Gendreau
37+
*
38+
*/
39+
@RunWith(Arquillian.class)
40+
public class LocaleTranspositionTest extends RewriteTest
41+
{
42+
43+
@Deployment(testable = false)
44+
public static WebArchive getDeployment()
45+
{
46+
WebArchive deployment = RewriteTest
47+
.getDeployment()
48+
.addPackages(true, Root.class.getPackage())
49+
.addAsServiceProvider(ConfigurationProvider.class, LocaleTranspositionConfigurationProvider.class)
50+
.addAsWebResource(new StringAsset("search page"), "search")
51+
.addAsWebResource(new StringAsset("library page"), "library")
52+
.addAsResource(new File("src/test/resources/bundle_fr.properties"))
53+
.addAsResource(new File("src/test/resources/bundle_en.properties"));
54+
return deployment;
55+
}
56+
57+
/**
58+
* Currently failing
59+
*
60+
* @throws Exception
61+
*/
62+
@Test
63+
public void testLocaleTransposition() throws Exception
64+
{
65+
// 'rechercher' should be translated to 'search' and 'lang' should be joined
66+
HttpAction<HttpGet> action = get("/fr/rechercher");
67+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
68+
Assert.assertEquals("search page", action.getResponseContent());
69+
}
70+
71+
@Test
72+
public void testLocaleTranspositionWithAccent() throws Exception
73+
{
74+
// 'bibliothèque' should be translated to 'library' and 'lang' should be joined
75+
HttpAction<HttpGet> action = get("/fr/bibliothèque");
76+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
77+
Assert.assertEquals("library page", action.getResponseContent());
78+
}
79+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
search=search
2+
library=library
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rechercher=search
2+
bibliothèque=library
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2014 Université de Montréal
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transposition;
17+
18+
import java.util.Locale;
19+
import java.util.Map;
20+
import java.util.MissingResourceException;
21+
import java.util.ResourceBundle;
22+
import java.util.concurrent.ConcurrentHashMap;
23+
24+
import org.ocpsoft.rewrite.bind.Binding;
25+
import org.ocpsoft.rewrite.context.EvaluationContext;
26+
import org.ocpsoft.rewrite.event.Rewrite;
27+
import org.ocpsoft.rewrite.exception.RewriteException;
28+
import org.ocpsoft.rewrite.param.Parameter;
29+
import org.ocpsoft.rewrite.param.Parameters;
30+
import org.ocpsoft.rewrite.param.Transposition;
31+
32+
/**
33+
* A {@link Transposition} responsible for translating values from their matching translation from a
34+
* {@link ResourceBundle}. The lookup in the properties file is case-sensitive.
35+
*
36+
* Requires inverted properties files in the form of translated_name=name_to_bind.
37+
*
38+
* TODO Should we allow to load the properties file in a different encoding? TODO Allow to enable case insensitive
39+
* and/or ascii-folding on property lookup
40+
*
41+
* @author Christian Gendreau
42+
*/
43+
public class LocaleTransposition implements Transposition<String>
44+
{
45+
46+
// shared thread safe map between a String(representing the language) and a ResourceBundle.
47+
private static Map<String, ResourceBundle> bundleMap = new ConcurrentHashMap<String, ResourceBundle>();
48+
49+
private final String languageParam;
50+
private final String bundleName;
51+
52+
private LocaleTransposition(final String languageParam, final String bundleName)
53+
{
54+
this.languageParam = languageParam;
55+
this.bundleName = bundleName;
56+
}
57+
58+
/**
59+
* Create a {@link Binding} to a {@link ResourceBundle}, where the initial bound {@link Parameter} value is used as
60+
* the bundle lookup key. The resultant value of the bundle lookup is stored as the new bound {@link Parameter}
61+
* value.
62+
* <p>
63+
* For example, consider the following URL-based rule:
64+
*
65+
* <pre>
66+
* Configuration config = ConfigurationBuilder.begin()
67+
* .addRule(Join.path(&quot;/{lang}/{path}&quot;).to(&quot;/{path}&quot;))
68+
* .where(&quot;path&quot;).transposedBy(LocaleTransposition.bundle(&quot;org.example.Paths&quot;, &quot;lang&quot;));
69+
* </pre>
70+
* <p>
71+
* In the above scenario, "bundleName" is the resource bundle name. The value of the {@link Parameter} "lang" is used
72+
* as the bundle {@link Locale}. The initial value of the {@link Parameter} "path" is used as the lookup key, and is
73+
* transposed to the value of the corresponding resource bundle entry. Once transposition has occurred, after rule
74+
* evaluation, subsequent references to the "path" {@link Parameter} will return the value from the
75+
* {@link ResourceBundle} entry.
76+
* <p>
77+
* When this example is applied to a URL of: "/de/bibliotek", assuming a bundle called "org.example.Paths_de" exists
78+
* and contains the an entry "bibliotek=library", the rule will forward to the new URL: "/library", because the value
79+
* of "path" has been transposed by {@link LocaleTransposition}.
80+
*
81+
*
82+
* @param bundleName Fully qualified name of the {@link ResourceBundle}
83+
* @param localeParam The name of the {@link Parameter} that contains the {@link Locale} code.
84+
*
85+
* @return new instance of LocaleBinding
86+
*/
87+
public static LocaleTransposition bundle(final String bundleName, final String localeParam)
88+
{
89+
return new LocaleTransposition(localeParam, bundleName);
90+
}
91+
92+
@Override
93+
public String transpose(Rewrite event, EvaluationContext context, String value)
94+
{
95+
String transposedValue = null;
96+
97+
// FIXME this is currently failing due to the absence of the languageParam parameter in the context
98+
// Retrieve the value of lang from the context
99+
String targetLang = (String) Parameters.retrieve(context, this.languageParam);
100+
if (value != null)
101+
{
102+
if (!bundleMap.containsKey(targetLang))
103+
{
104+
Locale locale = new Locale(targetLang);
105+
try
106+
{
107+
ResourceBundle loadedBundle = ResourceBundle.getBundle(bundleName, locale);
108+
bundleMap.put(targetLang, loadedBundle);
109+
}
110+
catch (MissingResourceException e)
111+
{
112+
throw new RewriteException("Error occurred during Locale transposition of parameter value [" + value
113+
+ "] via bundle [" + bundleName + "_" + targetLang + "]", e);
114+
}
115+
}
116+
117+
if (bundleMap.containsKey(targetLang)) {
118+
try
119+
{
120+
// can we received more than one path section? e.g./search/service/
121+
transposedValue = bundleMap.get(targetLang).getString(value);
122+
}
123+
catch (MissingResourceException mrEx)
124+
{
125+
// if not found, do not translate and keep original value
126+
transposedValue = value;
127+
}
128+
}
129+
else
130+
{
131+
// if language is not defined, do not translate and keep original value
132+
transposedValue = value;
133+
}
134+
}
135+
return transposedValue;
136+
}
137+
}

0 commit comments

Comments
 (0)