建立多語言網(wǎng)站,可以獲得更多用戶流量。多語言網(wǎng)站被稱為國際化(i18n),與本地化相反。
你比如京東、淘寶這類電商網(wǎng)站就是國際化的電商網(wǎng)站,它支持多國語言。
注意: 國際化是一個由18個字符組成的單詞,第一個字符為i,最后一個字符為n,因此通常縮寫為 i18n。
Spring通過為不同的語言環(huán)境使用Spring攔截器,語言環(huán)境解析器和資源包,為國際化(i18n)提供了廣泛的支持。在本文中,我將知道您使用Spring Boot構(gòu)建了一個簡單的多語言網(wǎng)站。
你可以預(yù)覽一下示例:
在示例中,語言環(huán)境信息位于URL的參數(shù)上。語言環(huán)境信息將存儲在Cookie中,并且用戶不會在接下來的頁面中重新選擇語言。
- http://localhost:8080/SomeContextPath/login1?lang=zh
- http://localhost:8080/SomeContextPath/login1?lang=en
URL上的語言環(huán)境信息的另一個示例:
- http://localhost:8080/SomeContextPath/zh/login2
- http://localhost:8080/SomeContextPath/en/login2
12.1 創(chuàng)建Spring Boot項(xiàng)目
我在這里為以下語言創(chuàng)建2個屬性文件:中文、英語。
i18n / messages_zh.properties
label.password=密碼
label.submit=登陸label.title= 登陸頁面label.userName=用戶名
如果你的eclipse中顯示如下編碼形式:
label.password=\u5BC6\u7801
label.submit=\u767B\u9646label.title= \u767B\u9646\u9875\u9762label.userName=\u7528\u6237\u540D
修改首選項(xiàng)中的content Types下的:
第二步配置.proerties文件右鍵屬性配置,設(shè)置其字符編碼
i18n / messages_en.properties
label.password= Password
label.submit= Loginlabel.title= Login Pagelabel.userName= User Name
Eclipse支持使用消息編輯器來編輯文件的信息。
12.2 攔截器和LocaleResolver
您需要聲明2個Spring bean,包括localeResolver和messageResource。
LocaleResolver指定如何獲取用戶將使用的語言環(huán)境信息。CookieLocaleResolver將從Cookie讀取語言環(huán)境信息,以查找用戶以前使用的語言。
MessageResource 將加載屬性文件內(nèi)容
在Controller處理請求之前,它必須經(jīng)過攔截器,您需要在該攔截器中注冊LocaleChangeInterceptor,攔截器處理用戶的語言環(huán)境更改。
WebMvcConfig.JAVA
package me.laocat.i18n.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean(name = “localeResolver”)
public LocaleResolver getLocaleResolver() {
// Cookie本地化解析器
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieDomain(“myAppLocaleCookie”);
// 60 分鐘
resolver.setCookieMaxAge(60 * 60);
return resolver;
}
@Bean(name = “messageSource”)
public MessageSource getMessageResource() {
// 可重新加載的資源包消息源
ReloadableResourceBundleMessageSource messageResource = new ReloadableResourceBundleMessageSource();
// 讀 i18n/messages_xxx.properties file.
// 例如: i18n/messages_en.properties
messageResource.setBasename(“classpath:i18n/messages”);
messageResource.setDefaultEncoding(“UTF-8”);
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 本地化修改攔截器
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName(“lang”);
registry.addInterceptor(localeInterceptor).addPathPatterns(“/*”);
}
}
12.3 控制器和視圖
I18nController.java(在參數(shù)上設(shè)置本地化)
package me.laocat.i18n;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class I18nController {
@RequestMapping(value = { “/”, “/login1” })
public String staticResource(Model model) {
return “login1”;
}}
login1.html (Thymeleaf 視圖)
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:utext="#{label.title}"></title>
</head>
<body>
<div style="text-align: right;padding:5px;margin:5px 0px;background:#ccc;">
<a th:href="@{/login1?lang=en}">Login (En)</a>
| <a th:href="@{/login1?lang=zh}">Login (Zh)</a>
</div>
<form method="post" action="">
<table>
<tr>
<td>
<strong th:utext="#{label.userName}"></strong>
</td>
<td><input name="userName" /></td>
</tr>
<tr>
<td>
<strong th:utext="#{label.password}"></strong>
</td>
<td><input name="password" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" th:value="#{label.submit}" />
</td>
</tr>
</table>
</form>
</body>
</html>
12.4 URL上的語言環(huán)境信息
如果您要構(gòu)建一個多語言網(wǎng)站,其語言環(huán)境信息位于URL上。您需要更改一些配置:
- http://localhost:8080/SomeContextPath/en/login2
- http://localhost:8080/SomeContextPath/zh/login2
創(chuàng)建2個類UrlLocaleInterceptor和UrlLocaleResolver
UrlLocaleInterceptor.java
package me.laocat.i18n.interceptors;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;
public class UrlLocaleInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);if (localeResolver == null) {
throw new IllegalStateException(“找不到LocaleResolver:不在DispatcherServlet請求中??”);
}// 從LocaleResolver獲取區(qū)域設(shè)置
Locale locale = localeResolver.resolveLocale(request);
localeResolver.setLocale(request, response, locale);
return true;
}
}
UrlLocaleResolver.java
package me.laocat.i18n.resolver;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
@Configuration
public class UrlLocaleResolver implements LocaleResolver {
private static final String URL_LOCALE_ATTRIBUTE_NAME = “URL_LOCALE_ATTRIBUTE_NAME”;
@Bean(name = “messageSource”)
@Override
public Locale resolveLocale(HttpServletRequest request) {
// ==> /SomeContextPath/en/…
// ==> /SomeContextPath/zh/…
// ==> /SomeContextPath/WEB-INF/pages/…
String uri = request.getRequestURI();
System.out.println(“URI=” + uri);
String prefixEn = request.getServletContext().getContextPath() + “/en/”;
String prefixZh = request.getServletContext().getContextPath() + “/zh/”;
Locale locale = null;
// English
if (uri.startsWith(prefixEn)) {
locale = Locale.ENGLISH;
}
// China
else if (uri.startsWith(prefixZh)) {
locale = new Locale(“zh”, “CN”);
}
if (locale != null) {
request.getSession().setAttribute(URL_LOCALE_ATTRIBUTE_NAME, locale);
}
if (locale == null) {
locale = (Locale) request.getSession().getAttribute(URL_LOCALE_ATTRIBUTE_NAME);
if (locale == null) {
locale = Locale.ENGLISH;
}
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
// Nothing
}
}
重新更改WebMvcConfig中的Interceptor配置 :
WebMvcConfig.java
package me.laocat.i18n.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import me.laocat.i18n.interceptors.UrlLocaleInterceptor;
import me.laocat.i18n.resolver.UrlLocaleResolver;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean(name = “localeResolver”)
public LocaleResolver getLocaleResolver() {
// Cookie本地化解析器
// CookieLocaleResolver resolver = new CookieLocaleResolver();
// resolver.setCookieDomain(“myAppLocaleCookie”);
// 60 分鐘
// resolver.setCookieMaxAge(60 * 60);
LocaleResolver resolver = new UrlLocaleResolver();
return resolver;
}
@Bean(name = “messageSource”)
public MessageSource getMessageResource() {
// 可重新加載的資源包消息源
ReloadableResourceBundleMessageSource messageResource = new ReloadableResourceBundleMessageSource();
// 讀 i18n/messages_xxx.properties file.
// 例如: i18n/messages_en.properties
messageResource.setBasename(“classpath:i18n/messages”);
messageResource.setDefaultEncoding(“UTF-8”);
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 本地化修改攔截器
// LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
// localeInterceptor.setParamName(“lang”);
// registry.addInterceptor(localeInterceptor).addPathPatterns(“/*”);
UrlLocaleInterceptor localeInterceptor = new UrlLocaleInterceptor();
registry.addInterceptor(localeInterceptor).addPathPatterns(“/en/*”, “/zh/*”);
}
}
控制器:
I18nController2.java(URL上的語言環(huán)境)
package me.laocat.i18n.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class I18nController2 {
@RequestMapping(value = “/{locale:en|zh}/login2”)
public String login2(Model model) {
return “login2”;
}}
login2.html (Thymeleaf 視圖)
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:utext="#{label.title}"></title>
</head>
<body>
<div style="text-align: right;padding:5px;margin:5px 0px;background:#ccc;">
<a th:href="@{/en/login2}">Login (English)</a>
| <a th:href="@{/zh/login2}">Login (China)</a>
</div>
<form method="post" action="">
<table>
<tr>
<td>
<strong th:utext="#{label.userName}"></strong>
</td>
<td><input name="userName" /></td>
</tr>
<tr>
<td>
<strong th:utext="#{label.password}"></strong>
</td>
<td><input name="password" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" th:value="#{label.submit}" />
</td>
</tr>
</table>
</form>
</body>
</html>
運(yùn)行應(yīng)用程序:
http://localhost:8080/zh/login2
12.5 多語言網(wǎng)站,內(nèi)容存儲在數(shù)據(jù)庫中
上面的多語言網(wǎng)站示例無法滿足您的需求。您需要具有多種語言的新聞網(wǎng)站,并且其內(nèi)容存儲在數(shù)據(jù)庫中。您可以使用多個數(shù)據(jù)源的解決方案,其中每個數(shù)據(jù)源都是一個包含語言內(nèi)容的數(shù)據(jù)庫。