SpringBoot實現(xiàn)前后端分離國際化的示例詳解
前言
Springboot國際化可以幫助使用者在不同語言環(huán)境中構(gòu)建應用程序,這樣應用程序可以有效地適應不同語言文化背景下的用戶需求。
此外,Springboot國際化也可以方便多語言應用程序重用和維護,從而減少了系統(tǒng)部署的時間成本和維護的費用。
要實現(xiàn)Springboot國際化應用,主要有三個步驟。
1、設置國際化屬性文件
定義國際化資源文件,使用properties格式的文件,將不同的多國語言文本資源放在不同的文件中,每個文件的命名采用【locale】+【messages】的方式,如zh_CN.properties、en_US.properties等。
message.properties文件內(nèi)容可為空。
message.en_US.properties內(nèi)容示例:
40001=Hello
message.zh_CN.properties內(nèi)容示例:
40001=你好
2、創(chuàng)建解析器和攔截器
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import java.util.Locale; @Configuration public class LocaleConfig { @Bean public SessionLocaleResolver localeResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); localeResolver.setDefaultLocale(Locale.CHINA); return localeResolver; } @Bean public WebMvcConfigurer localeInterceptor() { return new WebMvcConfigurer() { @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("lang"); registry.addInterceptor(localeInterceptor); } }; } }
3、啟動配置文件設置
application.properties中添加如下內(nèi)容
#i18n spring.messages.basename=i18n.messages spring.messages.cache-duration=3600 spring.messages.encoding=UTF-8
application.yml中添加如下內(nèi)容
spring: messages: basename: i18n/messages
4、控制器示例
Springboot的國際化是通過一個稱為“messageSource”的bean實現(xiàn)的。
import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestControler { @Autowired private MessageSource messageSource; @GetMapping("/hello") public Map<Object, Object> test() { Map<Object, Object> result = new HashMap<Object, Object>(); result.put("code", 40001); result.put("msg", messageSource.getMessage("40001", null, LocaleContextHolder.getLocale())); return result; } }
5、小結(jié)
Springboot國際化可以幫助使用者在不同語言環(huán)境中構(gòu)建應用程序,這樣應用程序可以有效地適應不同語言文化背景下的用戶需求。
此外,Springboot國際化也可以方便多語言應用程序重用和維護,從而減少了系統(tǒng)部署的時間成本和維護的費用。要實現(xiàn)Springboot國際化應用,主要有三個步驟。
1.設置國際化屬性文件:要實現(xiàn)Springboot國際化,首先要準備一系列國際化屬性文件,包括語言和地區(qū)信息。
2.注冊國際化消息資源:將屬性文件中的語言和地區(qū)信息注冊到Springboot應用程序中,并指定默認值,以便在擴展多語言時可以快速、高效地訪問相關(guān)屬性資源。
3.定義多語言捆綁文件:將已定義的國際化屬性文件與應用程序結(jié)合起來,形成多語言捆綁文件,以便在用戶選擇不同語言時可以及時調(diào)整應用程序語言版本。
到此這篇關(guān)于SpringBoot實現(xiàn)前后端分離國際化的示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot國際化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中的注解之@Override和@Autowired
看別人寫的代碼,經(jīng)常會用到 @Override 和 @Autowired 這兩個注解.這邊總結(jié)一下這兩個注解的作用,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05Java實現(xiàn)數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)數(shù)組的方法分析
這篇文章主要介紹了Java實現(xiàn)數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)數(shù)組的方法,結(jié)合實例形式分析了Java字符串及數(shù)組相關(guān)的分割、遍歷、追加等操作技巧,需要的朋友可以參考下2018-06-06Spring @Async 的使用與實現(xiàn)的示例代碼
本篇文章主要介紹了Spring @Async 的使用與實現(xiàn)的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn)代碼
這篇文章主要介紹了Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11