Java的getlocale,Java ResourceBundle getLocale()方法与示例

ResourceBundle类getLocale()方法getLocale()方法在java.util包中可用。

getLocale()方法用于获取此ResourceBundle的语言环境。

getLocale()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

返回语言环境时,getLocale()方法不会引发异常。

语法:public abstract Enumeration getLocale();

参数:它不接受任何参数。

返回值:

此方法的返回类型为Locale,它获取此ResourceBundle语言环境。

示例//Java程序演示示例

//getLocale()ResourceBundle的语言环境方法的说明

import java.util.*;

public class GetLocaleOfResourceBundle {

public static void main(String[] args) {

//实例化ResourceBundle

//一些语言环境

ResourceBundle rb = ResourceBundle.getBundle("IncludeHelp...", Locale.JAPAN);

System.out.println("rb.getKeys(): ");

//通过使用getLocale()方法是

//返回此rb的语言环境

Locale lo = rb.getLocale();

System.out.println("rb.getLocale(): " + lo);

}

}

输出结果ja_JP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# i18n-demo 基于i18n利用springboot实现后台国际化demo 引入外部依赖:commons-lang3、lombok ## 背景 项目做到尾声,临时需要添加国际化内容,开始只是前台展示的页面添加了国际化支持,后来发现一些后台提示很不友好,遂也选择进行国际化处理。 ## 实现 **Springboot对国际化支持的很好,我们就用自带的i18n实现国际化就行。** 1. application.properties配置文件指定messages位置 ``` spring.messages.basename=i18n.message spring.messages.encoding=UTF-8 ``` 通过查看源码我们可以知道,如果不进行配置,默认读取resource下面messages文件,源码如下: ![springboot默认国际化配置读取位置](https://img-blog.csdnimg.cn/20200511183958366.png) 2. 在resource文件夹下面创建我们配置的basename,目录结构如下图: ![三个配置文件](https://img-blog.csdnimg.cn/20200512093846448.png) **其中 message.properties必须要有,其他的配置文件命名格式为:message_语言_国家.properties**,其中语言和国家格式可以查看 java.util.Locale 类中的说明。 3. 自定义重写 LocaleResolver 类的 resolveLocale 方法,代码如下: ``` public class MyLocaleResolverConfig implements LocaleResolver { private static final String PATH_PARAMETER = "lang"; private static final String PATH_PARAMETER_SPLIT = "_"; @Override public Locale resolveLocale(HttpServletRequest request) { String lang = request.getHeader(PATH_PARAMETER); Locale locale = request.getLocale(); if (!StringUtils.isEmpty(lang)) { String[] split = lang.split(PATH_PARAMETER_SPLIT); locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } } ``` resolveLocale 方法作用就是我们的程序从哪获取指定的语言信息,我这里采用的是从header中获取指定语言,如果没有则采用浏览器 默认的。 还可以从 Session 或 Cookie 中获取。**这里因为我们采取header中获取,所以前端发的请求中 Request Headers 中要有 key 为 lang,value 为 en_US 或 zh_CN 的header。** 4. 将我们自定义的 MyLocaleResolverConfig 作为 Bean 注册进系统中: ``` @Bean public LocaleResolver localeResolver() { return new MyLocale ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
-------------------------------------------------------------------------------- 概述 软件包 类 使用 树 已过时 索引 帮助 上一个类 下一个类 框架 无框架 所有类 摘要: 嵌套 | 字段 | 构造方法 | 方法 详细信息: 字段 | 构造方法 | 方法 -------------------------------------------------------------------------------- com.opensymphony.xwork2 类 ActionSupport java.lang.Object 继承者 com.opensymphony.xwork2.ActionSupport 所有已实现的接口: Action, LocaleProvider, TextProvider, Validateable, ValidationAware, Serializable 直接已知子类: DefaultActionSupport -------------------------------------------------------------------------------- public class ActionSupportextends Objectimplements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, SerializableProvides a default implementation for the most common actions. See the documentation for all the interfaces this class implements for more detailed information. 另请参见: 序列化表格 -------------------------------------------------------------------------------- 字段摘要 从接口 com.opensymphony.xwork2.Action 继承的字段 ERROR, INPUT, LOGIN, NONE, SUCCESS 构造方法摘要 ActionSupport() 方法摘要 void addActionError(String anErrorMessage) Add an Action-level error message to this Action. void addActionMessage(String aMessage) Add an Action-level message to this Action. void addFieldError(String fieldName, String errorMessage) Add an error message for a given field. void clearActionErrors() Clears action errors. void clearErrors() Clears all errors. void clearErrorsAndMessages() Clears all errors and messages. void clearFieldErrors() Clears field errors. void clearMessages() Clears messages. Object clone() String doDefault() String execute() A default implementation that does nothing an returns "success". Collection<String> getActionErrors() Get the Collection of Action-level error messages for this action. Collection<String> getActionMessages() Get the Collection of Action-level messages for this action. Collection<String> getErrorMessages() 已过时。 Use getActionErrors(). Map<String,List<String>> getErrors() 已过时。 Use getFieldErrors(). Map<String,List<String>> getFieldErrors() Get the field specific errors associated with this action. String getFormatted(String key, String expr) Dedicated method to support I10N and conversion errors Locale getLocale() Gets the provided locale. String getText(String aTextName) Gets a message based on a message key, or null if no message is found. String getText(String aTextName, List<?> args) Gets a message based on a key using the supplied args, as defined in MessageFormat, or null if no message is found. String getText(String aTextName, String defaultValue) Gets a message based on a key, or, if the message is not found, a supplied default value is returned. String getText(String key, String[] args) Gets a message based on a key using the supplied args, as defined in MessageFormat, or null if no message is found. String getText(String aTextName, String defaultValue, List<?> args) Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. String getText(String key, String defaultValue, List<?> args, ValueStack stack) Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. String getText(String aTextName, String defaultValue, String obj) Gets a message based on a key using the supplied obj, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. String getText(String key, String defaultValue, String[] args) Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. String getText(String key, String defaultValue, String[] args, ValueStack stack) Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. ResourceBundle getTexts() Get the resource bundle associated with the implementing class (usually an action). ResourceBundle getTexts(String aBundleName) Get the named bundle, such as "com/acme/Foo". boolean hasActionErrors() Check whether there are any Action-level error messages. boolean hasActionMessages() Checks whether there are any Action-level messages. boolean hasErrors() Checks whether there are any action errors or field errors. boolean hasFieldErrors() Check whether there are any field errors associated with this action. boolean hasKey(String key) Checks if a message key exists. String input() void pause(String result) Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return the specified result, such as Action.SUCCESS, Action.INPUT, etc. void setActionErrors(Collection<String> errorMessages) Set the Collection of Action-level String error messages. void setActionMessages(Collection<String> messages) Set the Collection of Action-level String messages (not errors). void setContainer(Container container) void setFieldErrors(Map<String,List<String>> errorMap) Set the field error map of fieldname (String) to Collection of String error messages. void validate() A default implementation that validates nothing. 从类 java.lang.Object 继承的方法 equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 构造方法详细信息 ActionSupport public ActionSupport()方法详细信息 setActionErrors public void setActionErrors(Collection<String> errorMessages)从接口 ValidationAware 复制的描述 Set the Collection of Action-level String error messages. 指定者: 接口 ValidationAware 中的 setActionErrors 参数: errorMessages - Collection of String error messages -------------------------------------------------------------------------------- getActionErrors public Collection<String> getActionErrors()从接口 ValidationAware 复制的描述 Get the Collection of Action-level error messages for this action. Error messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection. 指定者: 接口 ValidationAware 中的 getActionErrors 返回: Collection of String error messages -------------------------------------------------------------------------------- setActionMessages public void setActionMessages(Collection<String> messages)从接口 ValidationAware 复制的描述 Set the Collection of Action-level String messages (not errors). 指定者: 接口 ValidationAware 中的 setActionMessages 参数: messages - Collection of String messages (not errors). -------------------------------------------------------------------------------- getActionMessages public Collection<String> getActionMessages()从接口 ValidationAware 复制的描述 Get the Collection of Action-level messages for this action. Messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection. 指定者: 接口 ValidationAware 中的 getActionMessages 返回: Collection of String messages -------------------------------------------------------------------------------- getErrorMessages @Deprecated public Collection<String> getErrorMessages()已过时。 Use getActionErrors(). -------------------------------------------------------------------------------- getErrors @Deprecated public Map<String,List<String>> getErrors()已过时。 Use getFieldErrors(). -------------------------------------------------------------------------------- setFieldErrors public void setFieldErrors(Map<String,List<String>> errorMap)从接口 ValidationAware 复制的描述 Set the field error map of fieldname (String) to Collection of String error messages. 指定者: 接口 ValidationAware 中的 setFieldErrors 参数: errorMap - field error map -------------------------------------------------------------------------------- getFieldErrors public Map<String,List<String>> getFieldErrors()从接口 ValidationAware 复制的描述 Get the field specific errors associated with this action. Error messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection. 指定者: 接口 ValidationAware 中的 getFieldErrors 返回: Map with errors mapped from fieldname (String) to Collection of String error messages -------------------------------------------------------------------------------- getLocale public Locale getLocale()从接口 LocaleProvider 复制的描述 Gets the provided locale. 指定者: 接口 LocaleProvider 中的 getLocale 返回: the locale. -------------------------------------------------------------------------------- hasKey public boolean hasKey(String key)从接口 TextProvider 复制的描述 Checks if a message key exists. 指定者: 接口 TextProvider 中的 hasKey 参数: key - message key to check for 返回: boolean true if key exists, false otherwise. -------------------------------------------------------------------------------- getText public String getText(String aTextName)从接口 TextProvider 复制的描述 Gets a message based on a message key, or null if no message is found. 指定者: 接口 TextProvider 中的 getText 参数: aTextName - the resource bundle key that is to be searched for 返回: the message as found in the resource bundle, or null if none is found. -------------------------------------------------------------------------------- getText public String getText(String aTextName, String defaultValue)从接口 TextProvider 复制的描述 Gets a message based on a key, or, if the message is not found, a supplied default value is returned. 指定者: 接口 TextProvider 中的 getText 参数: aTextName - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getText public String getText(String aTextName, String defaultValue, String obj)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied obj, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. 指定者: 接口 TextProvider 中的 getText 参数: aTextName - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found obj - obj to be used in a MessageFormat message 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getText public String getText(String aTextName, List<?> args)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or null if no message is found. 指定者: 接口 TextProvider 中的 getText 参数: aTextName - the resource bundle key that is to be searched for args - a list args to be used in a MessageFormat message 返回: the message as found in the resource bundle, or null if none is found. -------------------------------------------------------------------------------- getText public String getText(String key, String[] args)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or null if no message is found. 指定者: 接口 TextProvider 中的 getText 参数: key - the resource bundle key that is to be searched for args - an array args to be used in a MessageFormat message 返回: the message as found in the resource bundle, or null if none is found. -------------------------------------------------------------------------------- getText public String getText(String aTextName, String defaultValue, List<?> args)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. 指定者: 接口 TextProvider 中的 getText 参数: aTextName - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found args - a list args to be used in a MessageFormat message 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getText public String getText(String key, String defaultValue, String[] args)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. 指定者: 接口 TextProvider 中的 getText 参数: key - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found args - an array args to be used in a MessageFormat message 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getText public String getText(String key, String defaultValue, List<?> args, ValueStack stack)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. Instead of using the value stack in the ActionContext this version of the getText() method uses the provided value stack. 指定者: 接口 TextProvider 中的 getText 参数: key - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found args - a list args to be used in a MessageFormat message stack - the value stack to use for finding the text 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getText public String getText(String key, String defaultValue, String[] args, ValueStack stack)从接口 TextProvider 复制的描述 Gets a message based on a key using the supplied args, as defined in MessageFormat, or, if the message is not found, a supplied default value is returned. Instead of using the value stack in the ActionContext this version of the getText() method uses the provided value stack. 指定者: 接口 TextProvider 中的 getText 参数: key - the resource bundle key that is to be searched for defaultValue - the default value which will be returned if no message is found args - an array args to be used in a MessageFormat message stack - the value stack to use for finding the text 返回: the message as found in the resource bundle, or defaultValue if none is found -------------------------------------------------------------------------------- getFormatted public String getFormatted(String key, String expr)Dedicated method to support I10N and conversion errors 参数: key - message which contains formatting string expr - that should be formatted 返回: formatted expr with format specified by key -------------------------------------------------------------------------------- getTexts public ResourceBundle getTexts()从接口 TextProvider 复制的描述 Get the resource bundle associated with the implementing class (usually an action). 指定者: 接口 TextProvider 中的 getTexts 返回: the bundle -------------------------------------------------------------------------------- getTexts public ResourceBundle getTexts(String aBundleName)从接口 TextProvider 复制的描述 Get the named bundle, such as "com/acme/Foo". 指定者: 接口 TextProvider 中的 getTexts 参数: aBundleName - the name of the resource bundle, such as "com/acme/Foo". 返回: the bundle -------------------------------------------------------------------------------- addActionError public void addActionError(String anErrorMessage)从接口 ValidationAware 复制的描述 Add an Action-level error message to this Action. 指定者: 接口 ValidationAware 中的 addActionError 参数: anErrorMessage - the error message -------------------------------------------------------------------------------- addActionMessage public void addActionMessage(String aMessage)从接口 ValidationAware 复制的描述 Add an Action-level message to this Action. 指定者: 接口 ValidationAware 中的 addActionMessage 参数: aMessage - the message -------------------------------------------------------------------------------- addFieldError public void addFieldError(String fieldName, String errorMessage)从接口 ValidationAware 复制的描述 Add an error message for a given field. 指定者: 接口 ValidationAware 中的 addFieldError 参数: fieldName - name of field errorMessage - the error message -------------------------------------------------------------------------------- input public String input() throws Exception抛出: Exception -------------------------------------------------------------------------------- doDefault public String doDefault() throws Exception抛出: Exception -------------------------------------------------------------------------------- execute public String execute() throws ExceptionA default implementation that does nothing an returns "success". Subclasses should override this method to provide their business logic. See also Action.execute(). 指定者: 接口 Action 中的 execute 返回: returns Action.SUCCESS 抛出: Exception - can be thrown by subclasses. -------------------------------------------------------------------------------- hasActionErrors public boolean hasActionErrors()从接口 ValidationAware 复制的描述 Check whether there are any Action-level error messages. 指定者: 接口 ValidationAware 中的 hasActionErrors 返回: true if any Action-level error messages have been registered -------------------------------------------------------------------------------- hasActionMessages public boolean hasActionMessages()从接口 ValidationAware 复制的描述 Checks whether there are any Action-level messages. 指定者: 接口 ValidationAware 中的 hasActionMessages 返回: true if any Action-level messages have been registered -------------------------------------------------------------------------------- hasErrors public boolean hasErrors()从接口 ValidationAware 复制的描述 Checks whether there are any action errors or field errors. Note: that this does not have the same meaning as in WW 1.x. 指定者: 接口 ValidationAware 中的 hasErrors 返回: (hasActionErrors() || hasFieldErrors()) -------------------------------------------------------------------------------- hasFieldErrors public boolean hasFieldErrors()从接口 ValidationAware 复制的描述 Check whether there are any field errors associated with this action. 指定者: 接口 ValidationAware 中的 hasFieldErrors 返回: whether there are any field errors -------------------------------------------------------------------------------- clearFieldErrors public void clearFieldErrors()Clears field errors. Useful for Continuations and other situations where you might want to clear parts of the state on the same action. -------------------------------------------------------------------------------- clearActionErrors public void clearActionErrors()Clears action errors. Useful for Continuations and other situations where you might want to clear parts of the state on the same action. -------------------------------------------------------------------------------- clearMessages public void clearMessages()Clears messages. Useful for Continuations and other situations where you might want to clear parts of the state on the same action. -------------------------------------------------------------------------------- clearErrors public void clearErrors()Clears all errors. Useful for Continuations and other situations where you might want to clear parts of the state on the same action. -------------------------------------------------------------------------------- clearErrorsAndMessages public void clearErrorsAndMessages()Clears all errors and messages. Useful for Continuations and other situations where you might want to clear parts of the state on the same action. -------------------------------------------------------------------------------- validate public void validate()A default implementation that validates nothing. Subclasses should override this method to provide validations. 指定者: 接口 Validateable 中的 validate -------------------------------------------------------------------------------- clone public Object clone() throws CloneNotSupportedException覆盖: 类 Object 中的 clone 抛出: CloneNotSupportedException -------------------------------------------------------------------------------- pause public void pause(String result)Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return the specified result, such as Action.SUCCESS, Action.INPUT, etc. The next time this action is invoked (and using the same continuation ID), the method will resume immediately after where this method was called, with the entire call stack in the execute method restored. Note: this method can only be called within the execute() method. 参数: result - the result to return - the same type of return value in the execute() method. -------------------------------------------------------------------------------- setContainer public void setContainer(Container container) -------------------------------------------------------------------------------- 概述 软件包 类 使用 树 已过时 索引 帮助 上一个类 下一个类 框架 无框架 所有类 摘要: 嵌套 | 字段 | 构造方法 | 方法 详细信息: 字段 | 构造方法 | 方法 --------------------------------------------------------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值