如何在Java应用程序中处理多种语言? [英] How to handle multiple languages in Java apps?
问题描述
我正在使用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?
推荐答案
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屋!