最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何集成i18n(多語言)

 更新時間:2024年04月03日 10:29:41   作者:@幻影忍者  
這篇文章主要介紹了SpringBoot如何集成i18n(多語言)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot集成i18n(多語言)

配置文件

spring:

        messages:
                basename: il8n/messages # 配置國際化資源文件路徑
                fallback-to-system-locale: true # 是否使用系統(tǒng)默認的語言環(huán)境作為備選項

國際化配置

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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
* 國際化配置
*/
@Configuration
public class I18nlocaleConfig implements WebMvcConfigurer{
/**
* 默認解析器 其中l(wèi)ocale表示默認語言
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {

LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("Accept-Language");
return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}

參數(shù)解析

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 參數(shù)解析
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
// 從 request 域中讀取傳過來的參數(shù)
String l = request.getHeader("Accept-Language");
// 聲明 Locale 為默認語言顯示
Locale locale = Locale.getDefault();
// 判斷傳入?yún)?shù)是否為空
if (!StringUtils.isEmpty(language) && StringUtils.contains(language,"_")){
// 將傳過來的參數(shù),通過下劃線分割,獲取到地區(qū)(zh)即代碼(CN)
String[] split = l.split("_");
// 進行賦值
locale = new Locale(split[0],split[1]);
}
// 返回
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}

ApplicationEvent

import com.zzdy.recharge.il8n.utils.MessageUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ApplicationEvent implements ApplicationListener<ContextRefreshedEvent> {
@Resource
protected MessageSource messageSource;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
MessageUtils.setMessageSource(messageSource);
}
}

MessageUtils

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;


public class MessageUtils extends ResourceBundleMessageSource {
private static MessageSource messageSource;

public static void setMessageSource(MessageSource source){
messageSource=source;
}
public MessageUtils() {
super();
}
/**
* 獲取單個國際化翻譯值
*/
public static String get(String pvsKey) {
try {
return messageSource.getMessage(pvsKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
/**
* 獲取單個國際化翻譯值
*/
public static String get(String pvsKey,Object ... pvParams) {
try {
return messageSource.getMessage(pvsKey, pvParams, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
}

運行

import com.zzdy.recharge.il8n.utils.MessageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("recharge/i18n")
public class GreetingController {

@GetMapping("/greeting")
  public String greeting() {
        return MessageUtils.get("not.null");
  }
}

運行截圖

  • 中文

  • 英文

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java多種方法實現(xiàn)合并多個list對象列表

    Java多種方法實現(xiàn)合并多個list對象列表

    Java編程中,合并多個列表對象可以通過Stream?API或傳統(tǒng)循環(huán)方式實現(xiàn),使用Stream?API合并時,利用flatMap方法將嵌套的List展平,再通過collect方法收集成一個新的列表,傳統(tǒng)循環(huán)則通過創(chuàng)建一個空的ArrayList,并通過遍歷每個列表將元素添加進去
    2024-09-09
  • 使用EasyPOI實現(xiàn)多sheet動態(tài)列導(dǎo)出

    使用EasyPOI實現(xiàn)多sheet動態(tài)列導(dǎo)出

    這篇文章主要為大家詳細介紹了如何使用EasyPOI根據(jù)指定時間范圍創(chuàng)建動態(tài)列,以及如何將數(shù)據(jù)組織成符合要求的格式并導(dǎo)出,感興趣的可以了解下
    2025-03-03
  • Java字符串拼接效率測試過程解析

    Java字符串拼接效率測試過程解析

    這篇文章主要介紹了Java字符串拼接效率測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • springboot中使用自定義兩級緩存的方法

    springboot中使用自定義兩級緩存的方法

    這篇文章主要介紹了springboot中使用自定義兩級緩存的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 解決window.location.href之后session丟失的問題

    解決window.location.href之后session丟失的問題

    今天小編就為大家分享一篇關(guān)于解決window.location.href之后session丟失的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java實現(xiàn)合并多個PDF的示例代碼

    Java實現(xiàn)合并多個PDF的示例代碼

    這篇文章主要介紹了通過Java實現(xiàn)合并多個PDF,并將合并后的新PDF存儲到文件夾下,文中的示例代碼簡潔易懂,感興趣的可以跟隨小編一起試一試
    2022-01-01
  • springboot啟動報錯Failed to load class [javax.servlet.Filter]的解決

    springboot啟動報錯Failed to load class [java

    這篇文章主要介紹了springboot啟動報錯Failed to load class [javax.servlet.Filter]的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • 關(guān)于druid配置參數(shù)的使用詳解

    關(guān)于druid配置參數(shù)的使用詳解

    這篇文章主要介紹了關(guān)于druid配置參數(shù)的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • 徹底搞定堆排序:二叉堆

    徹底搞定堆排序:二叉堆

    二叉堆有兩種:最大堆和最小堆。最大堆:父結(jié)點的鍵值總是大于或等于任何一個子節(jié)點的鍵值;最小堆:父結(jié)點的鍵值總是小于或等于任何一個子節(jié)點的鍵值
    2021-07-07
  • Java Reactor反應(yīng)器模式使用方法詳解

    Java Reactor反應(yīng)器模式使用方法詳解

    這篇文章主要介紹了Java Reactor反應(yīng)器模式使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06

最新評論

溆浦县| 丹棱县| 彭山县| 仁化县| 陇南市| 舞钢市| 鄢陵县| 岑巩县| 天峨县| 凤阳县| 新郑市| 万山特区| 库车县| 家居| 根河市| 多伦县| 德惠市| 乌拉特前旗| 大关县| 邛崃市| 沂南县| 开化县| 仁化县| 樟树市| 平遥县| 葵青区| 论坛| 盈江县| 华池县| 图木舒克市| 莱州市| 玉田县| 阿坝县| 安福县| 苏尼特左旗| 旬邑县| 马尔康县| 义马市| 蒲城县| 集贤县| 海丰县|