问题描述
我正在编写一个使用 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).
这篇关于如何在 Java 应用程序中处理多种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!