Write an multi-languages application ( i18n )

Put your problem here if it does not fit any of the other categories.

Write an multi-languages application ( i18n )

Postby thoas » Sun Mar 09, 2008 2:14 pm

Hi,

1) XML side

I want to include many languages in my application which has for default language : english.

So, i read this link which shows how to include several strings.xml files by language in different directory in /values.
My question is simple how to make a reference to these files, for example with a simple strings.xml, if I want to make a reference, i use :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.     <Button android:id="@+id/ok"
  2.            android:layout_width="wrap_content"
  3.            android:layout_height="wrap_content"
  4.            android:layout_below="@id/entry"
  5.            android:layout_alignParentRight="true"
  6.            android:layout_marginLeft="10px"
  7.            android:text="@string/hello_word" />
  8.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


But in my case, there are 2 files (for example) in /values-en and /values-es so the current reference (in my code) is deprecated.

2) Java side

Is anybody use ResourceBundle with success?

This code doesn't work :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         ResourceBundle rb = ResourceBundle.getBundle("myApps/lang/");
  2.         _edit = (TextView) this.findViewById(R.id.label);
  3.         _edit.setText(rb.getString("machin"));
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I get an MisingResourceException. I think the path is invalide but I don't find the correct sentence.

Thank you in advance
User avatar
thoas
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 09, 2008 1:45 pm

Top

Postby Jodol » Sun Mar 09, 2008 4:30 pm

Hi Thoas,

The answer to your first question is simple. You don't have to address the right language file. Android will decide which language file to use depending on your global language settings.

Hopefull someone else can help you with the second question, for i haven't tried anything with resource bundles.

Greetz,

Jodol
Member of a Dutch company developing applications for Android.
ColdTrick Solutions
User avatar
Jodol
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Dec 19, 2007 6:29 pm
Location: Netherlands

Postby thoas » Sun Mar 09, 2008 5:46 pm

1) XML side

Thank you for your reply.

You are right for the 1), but according of this scheme, even if I decide to force the language setting in my application
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Locale.setDefault(new Locale("fr", "FR"));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it doesn't work whereas android must normally take the directory /values-fr, musn't it? Or does the user of Android have to set the global language settings in the OS?

if the answer is "yes" : where are the language settings in Android?

else : is there another method?

2) Java side

I make my own answer for my second point. I have not found the answer for ResourceBundle.getBundle(), but we can redirect this problem with PropertyResourceBundle with the full emulator data path.

First, i found this "hack" in an issue of android svn here

The ResourceBundle issue may not be resolved...

To start, we have these files in /lang which is in the project directory :
Languages_es_ES.properties
Languages_en_US.properties (the default Locale in my emulator)

We need to move it in the path /data/data/mypackageproject of emulator. To do that, launch adb in /tools folder of the sdk and enter this command :

adb push /path/to/myproject/lang /data/data/mypackageproject

Then, you will see files are uploading.

Now to load the resourcebundle, i apply this java source :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. _loc = new Locale("es", "ES");
  2. ResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(new File("/data/data/mypackageproject/Languages_"+_loc.toString()+".properties")));
  3. Log.v("MyProject", "test : " + bundle.getString("hello")); // return "test : hola" in the ddms
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Normally, it works but, i don't know why this sentence doesn't work :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ResourceBundle bundle = ResourceBundle.getBundle("/data/data/mypackageproject/Languages");
  2.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
User avatar
thoas
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 09, 2008 1:45 pm

Postby plusminus » Mon Mar 10, 2008 12:29 am

Hello thoas,

thoas wrote:1) XML side

Thank you for your reply.

You are right for the 1), but according of this scheme, even if I decide to force the language setting in my application
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Locale.setDefault(new Locale("fr", "FR"));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


it doesn't work whereas android must normally take the directory /values-fr, musn't it? Or does the user of Android have to set the global language settings in the OS?

if the answer is "yes" : where are the language settings in Android?

else : is there another method?


Android will pick which of the various underlying resource files fits best during runtime, depending on the current device/system-configuration.
Also the Locale-Settings is currently not changeable in the emulator.

I haven't tried it, but setting a separate locale inside an application is likely, but I'm not sure whether switching the resources during runtime is possible... :?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby thoas » Mon Mar 10, 2008 11:26 am

So if i understood, language settings depend of the mobile which includes Android?

It's a bit abstract.

Hum... I will include my language files, but the directory /values-en for my XML will be always used as long as i can't change Android configuration.

Thank you for your reply.
User avatar
thoas
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 09, 2008 1:45 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google Feedfetcher and 17 guests