网站的国际化 [英] Internationalization in a website
问题描述
我使用ResourceBundle方法getBundle(Propertyfilename,Local.languagename)类返回本地的ResourceBundle对象
I m using ResourceBundle method getBundle(Propertyfilename,Local.languagename) class which returns an object of ResourceBundle of the local
rb = ResourceBundle.get Bundle(Locale字符串,Locale.ARABIC); - 不支持
rb = ResourceBundle.get Bundle("Locale Strings", Locale.ARABIC);-Not Supported
我如何使用它来支持阿拉伯语,乐队英语。
How can i use this to support arabic,band english.
推荐答案
首先, java.util.Locale
确实没有 Locale.ARABIC
常量。但这不应该阻止你自己使用 Locale
构造函数来定义语言代码:
First, the java.util.Locale
indeed doesn't have a Locale.ARABIC
constant. But that shouldn't stop you from defining it yourself using the Locale
constructor taking the language code:
public static final Locale ARABIC = new Locale("ar");
其次,默认情况下属性文件位于 ResourceBundle
引擎盖下,读作 InputStream
,带有 ISO-8859-1
编码。所以你真的需要有两个不同的属性文件,一个是 UTF-8
编码,用于维护这些值(例如 text_ar.properties .utf8
)和其他 ISO-8859-1
编码(例如 text_ar.properties
)您刚刚用于提供给Java应用程序。您可以使用 native2ascii
工具将 UTF-8
文件转换为 ISO-8859- 1
文件如下:
Second, by default the properties files are under the ResourceBundle
hoods read as an InputStream
with the ISO-8859-1
encoding. So you'll really need to have two different properties files, one in UTF-8
encoding which you use to maintain the values (e.g. text_ar.properties.utf8
) and other in ISO-8859-1
encoding (e.g. text_ar.properties
) which you just use to provide to the Java application. You can use the native2ascii
tool to convert from UTF-8
file to ISO-8859-1
file as follows:
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_ar.properties.utf8 text_ar.properties
这将转换非 ISO-8859-1
Unicode代码点的字符。例如。 القيمة
将成为 \\\ا \ u0644 \ u0642 \ u064a \ u0645 \ u00629
(因此使其无法维护,因此建议保留原始文件进行编辑。)
This will convert non ISO-8859-1
characters to Unicode codepoints. E.g. القيمة
would become \u0627\u0644\u0642\u064a\u0645\u0629
(which thus makes it unmaintainable, hence the recommendation to keep the original for editing).
或者,如果您已经使用Java 6,那么您可以使用 ResourceBundle.Control #newBundle()
和 PropertyResourceBundle
构造函数使用 Reader
使用 UTF-8 $ c $读取属性文件c>默认情况下。
Or, if you're already on Java 6, then you can make use of ResourceBundle.Control#newBundle()
and the PropertyResourceBundle
constructor taking an Reader
to read the properties files using UTF-8
by default.
这是一个启动示例,假设有一个普通 UTF-8
编码 text_ar.pro具有以下内容的类路径中的
文件:
Here's a kickoff example assuming that there's a "plain" UTF-8
encoded text_ar.properties
file in the classpath with the following content:
key=القيمة
package com.stackoverflow.q2183245;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;
public class Test {
private static final Locale ARABIC = new Locale("ar");
private static final Control UTF8CONTROL = new UTF8Control();
public static void main(String args[]) throws Exception {
ResourceBundle bundle = ResourceBundle.getBundle("text", ARABIC, UTF8CONTROL);
System.out.println(bundle.getString("key")); // Prints القيمة
}
}
class UTF8Control extends Control {
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// This line is changed to make it to read properties files as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
stream.close();
}
}
return bundle;
}
}
注意: newBundle()
方法是从原始源进行复制并略微更改以使代码示例更简洁,如果您希望将其还原为原始并且只需使用 PropertyResourceBundle更改该行
构造让它采取 UTF-8
编码读者
。
Note: the newBundle()
method is copypasted from original source and slightly changed to make the code example less verbose, if you want you can revert it to original and just change the line with PropertyResourceBundle
construct to let it take an UTF-8
encoded Reader
.
这篇关于网站的国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!