2

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jo.
  • 187
  • 2
  • 5
  • 9

5 Answers5

2

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
  • 833
  • 5
  • 14
1

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • No, I think that's where his problem is: He needs the BROWSER's locale, not Java's. – Carl Smotricz Nov 05 '09 at 14:59
  • 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 expect any suggestions – Jo. Nov 05 '09 at 15:05
  • are you saying before I call my javaApplet in asp.net I have script that is written as you said document.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. Nov 05 '09 at 15:11
  • 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 Nov 05 '09 at 15:15
1

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
  • 3,465
  • 2
  • 23
  • 26
1

Maybe this helps: Java Applet Locale setting

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

Community
  • 1
  • 1
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
1

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
  • 145,806
  • 30
  • 211
  • 305