aspose file tools*
The moose likes Java in General and the fly likes I18n problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I18n problem" Watch "I18n problem" New topic
Author

I18n problem

Monoj Roy
Ranch Hand

Joined: Oct 10, 2007
Posts: 98
I was trying to understand the Internationalization . So I have written the
following two properties file and and the Java class .

They are working fine when I am invoking the Java class with command line argument also (Passing the argument for local) for example
% java I18NSample en US
Hello.
How are you?
Goodbye.

Please clear my confusion .What I understand when I will run this program from US it should output US version properties file and If I run it from a
German machine it should fetch the German value .

My program is able to fetch the correct data only after I pass the Local values as a parameter in main() .So is it the programmer's responsibility
to identify from which country the client is asking data and depending that
we will call the correct version of properties file ?

If the answer is yes then how do I know who is calling my server what is the
local for this client ?

Please see the code below from sun :



MessagesBundle_en_US.properties

greetings = Hello.
farewell = Goodbye.
inquiry = How are you?



MessagesBundle_de_DE.properties

greetings = Hallo.
farewell = Tsch��.
inquiry = Wie geht's?



I have written the following Java Code

import java.util.*;

public class I18NSample {

static public void main(String[] args) {

String language;
String country;

if (args.length != 2) {
language = new String("en");
country = new String("US");
} else {
language = new String(args[0]);
country = new String(args[1]);
}

Locale currentLocale;
ResourceBundle messages;

currentLocale = new Locale(language, country);

messages =
ResourceBundle.getBundle("MessagesBundle",currentLocale);

System.out.println(messages.getString("greetings"));
System.out.println(messages.getString("inquiry"));
System.out.println(messages.getString("farewell"));
}
}
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
You can use

and


Take a look at the Locale class
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Or even better, change your code to



And create a MessagesBundle.properties with the default properties in case the Locale is not en_US neither de_DE
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
According to the javadoc (I haven't tried it), if you don't specify a Locale, the getBundle method used the default
i.e.should do what you want


Joanne
Monoj Roy
Ranch Hand

Joined: Oct 10, 2007
Posts: 98
Thanks for all your reply .

But I am not able to get what I am exactly want .May I not communicated clearly to understand what I want .

I want a way if it is there in I18N so that my program will automatically
fetch the correct properties value . I mean I will write a program and deploy it in server now if Germany comes as a client it will pick the German properties file if US come it will pick the English one .

As you mentioned if I use the default that will be Say language English So fetching default will not provide the German client the German properties file .It it will pick up default and show a German client a English one .

I mean I am asking.... should my application know the local ,means is that mandatory to pass the local value through URL or something ?So that knowing that value it will fetch the correct properties file ?


To be very clear when I hit the google from india I get the english one but when a chinies open the google.com he opens the chinies page so How thease works ?
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Yes, in this case, you must send to the server the locale of the client.
In Google's case, the Locale is sent by the browser in the header of the request.
You can use the same approach (if it's a web app).

Make this experiment:
Try change you language configuration in your browser (e.g. on IE7*, Tools/Internet Options, click on button Languages). Add a new language on the top of the list and access Google site.

* Sorry I don't have Firefox installed here, but you can do the same on it
Monoj Roy
Ranch Hand

Joined: Oct 10, 2007
Posts: 98
Thanks ..Thanks a lot . I got the Idea now .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I18n problem