Skip to content

Commit a050104

Browse files
committed
Added i18n documentation
1 parent 339c3ce commit a050104

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
== Internationalize your URLs
2+
3+
One of the frequently use patterns when building multi-national or multi-lingual websites is the ability to utilize Internationalization and Localization information gathered from the URL; to serve dynamic content based on the selected Locale, and even to customize URLs for each language or locality.
4+
5+
For instance, let's take the example of a website that provides a library of documents that may be accessed via the url:
6+
7+
[source,text]
8+
----
9+
http://example.com/library.jsp
10+
----
11+
12+
When the above URL is accessed, we should be presented with the library page; however, let's consider how this web application can detect and serve content in the user's native language. How would we go about achieving this?
13+
14+
We could assume that localization codes may be passed as part of the URL, for instance:
15+
16+
[source,text]
17+
----
18+
http://example.com/fr/biblioteque
19+
http://example.com/de/bibliothek
20+
http://example.com/pt/biblioteca
21+
----
22+
23+
=== "Simple" i18n and l10n using Rewrite
24+
25+
To accomplish this, one might naively create a rule for each combination of language and path, and while this may outwardly seem like a simple approach, it might result in a configuration that looks something like the configuration below - this configuration obviously suffers from too much duplication.
26+
27+
[source,java]
28+
----
29+
public class ExampleLocalizedConfig extends HttpConfigurationProvider
30+
{
31+
@Override
32+
public Configuration getConfiguration(final ServletContext context)
33+
{
34+
return ConfigurationBuilder.begin()
35+
.addRule()
36+
.when(Path.matches("/{lang}/library").withRequestBinding())
37+
.perform(Forward.to("/library.jsp"))
38+
39+
.addRule()
40+
.when(Path.matches("/{lang}/biblioteque").withRequestBinding())
41+
.perform(Forward.to("/library.jsp"));
42+
43+
// ... and so on
44+
}
45+
}
46+
----
47+
48+
NOTE: This configuration binds the parameter `"lang"` and its value to the request parameter map as if it were part of the request query string (via the `Path` element's `.withRequestBinding()` setting), and is passed to the application (automatically) as `/library.jsp?lang={lang}``:
49+
50+
As you can see, this results in a large number of highly similar rules that can, depending on the number of languages supported, and the number of pages mapped using this technique, begin to introduce performance issues and maintenance debt.
51+
52+
=== Creating a single rule to perform i18n and l10n
53+
54+
The behavior outlined in the previous section can be condensed into one single rule using the `LocaleTransposition` class. The following `ConfigurationProvider` demonstrates basic usage:
55+
56+
[source,java]
57+
----
58+
public class ExampleLocalizedConfig extends HttpConfigurationProvider
59+
{
60+
@Override
61+
public Configuration getConfiguration(final ServletContext context)
62+
{
63+
return ConfigurationBuilder.begin()
64+
.addRule()
65+
.when(Path.matches("/{lang}/{path}").withRequestBinding())
66+
.perform(Forward.to("/{path}.jsp"))
67+
.where("path").transposedBy(LocaleTransposition.bundle("org.example.Paths", "lang"));
68+
}
69+
}
70+
----
71+
72+
In the above configuration, `org.example.Paths` is the resource bundle name where translations are stored. The value of the parameter `"lang"` is extracted from the inbound request URL, and used as the bundle `Locale` code (such as `en`, or `PT_br`). The initial value of the parameter `"path"` is used as the entry lookup key, and is transposed to the value of the corresponding resource bundle entry. Once transposition has occurred, after rule evaluation, operations making reference to the `"path"` parameter will receive the value found in the appropriate `ResourceBundle` entry, instead of the original value that was extracted from the request URL.
73+
74+
When this example is applied to a URL of: `"/de/bibliotek"`, assuming a bundle called `org.example.Paths_de` exists on the classpath and contains the an entry `"bibliotek=library"`, the rule will forward the request to the server resource located at `"/library.jsp"`, because the value of `"path"` has been transposed by `LocaleTransposition` using the resource bundle as a lookup table (shown below).
75+
76+
77+
=== Further simplification using Join
78+
79+
In your application, you may opt to replace the `Path` and `Forward` elements with one combined rule, such as a `Join`. The `Path` and `Forward` elements allow fine control, but are used here only to showcase the parameter transposition lifecycle (occurs between `.when()` and `.perform()` steps.). A simplified configuration using `Join` would look something like this:
80+
81+
[source,java]
82+
----
83+
public class ExampleLocalizedConfig extends HttpConfigurationProvider
84+
{
85+
@Override
86+
public Configuration getConfiguration(final ServletContext context)
87+
{
88+
return ConfigurationBuilder.begin()
89+
.addRule(Join.path("/{lang}/{path}").to("/{path}.jsp"))
90+
.where("path").transposedBy(LocaleTransposition.bundle("org.example.Paths", "lang"));
91+
}
92+
}
93+
----
94+
95+
=== The Resource Bundles
96+
97+
In order for this example to work, we must also have corresponding Java resource bundles on the classpath. The following table displays what some corresponding resource bundles for this example would look like:
98+
99+
[cols="2*a"]
100+
|===
101+
|Bundle Location |Bundle Contents
102+
103+
104+
|`src/main/java/org/example/Paths_en.properties`
105+
|[source,text]
106+
----
107+
libary=library
108+
home=home
109+
search=search
110+
----
111+
112+
|`src/main/java/org/example/Paths_fr.properties`
113+
|[source,text]
114+
----
115+
bibliotheque=library
116+
maison=home
117+
recherche=search
118+
----
119+
120+
121+
|`src/main/java/org/example/Paths_de.properties`
122+
|[source,text]
123+
----
124+
bibliothek=library
125+
heim=home
126+
suche=search
127+
----
128+
129+
|===
130+
131+
As you can see, each file must contain a list of translations with which the parameter specified in the `.where("param")` clause should be transposed.
132+
133+
=== Summary
134+
135+
There are obviously many ways to achieve i18n and l10n support in your application, and this is just one example of how to use the `LocaleTransposition` API, but hopefully gives a good place to start for internationalizing your site and services. Rewrite's APIs are flexible and powerful, allowing a great deal of control, so w encourage you to take your own approach and give us feedback on how this may be improved and brought back to the Rewrite framework as a whole.
136+
137+
138+
139+
140+
141+
142+
143+
144+

‎documentation/src/main/asciidoc/index.asciidoc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ of the framework, just skip the steps you don't need.
1717
* link:configuration/annotations/[Simplify with Annotations]
1818
* Response Interception
1919
* Resource Transformation
20-
* Internationalize your URLs
20+
* link:configuration/i18n[Internationalize your URLs]
2121
* link:migration/prettyfaces3[PrettyFaces 3.x Migration Guide]
2222

2323
=== Integrate Rewrite with other Frameworks

0 commit comments

Comments
 (0)