Reputation: 77
I have a web application. For multilanguage purpose, I use Java Struts 1.3 with MessageResources
. The default language is italian
(MessageResources.properties
) and the second language is english
(MessageResources_en.properties
).
in struts-config.xml
:
<!--Message Resources -->
<message-resources parameter="MessageResources" />
in my index.jsp
:
<% String language = request.getLocale().getLanguage(); %>
var lang = '<%=language%>';
<script type="text/javascript" src="js/language.js"></script>
in js/language.js
:
$(document).ready(function() {
var browser = null;
var val = navigator.userAgent.toLowerCase();
if(val.indexOf("firefox") > -1) browser = 'FIREFOX';
else if(val.indexOf("msie") > -1) browser = 'IE';
else if(val.indexOf("chrome") > -1) browser = 'CHROME';
$.ajax({
url:"SetDefaultLanguage.do",
data: "isolingua="+lang+"&browser="+browser+"&val="+val,
success: function (response) {}
});
});
in SetDefaultLanguage.java
:
//parametro del browser
if(request.getParameter("isolingua") != null)
{
isolingua = request.getParameter("isolingua");
switch (isolingua)
{
case "en":
request.getSession().setAttribute("lingua", 2);
Locale.setDefault(Locale.ENGLISH);
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
System.out.println("CASE EN: "+Locale.getDefault()+" - "+request.getSession().getAttribute(Globals.LOCALE_KEY));
break;
case "it":
request.getSession().setAttribute("lingua", 1);
Locale.setDefault(Locale.ITALIAN);
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ITALIAN);
System.out.println("CASE IT: "+Locale.getDefault()+" - "+request.getSession().getAttribute(Globals.LOCALE_KEY));
break;
default:
request.getSession().setAttribute("lingua", 1);
Locale.setDefault(Locale.ITALIAN);
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ITALIAN);
System.out.println("CASE DEFAULT: "+Locale.getDefault()+" - "+request.getSession().getAttribute(Globals.LOCALE_KEY));
break;
}
}
setting as browser language "italian"
, it prints always
CASE IT: it - it
but here it is the strange behavior: my index, registration or retrieve password pages can be:
italian
italian
and registration in english
italian
and just some words of retrieve password and registration in english
italian
other info:
request.getSession().setAttribute("lingua", <int>)
is used in servlets to retrieve the information into the db in the correct language; this always works correctly.Upvotes: 3
Views: 406
Reputation: 1
This is because you use Locale.setDefault()
What the doc says:
Sets the default locale for this instance of the Java Virtual Machine.
...
Since changing the default locale may affect many different areas of functionality, this method should only be used if the caller is prepared to reinitialize locale-sensitive code running within the same Java Virtual Machine.
Usually setting a session attribute Globals.LOCALE_KEY
enough to change a locale and you can use the code to save a locale
// Extract attributes we will need
HttpSession session = request.getSession();
// Get locale from request, if any
Locale locale = request.getLocale();
// If supplied, set Locale based on request parameter
String language = request.getParameter("isolingua");
if (language != null && language.length() > 0) {
switch (language) {
case "en":
locale = Locale.ENGLISH;
session.setAttribute("lingua", 2);
break;
default:
locale = Locale.ITALIAN;
session.setAttribute("lingua", 1);
break;
}
}
//Save locale
session.setAttribute(Globals.LOCALE_KEY, locale);
Upvotes: 2
Reputation: 649
Would you mind, try implementing it with a this following logic:
if (request.getParameter("isolingua") != null) { // check to avoid null pointer excptn.
if (request.getParameter("isolingua").equals("en")) {
// set english locale
}
else {
// set italian locale (our default)
}
}
I am sure, it wont land you in a pool of abnormal/unexpected cases.
Upvotes: 0