如何在java中处理多种语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1270067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:39:09  来源:igfitidea点击:

How to handle multiple languages in java?

javainternationalization

提问by MemoryLeak

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 ?

我正在编写一个使用 jsp 和 java 的程序,我如何使用属性文件来支持多种语言?顺便说一句,总有类似\u4345这样的东西,这是什么?他们怎么来?

采纳答案by Thilo

For the multiple languages, check out the ResourceBundleclass.

对于多种语言,请查看ResourceBundle类。

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.

关于\u4345,这是 Java 的阴暗和非常烦人的遗留角落之一。属性文件需要采用 ASCII 格式,以便所有非 ASCII 字符都需要编码为\uxxxx(它们的 Unicode 值)。您可以使用native2ascii 命令行工具转换文件以使用此编码。如果您使用的是 IDE 或构建工具,则应该有一个选项可以自动调用它。

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 encodingwhen you load it:

如果属性文件是您可以完全控制的内容,您可以从 Java6 开始,也可以直接在属性文件中使用 UTF-8(或任何其他字符集),并在加载时指定该编码

// new in Java6
props.load(new InputStreamReader(new FileInputStream(file), 'UTF-8'));

Again, this only works if you load the Properties yourself, not if someone else does it, such as a ResourceBundle (used for internationalization).

同样,这仅在您自己加载属性时才有效,而不是在其他人加载时有效,例如 ResourceBundle(用于国际化)。

回答by Peter

there is an entire tutorial on http://java.sun.com/docs/books/tutorial/i18n/index.html

http://java.sun.com/docs/books/tutorial/i18n/index.html上有完整的教程

This specifies and explains about anything you need to know.

这指定并解释了您需要了解的任何内容。

回答by Robert Petermeier

The Java tutorialon i18n has been mentioned already by Peter. If you are building JSPs you probably want to look at the JSTLwhich basically allows you to use the functionality of ResourceBundle through JSP tags.

Peter 已经提到了有关 i18n的Java 教程。如果您正在构建 JSP,您可能想查看JSTL,它基本上允许您通过 JSP 标记使用 ResourceBundle 的功能。