一、開(kāi)發(fā)背景
在當(dāng)今全球化的環(huán)境下,為了更好地滿足用戶的多語(yǔ)言需求,越來(lái)越多的應(yīng)用程序需要支持國(guó)際化多語(yǔ)言配置。Spring Boot作為一種快速開(kāi)發(fā)框架,提供了方便的國(guó)際化支持,使得應(yīng)用程序可以輕松地適應(yīng)不同的語(yǔ)言環(huán)境。通過(guò)集成Spring Boot的國(guó)際化多語(yǔ)言配置,應(yīng)用程序可以根據(jù)用戶的語(yǔ)言環(huán)境自動(dòng)選擇相應(yīng)的語(yǔ)言資源,從而實(shí)現(xiàn)更好的用戶體驗(yàn)。這種配置方式不僅可以適應(yīng)不同語(yǔ)言環(huán)境,還可以為應(yīng)用程序提供更好的擴(kuò)展性和可維護(hù)性,是現(xiàn)代應(yīng)用程序開(kāi)發(fā)的必備技能之一。
二、多語(yǔ)言轉(zhuǎn)換工具類
1、LocaleMessageUtil(根據(jù)語(yǔ)言編碼查詢內(nèi)容)
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class LocaleMessageUtil {
@Resource
private MessageSource messageSource;
public LocaleMessageUtil() {
}
public String getMessage(String code) {
return this.getConnectorMessage(code, null, null);
}
public String getMessage(String code, String param) {
return this.getConnectorMessage(code, param, null);
}
public String getMessage(String[] codes) {
return this.getConnectorMessage(null, null, codes);
}
private String getConnectorMessage(String code, String param, String[] codes) {
StringBuilder stringBuilder = new StringBuilder();
if (StringUtils.isNotEmpty(param)) {
stringBuilder.append(param).append(":");
} else if (StringUtils.isNotEmpty(code)) {
stringBuilder.append(this.messageSource.getMessage(code, null, LocaleContextHolder.getLocale()));
}
if (null != codes) {
for (String arr : codes) {
stringBuilder.append(this.messageSource.getMessage(arr, null, LocaleContextHolder.getLocale())).append(";");
}
}
return stringBuilder.toString();
}
}
2、MessageLocaleResolver(國(guó)際化解析)
根據(jù)請(qǐng)求頭中攜帶的請(qǐng)求語(yǔ)言內(nèi)容,生成會(huì)話,解析內(nèi)容。
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
/**
* 國(guó)際化解析
*/
public class MessageLocaleResolver implements LocaleResolver {
private static final String LANG = "lang";
private static final String LANG_SESSION = "lang_session";
@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale locale;
String language = request.getHeader(LANG);
//中文language=zh_CN
if (StringUtils.isNotEmpty(language)) {
locale = new Locale(language.toLowerCase());
} else {
locale = new Locale("zh_cn");
}
HttpSession session = request.getSession();
session.setAttribute(LANG_SESSION, locale);
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
三、Spring Web Servlet注入多語(yǔ)言插件轉(zhuǎn)換Bean
WebMvcConfig
import com.zfsw.mall.common.utils.lang.MessageLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
return new MessageLocaleResolver();
}
}
四、配置文件修改
application.yml
spring:
messages:
basename: i18n.messages
encoding: utf-8
五、多語(yǔ)言翻譯配置
1、resource目錄下,新建i18n目錄
使用idea右鍵在i18n目錄下,新建四個(gè)配置文件,分別為
- messages.properties:默認(rèn)語(yǔ)言記錄,請(qǐng)求頭中不傳“l(fā)ang”取值
- messages_en_us.properties:英文翻譯
- messages_zh_cn.properties:中文簡(jiǎn)體翻譯
- messages_zh_hant.properties:中文繁體翻譯
如圖:
當(dāng)然了,如果以后需要其他語(yǔ)言,也可以繼續(xù)添加,比如韓語(yǔ)、日語(yǔ)、法語(yǔ)等。
2、配置文件內(nèi)容
配置文件的內(nèi)容可以理解為一個(gè)key-value數(shù)據(jù)類型,key是自己系統(tǒng)內(nèi)定語(yǔ)言編碼,在解析器工具類中使用,value是翻譯后的內(nèi)容。如果想翻譯幾個(gè)就在幾個(gè)配置文件里加。例如:
- messages.properties
weather.is.nice=今天天氣不錯(cuò)
- messages_en_us.properties
weather.is.nice=The weather is nice today
- messages_zh_cn.properties
weather.is.nice=今天天氣不錯(cuò)
- messages_zh_hant.properties
weather.is.nice=今天天氣不錯(cuò)
六、測(cè)試效果
寫一個(gè)控制器,驗(yàn)證一下
TestLangController
import com.zfsw.mall.common.utils.lang.LocaleMessageUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api/user")
@ApiOperation(value = "多語(yǔ)言測(cè)試")
public class TestLangController {
@Resource
private LocaleMessageUtil messageUtil;
@GetMapping("/lang/test")
@ApiOperation(value = "多語(yǔ)言測(cè)試")
public ResponseEntity<String> langTest() {
//語(yǔ)言編碼
String langCode = "weather.is.nice";
//翻譯內(nèi)容
String message = messageUtil.getMessage(langCode);
return ResponseEntity.ok().body(message);
}
}
啟動(dòng)項(xiàng)目,試一下
-
請(qǐng)求頭不加lang參數(shù)
-
英文翻譯
-
中文簡(jiǎn)體翻譯
-
中文繁體翻譯
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-731975.html
七、前端
前端增加切換語(yǔ)言按鈕,中英文切換后,請(qǐng)求頭發(fā)生變動(dòng)即可!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-731975.html
config.headers["lang"] = store.getters.local || "zh-hant";
到了這里,關(guān)于SpringBoot集成國(guó)際化多語(yǔ)言配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!