SpringBoot3實現(xiàn)國際化的代碼步驟
國際化實現(xiàn)步驟
Spring Boot 3 提供了強大的國際化支持,使得應(yīng)用程序可以根據(jù)用戶的語言和區(qū)域偏好適配不同的語言和地區(qū)需求。
添加國際化資源文件: 國際化資源文件通常放在 src/main/resources
目錄下,并按照不同的語言和地區(qū)命名,例如:
messages.properties
:默認(rèn)語言(如英文)messages_zh_CN.properties
:中文簡體messages_fr.properties
:法語
配置 MessageSource Bean: 可以通過在 application.properties
或 application.yml
中進行簡單配置來加載國際化資源文件:
spring: messages: basename: messages encoding: UTF-8
或者在配置類中定義 MessageSource
Bean:
@Configuration public class MessageConfig { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } }
- 使用國際化資源: 在代碼中可以通過
MessageSource
來獲取國際化消息。例如,在控制器中根據(jù)請求參數(shù)確定語言環(huán)境并獲取對應(yīng)的消息。 - 模板中的國際化: 如果使用 Thymeleaf 作為模板引擎,可以在模板中直接使用國際化消息。需要確保在
application.properties
中啟用了國際化支持,并且在模板中使用#{}
表達式引用消息鍵。 - 自動檢測客戶端語言: Spring Boot 提供了
LocaleResolver
來自動檢測和設(shè)置客戶端的語言環(huán)境??梢允褂?AcceptHeaderLocaleResolver
或自定義的LocaleResolver
。 - 緩存本地語言設(shè)置: 若要將本地語言設(shè)置緩存,可以在自己的配置類中增加
LocaleChangeInterceptor
攔截器和實現(xiàn)LocaleResolver
方法。比如使用CookieLocaleResolver
將語言設(shè)置存儲在 Cookie 中。 - 與 Spring Security 結(jié)合: 在使用 Spring Security 時,可以通過在資源文件中添加相應(yīng)的消息并在 Spring Security 配置中使用這些消息來實現(xiàn)登錄頁面和錯誤信息的多語言支持。
示例
配置國際化yaml
spring: messages: encoding: UTF-8 basename: i18n/messages profiles: active: zh_CN #-Dspring.profiles.active=en_US
英文
server: port: 8000 spring: jackson: date-format: MM-dd-yyyy
中文
spring: jackson: date-format: yyyy-MM-dd server: port: 8000
國際化配置
package com.cokerlk.language; import com.cokerlk.language.service.EnUSProductService; import com.cokerlk.language.service.IProductService; import com.cokerlk.language.service.ZhCNProductService; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Data public class I18NConfiguration { @Value("${spring.profiles.active}") private String locale; @Profile("zh_CN") @Bean public IProductService zhCNBussService(){ return new ZhCNProductService(); } @Profile("en_US") @Bean public IProductService enUSBussService(){ return new EnUSProductService(); } }
產(chǎn)品接口
package com.cokerlk.language.service; import java.util.Map; public interface IProductService { Map<String,String> getProduct(); }
中文產(chǎn)品
package com.cokerlk.language.service; import com.cokerlk.language.I18NConfiguration; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.context.MessageSource; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; @Slf4j public class ZhCNProductService implements IProductService { @Resource I18NConfiguration i18NConfiguration; @Resource MessageSource messageSource; @Override public Map getProduct() { log.info("中文"); Map result = new HashMap(); result.put("create-date", new Date()); result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale()))); return result; } }
英文產(chǎn)品
package com.cokerlk.language.service; import com.cokerlk.language.I18NConfiguration; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.context.MessageSource; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; @Slf4j public class EnUSProductService implements IProductService { @Resource I18NConfiguration i18NConfiguration; @Resource MessageSource messageSource; @Override public Map<String,String> getProduct() { log.info("英文"); Map result = new HashMap(); result.put("create-date", new Date()); result.put("text", messageSource.getMessage("product_name", null, Locale.of(i18NConfiguration.getLocale()))); return result; } }
message配置
#messages.properties product_name=huawei mate 70 #messages_en_US.properties product_name=Hua wei mate 70 #messages_zh_CN.properties product_name=華為mate70
測試結(jié)果
到此這篇關(guān)于SpringBoot3實現(xiàn)國際化的代碼步驟的文章就介紹到這了,更多相關(guān)SpringBoot3國際化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于SpringBoot+Redis的Session共享與單點登錄詳解
這篇文章主要介紹了基于SpringBoot+Redis的Session共享與單點登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Java實現(xiàn)發(fā)起HTTP請求的四種方法實現(xiàn)與適用場景
Java中發(fā)起HTTP請求有多種方式,包括原生庫、第三方庫及現(xiàn)代API,以下是常見的幾種實現(xiàn)方法,本文介紹了它們的具體實現(xiàn)代碼以及性能適用場景,感興趣的可以了解下2025-06-06解決微服務(wù)下Mybatis?xml無效綁定問題及分析Invalid?bound?statement
這篇文章主要介紹了解決微服務(wù)下Mybatis?xml無效綁定問題及分析Invalid?bound?statement,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Java JSqlParser解析,修改和生成SQL語句的實用技巧
JSQLParser 是一個強大的開源 Java 庫,用于解析 SQL 并提供語法樹操作功能,本文將詳細(xì)介紹如何使用 JSQLParser,并提供常見使用場景的代碼示例,希望對大家有所幫助2025-04-04詳解Java8與Runtime.getRuntime().availableProcessors()
這篇文章主要介紹了詳解Java8與Runtime.getRuntime().availableProcessors(),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06