SpringBoot3實(shí)現(xiàn)國(guó)際化的代碼步驟
國(guó)際化實(shí)現(xiàn)步驟
Spring Boot 3 提供了強(qiáng)大的國(guó)際化支持,使得應(yīng)用程序可以根據(jù)用戶(hù)的語(yǔ)言和區(qū)域偏好適配不同的語(yǔ)言和地區(qū)需求。
添加國(guó)際化資源文件: 國(guó)際化資源文件通常放在 src/main/resources 目錄下,并按照不同的語(yǔ)言和地區(qū)命名,例如:
messages.properties:默認(rèn)語(yǔ)言(如英文)messages_zh_CN.properties:中文簡(jiǎn)體messages_fr.properties:法語(yǔ)
配置 MessageSource Bean: 可以通過(guò)在 application.properties 或 application.yml 中進(jìn)行簡(jiǎn)單配置來(lái)加載國(guó)際化資源文件:
spring:
messages:
basename: messages
encoding: UTF-8或者在配置類(lèi)中定義 MessageSource Bean:
@Configuration
public class MessageConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}- 使用國(guó)際化資源: 在代碼中可以通過(guò)
MessageSource來(lái)獲取國(guó)際化消息。例如,在控制器中根據(jù)請(qǐng)求參數(shù)確定語(yǔ)言環(huán)境并獲取對(duì)應(yīng)的消息。 - 模板中的國(guó)際化: 如果使用 Thymeleaf 作為模板引擎,可以在模板中直接使用國(guó)際化消息。需要確保在
application.properties中啟用了國(guó)際化支持,并且在模板中使用#{}表達(dá)式引用消息鍵。 - 自動(dòng)檢測(cè)客戶(hù)端語(yǔ)言: Spring Boot 提供了
LocaleResolver來(lái)自動(dòng)檢測(cè)和設(shè)置客戶(hù)端的語(yǔ)言環(huán)境??梢允褂?AcceptHeaderLocaleResolver或自定義的LocaleResolver。 - 緩存本地語(yǔ)言設(shè)置: 若要將本地語(yǔ)言設(shè)置緩存,可以在自己的配置類(lèi)中增加
LocaleChangeInterceptor攔截器和實(shí)現(xiàn)LocaleResolver方法。比如使用CookieLocaleResolver將語(yǔ)言設(shè)置存儲(chǔ)在 Cookie 中。 - 與 Spring Security 結(jié)合: 在使用 Spring Security 時(shí),可以通過(guò)在資源文件中添加相應(yīng)的消息并在 Spring Security 配置中使用這些消息來(lái)實(shí)現(xiàn)登錄頁(yè)面和錯(cuò)誤信息的多語(yǔ)言支持。
示例
配置國(guó)際化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國(guó)際化配置
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

測(cè)試結(jié)果

到此這篇關(guān)于SpringBoot3實(shí)現(xiàn)國(guó)際化的代碼步驟的文章就介紹到這了,更多相關(guān)SpringBoot3國(guó)際化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
這篇文章主要介紹了基于SpringBoot+Redis的Session共享與單點(diǎn)登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Java實(shí)現(xiàn)發(fā)起HTTP請(qǐng)求的四種方法實(shí)現(xiàn)與適用場(chǎng)景
Java中發(fā)起HTTP請(qǐng)求有多種方式,包括原生庫(kù)、第三方庫(kù)及現(xiàn)代API,以下是常見(jiàn)的幾種實(shí)現(xiàn)方法,本文介紹了它們的具體實(shí)現(xiàn)代碼以及性能適用場(chǎng)景,感興趣的可以了解下2025-06-06
解決微服務(wù)下Mybatis?xml無(wú)效綁定問(wèn)題及分析Invalid?bound?statement
這篇文章主要介紹了解決微服務(wù)下Mybatis?xml無(wú)效綁定問(wèn)題及分析Invalid?bound?statement,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java JSqlParser解析,修改和生成SQL語(yǔ)句的實(shí)用技巧
JSQLParser 是一個(gè)強(qiáng)大的開(kāi)源 Java 庫(kù),用于解析 SQL 并提供語(yǔ)法樹(shù)操作功能,本文將詳細(xì)介紹如何使用 JSQLParser,并提供常見(jiàn)使用場(chǎng)景的代碼示例,希望對(duì)大家有所幫助2025-04-04
java實(shí)現(xiàn)文件打包壓縮輸出到瀏覽器下載
這篇文章主要介紹了java實(shí)現(xiàn)文件打包壓縮輸出到瀏覽器下載,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
JAVA包裝類(lèi)及自動(dòng)封包解包實(shí)例代碼
JAVA包裝類(lèi)及自動(dòng)封包解包實(shí)例代碼,需要的朋友可以參考一下2013-03-03
詳解Java8與Runtime.getRuntime().availableProcessors()
這篇文章主要介紹了詳解Java8與Runtime.getRuntime().availableProcessors(),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

