Problem in loading chinese properties file [*.properties]
Hi !
I want to load the "Chinese.properties" file in object of Properties Class. "Chinese.properties" is a unicode based language file. This file includes keys in english and values in chinese [actual representation not unicodes]. When I load it in my object of properties. It is not loading properly. Here is my sample code:
publicclass ASC_LoadLanguage{
publicstatic String m_strLangName;
publicstatic Properties m_objCurrentLang =new Properties();
publicstatic String m_strEncoding ="Unicode";
public ASC_LoadLanguage(){
}
publicstaticvoid main(String[] args){
try{
ASC_LoadLanguage obj_LoadLanguage =new ASC_LoadLanguage();
obj_LoadLanguage.loadLanguage(args[3]);
UIManager.put("OptionPane.messageFont",new Font("Arial Unicode MS", Font.PLAIN, 11));
//This part of code is not displaying any thing. actually it contains null
//The name of key is accurate. Fetches data for other languages e.g. English, //Spanish.
JOptionPane.showMessageDialog(null, m_objCurrentLang.getProperty(ASC_Keys.KEY1));
}
catch (Exception ex){
ex.printStackTrace();
}
}
publicvoid loadLanguage(String a_strLanguagePath){
try{
System.out.println("Loading language: " + a_strLanguagePath);
InputStream is =new FileInputStream(a_strLanguagePath);
m_objCurrentLang.load(is);
is.close();
is =null;
System.out.println("Current language to be used is: " + a_strLanguagePath);
}
catch (Exception a_e){
a_e.printStackTrace();
}
}
}
Please help me in this regrad. If I'm doing anything wrong.
Regards,
KS.
[3118 byte] By [
k5ma] at [2007-10-3 3:47:35]

> "Chinese.properties" is a unicode based language file. Okay. But does it follow the rule that it has to be encoded in ISO-8859-1 with Unicode escapes for non-Latin-1 characters? The API documentation for the Properties class explains this in detail.
Thanx for your interest. I have resolved this Issue by this:
I have used *.xml as my properties file instead of using *.properties.
Here is the sample code:
public class ASC_LoadLanguage {
public static String m_strLangName;
public static Properties m_objCurrentLang = new Properties();
public static String m_strEncoding = "Unicode";
public ASC_LoadLanguage() {
}
public static void main(String[] args) {
try {
ASC_LoadLanguage obj_LoadLanguage = new ASC_LoadLanguage();
//obj_LoadLanguage.loadLanguage(args[3]); //Loading from "*.properties" file
obj_LoadLanguage.loadLanguageFromXML(args[4]); //Loading from "*.xml" file
m_objCurrentLang.list(System.out);
UIManager.put("OptionPane.messageFont", new Font("Arial Unicode MS", Font.PLAIN, 11));
JOptionPane.showMessageDialog(null, m_objCurrentLang.getProperty(ASC_Keys.KEY1));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void loadLanguageFromXML(String a_strLanguagePath){
try {
System.out.println("Loading language: " + a_strLanguagePath);
InputStream is = new FileInputStream(a_strLanguagePath);
m_objCurrentLang.loadFromXML(is);
is.close();
is = null;
System.out.println("Current language to be used is: " + a_strLanguagePath);
}
catch (Exception a_e) {
a_e.printStackTrace();
}
}
public void loadLanguage(String a_strLanguagePath){
try {
System.out.println("Loading language: " + a_strLanguagePath);
InputStream is = new FileInputStream(a_strLanguagePath);
m_objCurrentLang.load(is);
is.close();
is = null;
System.out.println("Current language to be used is: " + a_strLanguagePath);
}
catch (Exception a_e) {
a_e.printStackTrace();
}
}
}
//-XML File contents
<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Testing XML Properties</comment>
<entry key="KEY1">喂的喂</entry>
<entry key="KEY2">喂的喂喂的喂</entry>
<entry key="KEY3">喂喂喂喂</entry>
<entry key="KEY4">喂的</entry>
<entry key="KEY5">喂的喂喂的喂</entry>
</properties>
Regards,
KS
k5ma at 2007-7-14 21:44:33 >

> Thanx for your interest. I have resolved this Issue> by this:> > I have used *.xml as my properties file> instead of using *.properties.An even better solution! I should have considered that possibility.
Looks like you've already resolved the issue using the loadFromXML() method, so this is fyi.
In the upcoming JDK 6, an overloaded method of Properties.load(), which takes a Reader instance, was added. So Properties files in any encoding can be loaded into Properties directly with this method.
Naoto
naotoa at 2007-7-14 21:44:33 >

Hi
I'm doing the following task, here is sample code:
//import statements
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
//args[0] is not null, it contains "default.xml"
loadLanguageFromXML(args[0]);
public void loadLanguageFromXML(String a_strLanguagePath){
try {
m_objCurrentLang = new Properties();
System.out.println("Loading language: " + a_strLanguagePath);
InputStream is = new FileInputStream(a_strLanguagePath);
//exception is coming at the line below
m_objCurrentLang.loadFromXML(is);
is.close();
is = null;
System.out.println("Current language to be used is: " + a_strLanguagePath);
}
catch (Exception a_e) {
a_e.printStackTrace();
}
}
But I'm having following exception at the line of code:
m_objCurrentLang.loadFromXML(is);
java.util.InvalidPropertiesFormatException: java.lang.NullPointerException
at java.util.XMLUtils.load(Unknown Source)
at java.util.Properties.loadFromXML(Unknown Source)
at ASC_LoadLanguage(ASC_LoadLanguage.java:91)
Caused by: java.lang.NullPointerException
at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:257)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:209)
at java.util.XMLUtils.getLoadingDoc(Unknown Source)
... 4 more
This exception is occurring when I want to load XML-property file. Note that It is running fine in my IDE, but when I created its release and run from a *.bat file then it is giving above defined exception.
Here is code of my *.bat file
Batch File name...LoadXMLLang.bat
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
JRE\bin\javaw -classpath "lib\asc_csv2properties.jar" ASC_LoadLanguage "default.xml"
pause
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
//-File contents of default.xml
<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Testing XML Properties</comment>
<entry key="KEY1">喂的喂</entry>
<entry key="KEY2">喂的喂喂的喂</entry>
<entry key="KEY3">喂喂喂喂</entry>
<entry key="KEY4">喂的</entry>
<entry key="KEY5">喂的喂喂的喂</entry>
</properties>
Please help me in this regard, because I'm unable to resolve the issue.
Regards,
KS
k5ma at 2007-7-14 21:44:33 >

My problem is resolved.Regards,KS
k5ma at 2007-7-14 21:44:33 >
