Linux下国际化问题

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

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

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

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

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

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

而以前已经肯定linux环境已经为中文环境,而为何获取到的语言环境又是英文呢。难道是虚拟机的问题?app

因而又查看JVM虚拟机的配置信息。less

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

可是为何页面struts显示没有问题呢,因而又继续检查代码。ide

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.



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

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

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

相关文章
相关标签/搜索