Linux下国际化问题

最近项目部署到Linux环境下,出现部分国际化资源文件内容展示为英文问题,出现该问题后感到非常奇怪。

之前项目也有部署到Linux环境下的情况,都并未出现该问题,仔细检查Linux环境变量也没问题。

153832_Psxq_269725.png

起初以为Spring获取Locale信息,应该是根据请求头来进行判断,所以又检查浏览器请求信息同时设置默认信息

154236_c3Fz_269725.png

后面请求头发现请求也没问题,这种时候只能对源码进行查看了。

仔细检查代码后发现出现英文乱码的地方都是使用Spring的上下文进行资源文件获取。

155235_uLlC_269725.png

而Spring对于locale获取的时候,如果用户未传递该值则是默认Java 虚拟机实例的当前默认语言环境值。

而之前已经确定linux环境已经为中文环境,而为什么获取到的语言环境又是英文呢。难道是虚拟机的问题?

于是又查看JVM虚拟机的配置信息。

160251_nok0_269725.png

通过jps命令可以清楚看到当前jvm虚拟机的详细的信息,同时也发现了确实是虚拟机出现的问题。

但是为什么页面struts显示没有问题呢,于是又继续检查代码。

public Locale getLocale() {
    ActionContext ctx = ActionContext.getContext();
    if (ctx != null) {
        return ctx.getLocale();
    } else {
        if (LOG.isDebugEnabled()) {
       LOG.debug("Action context not initialized");
        }
        return null;
    }
}

 

struts通过ActionContext进行获取,而ActionContext的信息又是来自于哪里呢?

public static ActionContext getActionContext(HttpServletRequest req) {
    ValueStack vs = getValueStack(req);
    return vs != null?new ActionContext(vs.getContext()):null;
}

 struts 的locale信息来自于request的请求中,所以该字段肯定不会为空,而访问的请求肯定是中文环境,所以页面显示没有问题,而Spring 由于没有传递locale对象,则直接获取虚拟机的语言环境,所以出现了部分信息出现乱码问题。

既然找到了问题就好处理,针对JVM虚拟机进行默认语言环境设置即可。

修改locale oralce的介绍

The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the Locale.getDefault() method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.languageuser.country, and user.variant system properties.

Third, your application can call the Locale.setDefault(Locale) method. The setDefault(Locale aLocale) method lets your application set a systemwide (actually VM-wide) resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.



161914_EVIT_269725.png

修改catalina.sh ,添加默认语言环境设置,保存重启,问题解决!

但是为什么系统已经是中文环境,jvm获取到的数据还是英文的环境呢,初次加载的时候系统环境语言为英文,后续修改jvm后但没有手工去更改语言环境,导致还是获取的第一次加载的环境。

之后咨询售后的同事,发现现场确实是这样,之后再在中文环境下安装的程序就未出现该问题。

转载于:https://my.oschina.net/luckyi/blog/546851

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值