如何根据 message.properties 检查区域设置

发布于 01-06 00:14 字数 294 浏览 4 评论 0原文

我正在使用 hibernate 和 RestEasy API 来制作服务器。我有 3 个属性文件 message.properties a、mesage_en-GB.properties 和 message_en.propperties 文件。 当我收到来自客户端的请求时,它的标头参数中有区域设置。我需要根据我的属性文件检查此区域设置,即如果它是 en-GB,则使用 message_en-GB.properties,如果未给出,则使用 message.properties,如果输入错误,则给出错误消息。 但我不知道如何比较这些值。 请建议。

I am using hibernate and RestEasy API to make a server. i have 3 properties files message.properties a, mesage_en-GB.properties and message_en.propperties files.
When I get a request from client , it have locale in header param. I need to check this locale against my properties file, i.e. if it is en-GB than use mesage_en-GB.properties, if not given than use message.properties, and if wrong enter than give error message.
but am not getting how to compare these values.
please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬2025-01-13 00:14:23

如果由于某种原因容器无法管理语言环境和捆绑包,您将需要手动管理它们。

我假设您的类路径中有属性文件。

首先,您需要创建要使用的区域设置。例如,您从 HTTP 标头获取语言作为 String fr,将其传递给 Locale.forLanguageTag(String) 方法或使用 语言环境(字符串)< Java6 中的 /a> 构造函数并获取 Locale 对象:

Locale locale = Locale.forLanguageTag(stringFromHeader) // since Java 7
Locale locale = new Locale(stringFromHeader) // prior to Java 7

然后将此区域设置传递给 ResourceBundle.getBundle 方法来获取正确的包:

ResourceBundle messages = ResourceBundle.getBundle("messages", locale);

然后调用 ResourceBundle 的 getString(key:String) 方法获取本地化字符串:

messages.getString("my_message")

要确保此方法100% 有效,您可以按照以下简单步骤操作:

1) 在某处创建文件夹 tmp

2) 在该文件夹中创建包含以下内容的文件 A.java

import java.util.*;

public class A {

public static void main(String[] args) throws Exception {
  Locale locale = new Locale("en");
  ResourceBundle messages = ResourceBundle.getBundle("messages", locale);
  System.out.println(messages.getString("my_message"));
  locale = new Locale("fr");
  messages = ResourceBundle.getBundle("messages", locale);
  System.out.println(messages.getString("my_message"));
}   
}

3) 创建文件 messages.properties 包含以下内容:

my_message=hello

4) 创建文件 messages_fr.properties 包含以下内容:

my_message=salut

5) 转到 tmp 文件夹并运行两个命令:

javac A.java
java -cp . A

输出是:

hello
salut

我希望这对您有用!

附:
这里有一个很棒的演练:http://docs.oracle。 com/javase/tutorial/i18n/resbundle/propfile.html

If locales and bundles can't be managed by a container for some reason you will need to manage them manually.

I assume you have property files in your classpath.

First of all you need to create the Locale that you're going to use. For example you get language from HTTP header as String fr, you pass it to Locale.forLanguageTag(String) method in Java7 or use Locale(String) constructor in Java6 and get Locale object:

Locale locale = Locale.forLanguageTag(stringFromHeader) // since Java 7
Locale locale = new Locale(stringFromHeader) // prior to Java 7

Then you pass this locale to the ResourceBundle.getBundle method to get the right bundle:

ResourceBundle messages = ResourceBundle.getBundle("messages", locale);

Then you call ResourceBundle's getString(key:String) method to get localized String:

messages.getString("my_message")

To make sure that this method 100% works you can follow the next simple steps:

1) Create a folder tmp somewhere

2) In that folder create file A.java with the following contents:

import java.util.*;

public class A {

public static void main(String[] args) throws Exception {
  Locale locale = new Locale("en");
  ResourceBundle messages = ResourceBundle.getBundle("messages", locale);
  System.out.println(messages.getString("my_message"));
  locale = new Locale("fr");
  messages = ResourceBundle.getBundle("messages", locale);
  System.out.println(messages.getString("my_message"));
}   
}

3) Create file messages.properties with the following contents:

my_message=hello

4) Create file messages_fr.properties with the following contents:

my_message=salut

5) Go to tmp folder and run two commands:

javac A.java
java -cp . A

The output is:

hello
salut

I hope that is does work for you!

PS:
There is a great walk-through here: http://docs.oracle.com/javase/tutorial/i18n/resbundle/propfile.html

临风闻羌笛2025-01-13 00:14:23

您需要在 Spring 配置中注册一个 LocaleChangeInterceptor 并使用 标签来输出消息。

这是一个很棒的演练: http: //viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html

PS
此机制可能因服务器而异,在某些情况下,使用 可能是更好的替代方案。

You will need to register a LocaleChangeInterceptor in Spring configuration and use <spring:message> tag to output messages.

Here's a great walkthrough: http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html

PS
This mechanism can change from server to server, in some cases using <fmt:message> could be a better alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文