一、Spring Boot國際化配置
Spring Boot使用MessageSource類實現國際化,我們可以在application.properties或application.yml文件中配置如下信息:
spring.messages.basename=i18n/message spring.messages.fallback-to-system-locale=true spring.messages.cache-duration=-1 #i18n/message.properties設置英文語言 #i18n/message_zh.properties設置中文語言
其中spring.messages.basename指定基礎消息文件的路徑,我們需要在i18n目錄下創建message.properties和message_zh.properties。fallback-to-system-locale設置當沒有匹配到語言時是否使用系統語言,默認為true。cache-duration設置消息緩存的時間,默認為-1,即永久緩存。
二、Spring Boot國際化默認為英文
在國際化配置中,如果不設置fallbackLocale,則默認使用英文作為基礎語言,當沒有匹配到其他語言時就使用英文。我們可以創建一個i18n/message.properties文件,並添加如下信息:
name=Tom age=21 gender=Male
當我們訪問http://localhost:8080/hello時,可以看到頁面打印出了以上信息。
三、Spring Boot國際化默認語言
我們可以通過設置Locale.getDefault()方法的返回值來設置默認語言。在Spring Boot中,我們可以通過配置文件進行設置,如下:
spring.mvc.locale=zh_CN
此時訪問http://localhost:8080/hello,頁面打印出的信息就會變成中文。
四、Spring Boot國際化亂碼
在配置文件中指定了中文編碼為UTF-8,但在頁面上顯示的卻是亂碼,這是因為我們沒有在message_zh.properties文件中指定編碼格式,我們可以在該文件中添加如下信息:
name=\u59D3\u540D age=\u5E74\u9F84 gender=\u6027\u522B
其中,\u59D3\u540D是“姓名”的Unicode編碼,\u5E74\u9F84是“年齡”的Unicode編碼,\u6027\u522B是“性別”的Unicode編碼。我們需要將所有的中文字符轉換成Unicode編碼,然後再放入配置文件中。
五、Spring Boot國際化原理
在Spring Boot中,國際化實現的主要原理是在MessageSource和LocaleResolver等類的幫助下,通過讀取配置文件來獲取相應語言環境的資源文件。
· MessageSource:提供了國際化資源文件的支持,可以通過getMessage()方法獲取國際化資源。
· ResourceBundleMessageSource:默認的MessageSource實現,使用了Java標準庫中的ResourceBundle類來讀取資源文件。
· LocaleResolver:提供了根據請求頭部信息來解析出語言環境的支持。
· AcceptHeaderLocaleResolver:默認的LocaleResolver實現,使用請求頭部信息中的“Accept-Language”字段來判斷客戶端語言偏好。
六、Spring Boot國際化多個base
我們也可以配置多個基礎消息文件的路徑,以逗號分隔,如下:
spring.messages.basename=i18n/message,i18n/message_custom
此時,在i18n目錄下需要新建message_custom.properties和message_custom_zh.properties文件,用於存放自定義消息信息。當匹配不到國際化信息時,會按照默認順序依次查找message和message_custom文件。
七、Spring Boot國際化動態加載
在配置文件中,我們不僅可以指定國際化資源文件的路徑,還可以指定國際化資源文件的實時修改。
在application.yml中,我們可以設置如下參數:
spring.messages.always-use-message-format=true spring.messages.basename=classpath*:i18n/message spring.messages.cache-duration=-1 spring.messages.encoding=UTF-8 spring.messages.fallback-to-system-locale=true spring.messages.use-code-as-default-message=true
always-use-message-format參數指定是否開啟MessageFormat的解析。而basename目前指向類路徑下的i18n/message文件。Spring Boot支持使用classpath*:前綴來掃描類路徑下的多個資源文件,通配符*會匹配所有的文件名。
我們可以在代碼中動態改變Locale信息,並通過MessageSource.getMessage()方法返回對應消息,代碼如下:
@Autowired private MessageSource messageSource; public String getMessage(String code, Object[] args, Locale locale) { return messageSource.getMessage(code, args, locale); }
八、Spring Boot國際化json
在前後端分離的場景下,我們可以將國際化信息以json格式返回給前端,讓前端自行解析國際化信息。我們可以使用下面的代碼返回國際化信息的json格式:
@RequestMapping(value = "/getMessageByJson", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public String getMessageByJson(@RequestParam String code, @RequestParam(required = false) String locale) { Locale l = StringUtils.hasText(locale) ? LocaleUtils.toLocale(locale) : LocaleContextHolder.getLocale(); Map map = new HashMap(); map.put(code, messageSource.getMessage(code, null, l)); return JSONObject.toJSONString(map); }
九、Spring Boot國際化配置中英文切換
我們可以通過在配置文件中指定Profile的方式來切換國際化信息。
在application.properties文件中添加如下信息:
spring.profiles.active=zh_CN
當我們需要切換到英文,只需要將application.properties文件名修改為application_en.properties,並設置spring.profiles.active=dev。
十、Spring Boot國際化一開始就變中文
在國際化配置中,我們可以在啟動類中強制設置默認語言環境為中文,這樣應用啟動時就直接使用中文語言了,代碼如下:
@SpringBootApplication public class Application { public static void main(String[] args) { Locale.setDefault(Locale.CHINA); SpringApplication.run(Application.class, args); } }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/184344.html