4

I have a class called Info, and i have a bunch of static String variables described in it.

public class Info{

    public static stringOne= "Hello";
    public static stringTwo = "world";

}

and i'm hoping to access these variables as Info.stringTwo from other classes.

1.) I need to know if this is java-Internationalization that i have applied here ? (I have all the messages that i will display in the application assigned in this class. And, i am hoping to have different languages support to the app as well)

Illep
  • 16,375
  • 46
  • 171
  • 302
  • 3
    Have you read [this tutorial](http://docs.oracle.com/javase/tutorial/i18n/index.html)? – assylias Nov 29 '12 at 18:54
  • possible duplicate of [Java Preferences and Internationalization (i18n)](http://stackoverflow.com/questions/5588837/java-preferences-and-internationalization-i18n) – CoolBeans Nov 29 '12 at 18:59
  • No, it is not java-Internationalization. It is some wheel re-inventing technique that does not make any sense. Please follow the standard way (see assylias' comment). – Paweł Dyda Nov 29 '12 at 19:02

4 Answers4

3

Have a look at Resource bundle

A copy paste from the documentation:

When your program needs a locale-specific object, it loads the ResourceBundle class using the getBundle method:

 ResourceBundle myResources =
 ResourceBundle.getBundle("MyResources", currentLocale);

Resource bundles contain key/value pairs. The keys uniquely identify a locale-specific object in the bundle.

Here's an example of a ListResourceBundle that contains two key/value pairs:

 public class MyResources extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] {
             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
             {"OkKey", "OK"},
             {"CancelKey", "Cancel"},
             // END OF MATERIAL TO LOCALIZE
        };
     }
 }

Keys are always Strings. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also Strings--"OK" and "Cancel"--but they don't have to be. The values can be any type of object. You retrieve an object from resource bundle using the appropriate getter method. Because "OkKey" and "CancelKey" are both strings, you would use getString to retrieve them:

button1 = new Button(myResources.getString("OkKey"));
button2 = new Button(myResources.getString("CancelKey"));
Frank
  • 16,476
  • 7
  • 38
  • 51
2

Here is an example from here:-

  import java.util.*;

  public class InternationalizationDemo {
  public static void main(String[] args) {
  String language;
   String country;
  Locale locale;
  ResourceBundle rb;

  if (args.length != 2) {
 language = new String("en");
  country = new String("US");
  }
 else {
 language = new String(args[0]);
 country = new String(args[1]);
 }
 locale = new Locale(language, country);
 rb = ResourceBundle.getBundle("MessagesBundle", locale);
 System.out.println(rb.getString("localeInfo") + " ( " + 
locale.getDisplayLanguage() + "," + locale.getDisplayCountry() + ").\n");
 System.out.println(rb.getString("welcome"));
 System.out.println(rb.getString("sayThanks"));
  }
 }
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Though using a ResourceBundle is the traditional and most well-known approach to internationalization in Java, it is possible to make internationalization data available as class members, somewhat similar to the way you seek.

You can further put your strings for some message in different languages in a Map, indexed by language. And make this Map a static member of some class. Thus you get the ability to reference these string collections for messages by their class member names in a compiler-checked manner. And next, if you have a way to select preferred user language at run time (you have to have it), you just pick the right string from an appropriate collection using its language key, boiling down to something like this:

logger.info (MyClassWithMessages.MY_MULTILANGUAGE_MESSAGE.s ());

And the s() method to be added to your Map subclass can be made resposible for dealing with user preferences and selection from Map by language key.

That said, the remaining task is just to formulate a convenient API for all this... You are welcome to have a look at such an implementation on my blog page Look Ma, no ResourceBundle :) ..., and the next page that goes ahead with message formatting arguments.

Sergey Ushakov
  • 2,425
  • 1
  • 24
  • 15
0

For internationalization of Java and other applications I implemented a Message Compiler, which creates the resource bundle files and constant definitions as Java enum or static final strings for the keys from one single source file. So the constants can be used in the Java source code, which is a much safer way than using plain string constants. The message compiler cannot only be used for Java. It creates also resource files and constants for Objective-C or Swift and can be extended for other programming environments.

thmayr
  • 57
  • 6