System.getProperty("user.language") always return "en" not user current Language settings
This is now drying me mad. I have a javaApplet on top of a asp.net page. Within this javaApplet I must detect the user preferred language FireFox-Tools-Options-Content-Languages-Choose.I have 3 languages in there and the firstOne is Spanish/Spain[es-ES]by doing the following
String locale= System.getProperty("user.language")+"-"+System.getProperty("user.region"); I expect locale to be "es-ES"
but I always get "en-null" and user.language is always "en"
How do you get the userPreferred language in Java?
开发者_如何学CThe correct result should be "es-ES"
Any suggestions#? thanks a lot
You need to pass the locale as a parameter to the applet...
<APPLET ...>
<PARAM name="Locale" value="es-ES">
</APPLET>
This, of course, can be rendered by your server side ASP.NET code based on the Accept-Language header.
Then, use the applet's getParameter(String name)
method to retrieve the locale that was passed in.
Value returned by a call to System.getProperty() has no relation with the Firefox prefered langage. If you take a look at the computer language settings, i'm sure that you will discover that en and null doesn't come from nowhere. Now how to get browser prefered langages from within you applet is another question for which , to be honest, i have no answer.
Maybe this helps: Java Applet Locale setting
Maybe you can pass the browser preferred langage with javascript as an argument to your applet.
Applets run on the client machine and have no knowledge about requests sent by the browser to the server. If you want to include information from the request, I suggest adding that as a parameter in the applet tag when generating the web page.
Not sure what's going on, but why don't you just make use of the existing java.util.Locale
API?
Locale locale = Locale.getDefault();
Or even better, use the Applet's
inherited getLocale()
method:
Locale locale = getLocale();
[Edit] Oh, it's clear. You want to get the client application's preferred language, not the operating system platform's preferred language. In this case you need to let JS pass it in to the applet, in theory:
document.appletname.language = window.navigator.userLanguage;
精彩评论