如何在java中处理多种语言? [英] How to handle multiple languages in java?
问题描述
我正在编写程序使用jsp和java,
如何使用属性文件来支持多种语言?
顺便说一下,总有类似\\\䍅,
这是什么?他们是怎么来的?
I am writing a program use jsp and java, how can i use properties files to support multiple languages ? and by the way, there are always something like \u4345, What's 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).
这篇关于如何在java中处理多种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!