问题描述
我正在编写一个使用 jsp 和 java 的程序.如何使用属性文件来支持多种语言?
i am writing a program use jsp and java. how can i use property files to support multiple languages?
顺便说一句,总有一些像 u4345 这样的东西.
这是什么?他们是怎么来的?
and by the way, there are always some things like u4345.
what is this? how do they come?
推荐答案
对于多种语言,请查看 resourcebundle 类.
for the multiple languages, check out the resourcebundle class.
关于 u4345,这是 java 的黑暗和非常烦人的遗留角落之一.属性文件需要是 ascii 格式,因此所有非 ascii 字符都需要编码为 uxxxx(它们的 unicode 值).您可以使用 native2ascii 命令转换文件以使用此编码线工具.如果您使用的是 ide 或构建工具,则应该有一个自动调用它的选项.
about the u4345, this is one of the dark and very annoying legacy corners of java. the property files need to be in ascii, so that all non-ascii characters need to encoded as uxxxx (their unicode value). you can convert a file to use this encoding with the native2ascii command line tool. if you are using an ide or a build tool, there should be an option to invoke this automatically.
如果属性文件是你自己可以完全控制的,你可以从 java6 开始,也可以直接在属性文件中使用 utf-8(或任何其他字符集),并且 在加载时指定编码:
if the property file is something you have full control over yourself, you can starting from java6 also use utf-8 (or any other character set) directly in the property file, and specify that encoding when you load it:
// new in java6 props.load(new inputstreamreader(new fileinputstream(file), 'utf-8'));
同样,这仅在您自己加载属性时才有效,而不是在其他人加载时才有效,例如 resourcebundle(用于国际化).
again, this only works if you load the properties yourself, not if someone else does it, such as a resourcebundle (used for internationalization).