tags:

views:

399

answers:

5

Hi, 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?

The correct result should be "es-ES"

Any suggestions#? thanks a lot

A: 

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;
BalusC
No, I think that's where his problem is: He needs the BROWSER's locale, not Java's.
Carl Smotricz
You're right. Extended answer.
BalusC
yes I need the BROWSER's locale not java's.Is this possible in java? in .net you do HttpContext.Current.Request.UserLanguages[0]; or HttpContext.Current.Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")I cannot beleive i cannot do in Java.I am a total novice in java ,Locale.getDefault(); does not return what I expectany suggestions
Jo
are you saying before I call my javaApplet in asp.net I have script that is written as you saiddocument.appletname.setLocale(window.navigator.Language);then I can do the Locale locale=Locale.getDefault();and that will get me "es-ES" "preferred language in browser"Is this correct?
Jo
I have never done Applets, but that's the theory how to invoke a method of an Java Applet using Javascript. You may find this Sun tutorial useful as well: http://java.sun.com/docs/books/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html
BalusC
A: 

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.

Olivier
A: 

Maybe this helps: http://stackoverflow.com/questions/227853/java-applet-locale-setting

Maybe you can pass the browser preferred langage with javascript as an argument to your applet.

PeterMmm
A: 

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.

Tom Hawtin - tackline
+1  A: 

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.

Chris R
This looks like the way togo.I will have a go thanks
Jo