In Java, you can use the *.java class as a resource file for multi-language internationalization.
1. First define the class, the class must inherit ListResourceBundle
The path to the class is: src/i18n
1 Public classMymessage_en_usextendsListResourceBundle {2 3 Private FinalObject mydata[][] = {{"Hello", "hello! " },4{"Greetting", "Love you." {0} " } };5 6 @Override7 protectedobject[][] getcontents () {8 returnMyData;9 }Ten One}
2. Writing test cases, using resource files
This file is in the default package
Static void Main () { new Locale ("zh", "CN"); = Resourcebundle.getbundle ("Mymessage", current); = Bundle.getstring ("greetting"); " Jay "));
Unfortunately, it's a mistake.
Exception in thread "main" Java.util.MissingResourceException:Can ' t find bundle for base name Mymessage, Locale ZH_CN At java.util.ResourceBundle.throwMissingResourceException (Unknown Source) at Java.util.ResourceBundle.getBundleImpl (Unknown source) at Java.util.ResourceBundle.getBundle (Unknown source) At program . I18ntest (Program.java:) at program.main (Program.java:20)
After many attempts, the main functions of class Mymessage_en_us and invocation are placed under the default package. That's it.
Output results
Hello, Jay.
Seems to be a resource path problem, after a variety of attempts to modify the path, as follows
Private Static void i18ntest () { new Locale ("zh", "CN"); = Resourcebundle.getbundle ("i18n.mymessage", current); = Bundle.getstring ("greetting"); " Jay "));
Instead of modifying the path where the file is stored.
Java:missingresourceexception, "Can ' t find bundle for base name Mymessage, Locale ZH_CN"