Internationalization
also known as I18N is used for displaying content specific to the locale based
on their languages, currencies and formatting conventions.
Resource bundle is
the file that contains the key/value pairs for the default language of your
application. The naming format for this file is
bundlename_language_country_variant.properties
For example, if you
have a bundle named ApplicationResource for the English language in the Unites
States for the Windows platform, then your properties file name would be
ApplicationResource_en_US_WIN.properties.
Lets take a senario
in which the user can see the same page in four different languages like
English, French, German and Italian. The jsp page gets displayed according to
the language selected by the user.
Our default resource bundle is ApplicationResource.properties file. To link the resouce bundle with the application the following tag
should be added in the struts-config.xml file.
1.<message-resources parameter="com/vaannila/ApplicationResource"/>
The next step is to
create ApplicationResource.properties file specific to each language.
French -
ApplicationResource_fr.properties
1.label.welcome = J'aime Struts
Italian -
ApplicationResource_it.properties
1.label.welcome = ti amo Struts
German -
ApplicationResource_de.properties
1.label.welcome = Ich liebe Struts
There are two ways in which you can
internationalize struts application. One is by setting theorg.apache.struts.action.LOCALE to the corresponding language and the other way is to set the language
preference in the browser.
In this example we
will see how to internationalize Struts application by setting different values
to org.apache.struts.action.LOCALE variable.
Based on the
language selected by the user the corresponding method in the LocaleAction is
called. LocaleAction class extends DispatchAction.
01.public class LocaleAction extends DispatchAction {
02.
03.private final static String SUCCESS = "success";
04.
05.public ActionForward english(ActionMapping mapping, ActionForm
form,
06.HttpServletRequest
request, HttpServletResponse response)
07.throws Exception {
08.HttpSession session =
request.getSession();
09.session.setAttribute("org.apache.struts.action.LOCALE", Locale.ENGLISH);
10.return mapping.findForward(SUCCESS);
11.}
12.
13.public ActionForward french(ActionMapping mapping, ActionForm form,
14.HttpServletRequest
request, HttpServletResponse response)
15.throws Exception {
16.HttpSession session =
request.getSession();
17.session.setAttribute("org.apache.struts.action.LOCALE", Locale.FRENCH);
18.return mapping.findForward(SUCCESS);
19.}
20.
21.public ActionForward german(ActionMapping mapping, ActionForm form,
22.HttpServletRequest
request, HttpServletResponse response)
23.throws Exception {
24.HttpSession session =
request.getSession();
25.session.setAttribute("org.apache.struts.action.LOCALE", Locale.GERMAN);
26.return mapping.findForward(SUCCESS);
27.}
28.
29.public ActionForward italian(ActionMapping mapping, ActionForm
form,
30.HttpServletRequest
request, HttpServletResponse response)
31.throws Exception {
32.HttpSession session =
request.getSession();
33.session.setAttribute("org.apache.struts.action.LOCALE", Locale.ITALIAN);
34.return mapping.findForward(SUCCESS);
35.}
36.}
Based on the value
set in the org.apache.struts.action.LOCALE variable the corresponding
ApplicationResource.properties file will be used for displaying data.
No comments:
Post a Comment