码迷,mamicode.com
首页 > 其他好文 > 详细

国际化

时间:2019-02-23 01:02:18      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:ase   TBase   jekyll   utf-8   base   文件   pass   ISE   hold   

1)、编写国际化配置文件

技术图片

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=\u767B\u9646
login.password=\u5BC6\u7801
login.remember=\u8BB0\u4F4F\u6211
login.tip=\u8BF7\u767B\u5F55
login.username=\u7528\u6237\u540D

login.properties

login.btn=\u767B\u9646
login.password=\u5BC6\u7801
login.remember=\u8BB0\u4F4F\u6211
login.tip=\u8BF7\u767B\u5F55
login.username=\u7528\u6237\u540D

2)使用ResourceBundleMessageSource管理国际化资源文件

    MessageSourceAutoConfiguration   

       @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (StringUtils.hasText(this.basename)) {
            messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
                    StringUtils.trimAllWhitespace(this.basename)));
        }
        if (this.encoding != null) {
            messageSource.setDefaultEncoding(this.encoding.name());
        }
        messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
        messageSource.setCacheSeconds(this.cacheSeconds);
        messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
        return messageSource;
    }        

配置basename

spring.messages.basename=il8n.login

3) Thymeleaf使用Message Expressions: #{...}:获取国际化内容

比如登录页面实现国际化

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta 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="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Jekyll v3.8.5">
    <title>Signin Template · Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="/docs/4.2/dist/css/bootstrap.min.css" rel="stylesheet" th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.min.css}" crossorigin="anonymous">
    

    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
    <!-- Custom styles for this template -->
    <link th:href="@{assert/css/signin.css}" rel="stylesheet">
  </head>
  <body class="text-center">
    <form class="form-signin" th:action="@{/toLogin}">
  <img class="mb-4" th:src="@{/assert/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>
  <label class="sr-only" th:text="#{login.username}">Username</label>
  <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required autofocus>
  <label for="inputPassword" class="sr-only" th:text="#{login.password}">Password</label>
  <input type="password" name="password" id="inputPassword" class="form-control" 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">&copy; 2017-2018</p>
</form>
</body>
</html>

4)springBoot 国际化默认AcceptHeaderLocaleResolver

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
        public LocaleResolver localeResolver() {
            if (this.mvcProperties
                    .getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
                return new FixedLocaleResolver(this.mvcProperties.getLocale());
            }
            AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
            localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
            return localeResolver;
        }

通过浏览器的语言设置来显示页面语言

AcceptHeaderLocaleResolver
@Override
public Locale resolveLocale(HttpServletRequest request) { Locale defaultLocale = getDefaultLocale(); if (defaultLocale != null && request.getHeader("Accept-Language") == null) { return defaultLocale; } Locale requestLocale = request.getLocale(); List<Locale> supportedLocales = getSupportedLocales(); if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) { return requestLocale; } Locale supportedLocale = findSupportedLocale(request, supportedLocales); if (supportedLocale != null) { return supportedLocale; } return (defaultLocale != null ? defaultLocale : requestLocale); }
DispatcherServlet处理国际化
DispatcherServlet
protected
void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine locale for request and apply it to the response. Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale);

 

国际化

标签:ase   TBase   jekyll   utf-8   base   文件   pass   ISE   hold   

原文地址:https://www.cnblogs.com/xiaoliangup/p/10421203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!