七、SpringBoot 国际化

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/PZHU_CG_CSDN/article/details/81915592

1、编写国际化文件

(1)、在 resource 文件夹下创建名为 i18n 的文件夹

(2)、在 i18n 文件夹下创建国际化文件,创建好文件后,应当是下面这样的视图

(3)、国际化文件内容

login.properties:


    login.btn=登录
    login.password=密码
    login.remember=记住我
    login.tip=请登录~
    login.username=用户名

login_en_US.properties:


    login.btn=Sign in
    login.password=Password
    login.remember=remember-me
    login.tip=Please sign in
    login.username=UserName

login_zh_CN.properties:


    login.btn=登录
    login.password=密码
    login.remember=记住我
    login.tip=请登录
    login.username=用户名

如果在 properties 文件中不能显示中文,设置如下:

2、SpringBoot 中配置国际化组件

(1)、SpringBoot 自动配置的国际化组件


    @Configuration
    @ConditionalOnMissingBean(
        value = {MessageSource.class},
        search = SearchStrategy.CURRENT
    )
    @AutoConfigureOrder(-2147483648)
    @Conditional({MessageSourceAutoConfiguration.ResourceBundleCondition.class})
    @EnableConfigurationProperties
    public class MessageSourceAutoConfiguration {
        private static final Resource[] NO_RESOURCES = new Resource[0];

        public MessageSourceAutoConfiguration() {
        }

        //默认的国际化配置文件的基名为 messages,所以我们可以将我们的国际化配置文件命名为 messages.properties
        @Bean
        @ConfigurationProperties(
            prefix = "spring.messages"
        )
        public MessageSourceProperties messageSourceProperties() {
            return new MessageSourceProperties();
        }

        @Bean
        public MessageSource messageSource() {
            MessageSourceProperties properties = this.messageSourceProperties();
            ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
            if (StringUtils.hasText(properties.getBasename())) {
                //设置国际化资源文件的基础名(去掉语言国家代码的) 
                messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
            }

            if (properties.getEncoding() != null) {
                messageSource.setDefaultEncoding(properties.getEncoding().name());
            }

            messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
            Duration cacheDuration = properties.getCacheDuration();
            if (cacheDuration != null) {
                messageSource.setCacheMillis(cacheDuration.toMillis());
            }

            messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
            messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
            return messageSource;
        }

springboot 在处理国际化时,需要 LocaleResolver ,在 springboot 自动配置中可以看到如下代码:


    //向容器中加入了 LocaleResolver 对象
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
        prefix = "spring.mvc",
        name = {"locale"}
    )
    public LocaleResolver localeResolver() {
        if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
            return new FixedLocaleResolver(this.mvcProperties.getLocale());
        } else {
            AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
            localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
            return localeResolver;
        }
    }

  所以我们将自己的 LocaleResolver 对象添加到 IOC 容器中,就会让springboot 自动配置的 LocaleResolver 失效。

自定义 LocaleResolver 类:


    public class MyLocaleResolver implements LocaleResolver {

        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            //在 url 链接上带有 locale 参数,所以在这儿获取参数,使用指定地区语言
            String localName = request.getParameter("l");
            Locale locale = Locale.getDefault();
            if(!StringUtils.isEmpty(localName)){
                String[] split = localName.split("_");
                locale = new Locale(split[0], split[1]);

            }
            return locale;
        }

        @Override
        public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

        }
    }

然后将自定义的 LocaleResolver 添加到 IOC 容器中:


    //在自定义的 config 文件中加入
    @Bean
    public LocaleResolver localeResolver(){
        //向容器中添加自定义的 LocaleResolver 对象        
        return new MyLocaleResolver();
    }

  经过以上配置后,还不能使用我们自己配置的国际化文件,因为在前面我们知道,springboot 默认用的是 messages 基名的国际化文件,所以在这儿我们还需要在 application.xml 配置文件中添加我们国际化文件的基名。


    #设置国际化资源的基础名
    spring.messages.basename=i18n.login

其中 i18n 是国际化配置文件的文件夹名,login 是国际化文件的前缀名。

(2)、在页面上获国际化的值

  页面上的文本值以键值的形式从国际化配置文件中获取。


    <!DOCTYPE html>
    <html lang="en"  xmlns:th="http://www.thymeleaf.org">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <meta name="description" content="">
            <meta name="author" content="">
            <title>Signin Template for Bootstrap</title>
            <!-- Bootstrap core CSS -->
            <link href="/asserts/css/bootstrap.min.css" rel="stylesheet" th:href="@{/asserts/css/bootstrap.min.css}">
            <!-- Custom styles for this template -->
            <link href="/asserts/css/signin.css" rel="stylesheet" th:href="@{/asserts/css/signin.css}">
        </head>
        <body class="text-center">
            <form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
                <img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="/asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
                <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>

                <!--判断-->
                <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

                //从国际化文件中获取 login.username 
                <label class="sr-only" th:text="#{login.username}">Username</label>
                <input type="text" name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
                <label class="sr-only" th:text="#{login.password}">Password</label>
                <input type="password" name="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
                <div class="checkbox mb-3">
                    <label>
                        <input type="checkbox" value="remember-me"/> [[#{login.remember}]]
                    </label>
                </div>
                <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>

                <p class="mt-5 mb-3 text-muted">© 2017-2018</p>

                //切换,将语言、国家携带在 url 连接上,我这里请求 /index 会转发到当前页面
                <a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
                <a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>
            </form>
        </body>

    </html>

效果:

总结:

  1. 编写国家化配置文件。
  2. 配置国际化解析器 LocaleResolver。
  3. 页面上通过键值对获取国际化值。

猜你喜欢

转载自blog.csdn.net/PZHU_CG_CSDN/article/details/81915592