我想读阿拉伯语和波斯语键从Java文件,而不是性质&值与资源包文件,但它显示未知字符。如何阅读阿拉伯语或从Java文件波斯(键和值)与资源包
我所做的是我编写3个文件为:1。 LabelsBundle_en_US.java 2. LabelsBundle_ar_AE.java 3. LabelsBundle_fa_IR.java
我LabelsBundle_en_US.java文件看起来像:
public class LabelsBundle_en_US extends ListResourceBundle{
static final Object[][] contents = {
{"REGISTER","Registration Form"},
{"USERNAME","Email"},
{"PASSWORD","Password"},
{"CONFIRM_PASS","Confirm Password"},
{"SUBMIT","Register"}
};
protected Object[][] getContents(){
return contents;
}
}//class
我LabelsBundle_fa_IR.java文件看起来像:
public class LabelsBundle_fa_IR extends ListResourceBundle{
static final Object[][] contents = {
{"REGISTER","ثبت نام"},
{"USERNAME","ایمیل"},
{"PASSWORD","رمز"},
{"CONFIRM_PASS","مرور رمز"},
{"SUBMIT","ارسال"}
};
protected Object[][] getContents(){
return contents;
}
}//class
这里是我的servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
res.setContentType("text/html; charset=UTF-8");
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
StringWriter sWriter = new StringWriter();
PrintWriter out = new PrintWriter(sWriter);
String country = req.getParameter("country");
String language = req.getParameter("language");
Locale locale=null;
if(country == null){
locale = new Locale("en","US");
}
else{
locale = new Locale(language, country);
}
ResourceBundle rb = ResourceBundle.getBundle("com.i18n.resource.bundles.LabelsBundle",locale);
req.setAttribute("resource", rb);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"+
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fi\" dir=\"auto\">"+
"<head>"+
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />"+
"<body>"+
"<center><h1>"+rb.getString("REGISTER")+"</h1></center>"+
"<table border=0 width=540 align=center>"+
"<tr><td colspan=2 align=center><h1>"+rb.getString("REGISTER")+"</h1></td></tr>"+
"<tr><td>"+rb.getString("USERNAME")+"</td><td><input type=text name=username></td></tr>"+
"<tr><td>"+rb.getString("PASSWORD")+"</td><td><input type=password name=password></td></tr>"+
"<tr><td>"+rb.getString("CONFIRM_PASS")+"</td><td><input type=password name=cPass></td></tr>"+
"<tr><td colspan=2 align=center><input type=submit value="+rb.getString("SUBMIT")+"></td></tr>"+
"</table></html>");
res.getWriter().print(sWriter.toString());
}//doGet
此代码(S)工作正常时,语言是en_US,但是当我将其更改为阿拉伯语或波斯语然后它作为显示:
ط«ط¨طھ ظ†ط§ظ…
ط§غŒظ...غŒظ“
ط± ظ...ط²
ظ...ط±ظط±ط±ظ...ط² ط§ط±ط³ط§ظ“
请告知
您的解决方案是为ListResourceBundle的文件的工作完美。正如你建议我使用属性文件,并再次面临同样的问题。与Java资源包封装阿拉伯语,并不能准确显示波斯但奇怪的迹象 – Ghayel
答案是存在的:“在属性文件的编码是明确的:拉丁语1”。所以,如果你把UTF-8没有工作。您需要使用native2ascii并将其转换为转义形式。是的,这很笨拙,但这是标准的,官方的做法。这比在Java源代码上做得更可靠。它看起来像被(最终!)计划为Java 9. –
'海峡=新的String UTF-8的.properties(rb.getString(键).getBytes( “ISO-8859-1”), “UTF-8”); '解决我的问题 – Ghayel