国际化
Posted xiaoliangup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了国际化相关的知识,希望对你有一定的参考价值。
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=u767Bu9646 login.password=u5BC6u7801 login.remember=u8BB0u4F4Fu6211 login.tip=u8BF7u767Bu5F55 login.username=u7528u6237u540D
login.properties
login.btn=u767Bu9646 login.password=u5BC6u7801 login.remember=u8BB0u4F4Fu6211 login.tip=u8BF7u767Bu5F55 login.username=u7528u6237u540D
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">© 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);
以上是关于国际化的主要内容,如果未能解决你的问题,请参考以下文章