我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
当前回答
http://sourceforge.net/projects/eclipse-rbe/
如前所述,属性文件应该用ISO 8859-1编码
您可以使用上面的eclipse IDE插件为您进行Unicode转换。
其他回答
对于当前(2021-2)Java版本,仍然有旧的ISO-8859-1函数utils.Properties#load。
如果您使用属性。装载时必须使用ISO-8859-1。 如果使用ResourceBundle,那么UTF-8应该没问题。
请允许我引用官方文件。
PropertyResourceBundle
PropertyResourceBundle can be constructed either from an InputStream or a Reader, which represents a property file. Constructing a PropertyResourceBundle instance from an InputStream requires that the input stream be encoded in UTF-8. By default, if a MalformedInputException or an UnmappableCharacterException occurs on reading the input stream, then the PropertyResourceBundle instance resets to the state before the exception, re-reads the input stream in ISO-8859-1, and continues reading. If the system property java.util.PropertyResourceBundle.encoding is set to either "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding, and throws the exception if it encounters an invalid sequence. If "ISO-8859-1" is specified, characters that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section 3.3 of The Java™ Language Specification whereas the other constructor which takes a Reader does not have that limitation. Other encoding values are ignored for this system property. The system property is read and evaluated when initializing this class. Changing or removing the property has no effect after the initialization.
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/PropertyResourceBundle.html
属性#负载
从输入字节流中读取属性列表(键和元素对)。输入流采用load(Reader)中指定的简单的面向行的格式,并假定使用ISO 8859-1字符编码;即每个字节是一个拉丁字符。非拉丁字符1和某些特殊字符使用Java™语言规范3.3节中定义的Unicode转义符在键和元素中表示。
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Properties.html加载(java.io.InputStream)
不管怎样,我的问题是文件本身的编码是错误的。使用iconv对我很有效
iconv -f ISO-8859-15 -t UTF-8 messages_nl.properties > messages_nl.properties.new
Java 9及更新版本
从Java 9开始,属性文件默认编码为UTF-8,使用ISO-8859-1以外的字符应该可以开箱即用。
Java 8及以上版本
当指定了.properties文件时,ResourceBundle#getBundle()在封面下使用PropertyResourceBundle。这反过来使用默认的Properties#load(InputStream)来加载这些属性文件。根据javadoc,它们默认读取为ISO-8859-1。
public void load(InputStream stream)抛出IOException
从输入字节流中读取属性列表(键和元素对)。输入流采用load(Reader)中指定的简单的面向行的格式,并假定使用ISO 8859-1字符编码;即每个字节是一个拉丁字符。非拉丁字符1和某些特殊字符使用Java™语言规范3.3节中定义的Unicode转义符在键和元素中表示。
So, you'd need to save them as ISO-8859-1. If you have any characters beyond ISO-8859-1 range and you can't use \uXXXX off top of head and you're thus forced to save the file as UTF-8, then you'd need to use the native2ascii tool to convert an UTF-8 saved properties file to an ISO-8859-1 saved properties file wherein all uncovered characters are converted into \uXXXX format. The below example converts a UTF-8 encoded properties file text_utf8.properties to a valid ISO-8859-1 encoded properties file text.properties.
native2ascii -encoding UTF-8 text_utf8.properties text.properties
在使用Eclipse等正常的IDE时,当您在基于Java的项目中创建.properties文件并使用Eclipse自己的编辑器时,这已经自动完成了。Eclipse将透明地将超出ISO-8859-1范围的字符转换为\uXXXX格式。另见下面的截图(注意底部的“属性”和“源”选项卡,点击放大):
或者,您也可以创建一个定制的ResourceBundle。控件实现,其中您显式读取属性文件为UTF-8使用InputStreamReader,这样您就可以将它们保存为UTF-8而不需要与native2ascii的麻烦。下面是一个开始的例子:
public class UTF8Control extends Control {
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
// The below is a copy of the default implementation.
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 {
// Only 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;
}
}
可以这样使用:
ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control());
参见:
Unicode -如何得到正确的字符?
我们创建了一个资源。utf8文件,包含UTF-8格式的资源,并有一个规则运行如下:
native2ascii -encoding utf8 resources.utf8 resources.properties
从Java 9开始,加载属性文件的默认值已更改为UTF-8。https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm