Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3508 Members - Latest Member: NevilleKemp

26. May 2012, 03:12:13 pm
Xith3D CommunityGeneral CategoryGeneral Discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)Internationalization
Pages: [1]
Print
Author Topic: Internationalization  (Read 1543 times)
'n ddrylliog
Moderator
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« on: 14. February 2007, 04:47:27 pm »

Any tips about Internationalization ?

For my "Digibots!" population evolution project, I coded and wrote messages in English, but my examiners will be french-speaking (like me). But I don't want to translate everything and lose the original English version. So I suppose there's a way to internationalize that. Anyone experienced that before ?

Note : I post that while I'm investigating the matter.. you could save my time by sharing your experience Smiley
Logged
'n ddrylliog
Moderator
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« Reply #1 on: 14. February 2007, 05:07:59 pm »

Okay, fully working now.

The "Externalize Strings" feature of Eclipse is stunning !
Set up perfectly in a few minutes.. And it choose automatically from Locale (well, I actually had to tune the code).

Code:
/**
 * "Digibots" sourcecode is licenced under the
 * CeCILL license
 *
 * Made early 2007 by Amos Wenger, Olivier Charvet
 * and Quentin Merleau
 */
package org.digibots.world;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Messages {
   
    private static final ResourceBundle RESOURCE_BUNDLE;
    static {
        String BUNDLE_NAME = "org.digibots.world.messages"; //$NON-NLS-1$
        ResourceBundle bundle = null;
        try {
            bundle = ResourceBundle.getBundle(BUNDLE_NAME+"_"+Locale.getDefault().getCountry());
        } catch(Exception e) {
            System.out.println("Falling back to default language, because translation "
                    +Locale.getDefault().getDisplayName()+" hasn't been found");
            bundle = ResourceBundle.getBundle(BUNDLE_NAME);
        }
        RESOURCE_BUNDLE = bundle;
    }
   
    private Messages() {
    }
   
    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

Please re-use if you need it ! Smiley
Logged
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« Reply #2 on: 14. February 2007, 05:10:52 pm »

OMG, boo... I just finished looking up these APIs from memory and typing this LOL


At least for simple resource bundling, here are the APIs (without any attempt to organize them coherently or optimally):

Code:
import java.util.ResourceBundle;
import java.text.MessageFormat;
...
Locale currentLocale=...;
ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale);
StringBuffer userMessage = new StringBuffer();

String unformatted = myResources.getString( "message-1" );
MessageFormat formatter = new MessageFormat(unformatted,currentLocale);
formatter.format(arguments, userMessage, null );

unformatted = myResources.getString( "message-2" );
formatter = new MessageFormat(unformatted,currentLocale);
formatter.format(arguments, userMessage, null );

unformatted = myResources.getString( "message-3" );
formatter = new MessageFormat(unformatted,currentLocale);
formatter.format(arguments, userMessage, new String[] { calculatedValue1, calculatedValue2 } );

return userMessage.toString();

All the examples include the ability for an external property file to reformat dates and money.  The last example shows the ability to reorder values that your program must calculate because different languages choose to display them in different order.

Edit: added the import for MessageFormat
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
'n ddrylliog
Moderator
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« Reply #3 on: 14. February 2007, 05:22:27 pm »

Thanks  Grin And sorry for the false alarm
Logged
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« Reply #4 on: 14. February 2007, 05:31:07 pm »

Actually, I did realize after posting that my example included the correct combination with MessageFormat which is very useful depending on the selection of languages in your translation domain.
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #5 on: 14. February 2007, 06:20:53 pm »

Looks cool. I will make use of this technique when I come to this point in my game. If you would not have posted this here I might have written a worse implementation myself in the future. So thanks for the info Smiley.

Marvin
Logged
'n ddrylliog
Moderator
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« Reply #6 on: 15. February 2007, 11:16:10 am »

Looks cool. I will make use of this technique when I come to this point in my game. If you would not have posted this here I might have written a worse implementation myself in the future. So thanks for the info Smiley.
BTW, I'm willing to do any french translations needed for any (well, Java+Xith3D) game. (Don't do that yourself if you don't feel comfortable with the french language).
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic