国际化i18n–messages文本替换

国际化,通常被称之为i18n,指的是将应用程序设计为可以翻译成各种语言的过程.这通常会涉及将文本放到属性bundle中,并且要目标地域作为后缀,例如messages_en.properties,messages_cn.properties.
属性bundle的解析过程是首先尝试最为具体的地域,如果无法找到的话,将会依次使用备用的非具体地域.
首先建立3个文件

messages.properties
NotEmpty.profileForm.name=default(name)
NotEmpty.profileForm.email=defaule(email)
messages_en_US.properties
NotEmpty.profileForm.name=enUS(name)
NotEmpty.profileForm.email=enUS(email)
messages_zh_CN.properties
NotEmpty.profileForm.name=CN(NAME)
NotEmpty.profileForm.email=CN(EMAIL)
我们可以通过一个属性来配置应用程序的地域,而且一旦定义之后,就不能修改了,要配置我们应用的地域,只需添加如下属性到application.propertes中:

spring.mvc.locale=en_US
spring.mvc.locale-resolver=FIXED
其中locale-resolver为枚举 支持的两种设置如下:

public enum LocaleResolver {
        /**
         * Always use the configured locale.
         */
        FIXED,
        /**
         * Use the "Accept-Language" header or the configured locale if the header is not
         * set.
         */
        ACCEPT_HEADER
    }
设置为en_US时:
设置为zh_CN时: