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

springboot如何實(shí)現(xiàn)國際化配置

 更新時(shí)間:2023年06月15日 14:20:39   作者:修行者Java  
這篇文章主要介紹了springboot如何實(shí)現(xiàn)國際化配置問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot國際化配置

1.在yml文件的spring下加入下面代碼

 messages:
	encoding: UTF-8
    basename: i18n/messages
    cache-second: 3600

2.在resource下創(chuàng)建i18n文件夾

(internationalization國際化,共18個(gè)字母簡稱i18n),

里面創(chuàng)建messages.properties(默認(rèn)文件,非中、英文時(shí)讀?。?/p>

  • messages_en.properties(英文)
  • messages_zh.properties(中文)


這是我用到的內(nèi)容,可以參考下格式

英文:

NotEmpty=The input cannot be null
PhoneNotEmpty=The cell phone number cannot be empty
EmailNotEmpty=The mailbox cannot be empty
CodeNotEmpty=Verification code cannot be empty
ResendCode=Please resend the verification code
PhoneRegistered=The phone number has been registered
EmailRegistered=Email registered
PhoneNotRegistered=The phone number is not registered
EmailNotRegistered=Email not registered
CodeIncorrect=Verification code is incorrect
RegisterError=Registration failed. Please try again
AuthError=Authorization failed. Please try again
OperationError=Operation failed, please try again
BindingError=Binding failed, please try again
PhoneNotExist=The cell phone number does not exist
EmailNotExist=Email does not exist
PasswordError=Password Error
AccountFrozen=The account has been frozen, please contact customer service
AccountNotAudit=The account has not been audited, please contact customer service
SendError=Failed to send. Please try again
DownloadError=Download failed, please try again
DeleteError=Delete failed, please try again
RetrieveError=Retrieve failed, please try again
UpdateError=Modification failed, please try again
OriginalPasswordError=The original password is incorrect
ArticleNotExist=There is no announcement
UploadError=Upload failed. Please try again
MessageError=Message failed, please try again
ReplyError=Reply failed, please try again
RoomEntryError=Room entry failed. Please try again
RoomExitError=Room exit failed. Please try again
CreateRoomError=Studio creation failed. Please try again

中文:

  • NotEmpty=輸入不能為空
  • PhoneNotEmpty=手機(jī)號不能為空
  • EmailNotEmpty=郵箱不能為空
  • CodeNotEmpty=驗(yàn)證碼不能為空
  • ResendCode=請重新發(fā)送驗(yàn)證碼
  • PhoneRegistered=手機(jī)號已注冊
  • EmailRegistered=郵箱已注冊
  • PhoneNotRegistered=手機(jī)號未注冊
  • EmailNotRegistered=郵箱未注冊
  • CodeIncorrect=驗(yàn)證碼不正確
  • RegisterError=注冊失敗,請重試
  • AuthError=授權(quán)失敗,請重試
  • OperationError=操作失敗,請重試
  • BindingError=綁定失敗,請重試
  • PhoneNotExist=手機(jī)號不存在
  • EmailNotExist=郵箱不存在
  • PasswordError=密碼錯(cuò)誤
  • AccountFrozen=賬號已被凍結(jié),請聯(lián)系客服
  • AccountNotAudit=賬號未審核,請聯(lián)系客服
  • SendError=發(fā)送失敗,請重試
  • DownloadError=下載失敗,請重試
  • DeleteError=刪除失敗,請重試
  • RetrieveError=密碼找回失敗,請重試
  • UpdateError=更新失敗,請重試
  • OriginalPasswordError=原密碼錯(cuò)誤
  • ArticleNotExist=沒有此公告
  • UploadError=上傳失敗,請重試
  • MessageError=留言失敗,請重試
  • ReplyError=回復(fù)失敗,請重試
  • RoomEntryError=房間進(jìn)入失敗,請重試
  • RoomExitError=房間退出失敗,請重試
  • CreateRoomError=房間創(chuàng)建失敗,請重試

我這里直接寫的是中文,csdn有很多版本,有的是ascii碼,看個(gè)人喜好。

如果后面出現(xiàn)中文亂碼情況,修改下這里:

3.后臺公共方法

package com.es.api.modules.common.controller;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
@Controller
public class I18nController {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(I18nController.class);
    private MessageSource messageSource;
    /**
     * 初始化
     *
     * @return
     */
    private MessageSource initMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }
    /**
     * 設(shè)置當(dāng)前的返回信息
     *
     * @param request
     * @param code
     * @return
     */
    public String getMessage(HttpServletRequest request, String code) {
        if (messageSource == null) {
            messageSource = initMessageSource();
        }
        String language = request.getHeader("language");
        //默認(rèn)沒有就是請求地區(qū)的語言
        Locale locale = null;
        if (language == null) {
            locale = request.getLocale();
        } else if ("en".equals(language)) {
            locale = Locale.ENGLISH;
        } else if ("zh".equals(language)) {
            locale = Locale.CHINA;
        }
        //其余的不正確的默認(rèn)就是本地的語言
        else {
            locale = request.getLocale();
        }
        String result = null;
        try {
            result = messageSource.getMessage(code, null, locale);
        } catch (NoSuchMessageException e) {
            LOGGER.error("Cannot find the error message of internationalization, return the original error message.");
        }
        if (result == null) {
            return code;
        }
        return result;
    }
}

注意:我app傳回的language是zh和en,所以我的properties文件命名直接是zh和en,這里注意下,不然錯(cuò)誤提示是亂碼

方法中調(diào)用:首先注入工具

@Autowired
private I18nController i18n;
@Autowired
private HttpServletRequest request;

然后

String message = i18n.getMessage(request, "NotEmpty");
return ResponseUtils.error(message);

總結(jié)

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

相關(guān)文章

  • Java中動態(tài)設(shè)置JVM參數(shù)的方法總結(jié)

    Java中動態(tài)設(shè)置JVM參數(shù)的方法總結(jié)

    通過動態(tài)設(shè)置JVM參數(shù),開發(fā)者可以更有效地管理資源使用和優(yōu)化性能,本文將詳細(xì)闡述如何在Java中動態(tài)設(shè)置JVM參數(shù),感興趣的小伙伴可以了解下
    2024-12-12
  • Java枚舉學(xué)習(xí)之定義和基本特性詳解

    Java枚舉學(xué)習(xí)之定義和基本特性詳解

    枚舉是JAVA?5.0后增加的一個(gè)重要類型??梢杂脕肀硎疽唤M取值范圍固定的變量。本文將通過示例為大家詳細(xì)講解枚舉的定義和基本特性,感興趣的可以了解一下
    2022-08-08
  • 詳解application.properties和application.yml文件的區(qū)別

    詳解application.properties和application.yml文件的區(qū)別

    這篇文章主要介紹了詳解application.properties和application.yml文件的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java堆&優(yōu)先級隊(duì)列示例講解(下)

    Java堆&優(yōu)先級隊(duì)列示例講解(下)

    這篇文章主要通過示例詳細(xì)為大家介紹Java中的堆以及優(yōu)先級隊(duì)列,文中的示例代碼講解詳細(xì),對我們了解java有一定幫助,需要的可以參考一下
    2022-03-03
  • 教你利用springboot集成swagger并生成接口文檔

    教你利用springboot集成swagger并生成接口文檔

    有很多小伙伴不會利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼圖文介紹及代碼示例,對不會這個(gè)方法的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 一文帶你徹底搞懂Lambda表達(dá)式

    一文帶你徹底搞懂Lambda表達(dá)式

    這篇文章主要介紹了一文帶你徹底搞懂Lambda表達(dá)式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java方法重載實(shí)現(xiàn)原理及代碼實(shí)例

    Java方法重載實(shí)現(xiàn)原理及代碼實(shí)例

    這篇文章主要介紹了Java方法重載實(shí)現(xiàn)原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • mybatisplus的邏輯刪除過程

    mybatisplus的邏輯刪除過程

    這篇文章主要介紹了mybatisplus的邏輯刪除過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-08-08
  • springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解

    springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解

    這篇文章主要介紹了springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • SpringBoot中集成IP2Region實(shí)現(xiàn)高效IP地址地理位置查詢

    SpringBoot中集成IP2Region實(shí)現(xiàn)高效IP地址地理位置查詢

    IP2Region是一個(gè)高效的IP地址地理位置查詢庫,能夠快速根據(jù)IP地址獲取其地理位置信息,它支持IPv4和IPv6地址的查詢,并提供多種緩存方式來優(yōu)化查詢性能,在這篇博客中,我們將介紹如何將IP2Region集成到SpringBoot項(xiàng)目中,實(shí)現(xiàn)IP地址的地理位置查詢服務(wù)
    2025-12-12

最新評論

山西省| 泗洪县| 乃东县| 宝鸡市| 乌审旗| 黑山县| 乌兰浩特市| 湖北省| 太白县| 乃东县| 璧山县| 潢川县| 盐源县| 宁都县| 富锦市| 司法| 凤翔县| 广安市| 定南县| 塘沽区| 福贡县| 弥渡县| 德清县| 萨迦县| 临安市| 吴堡县| 池州市| 左贡县| 万荣县| 安化县| 酒泉市| 雷波县| 渑池县| 贞丰县| 饶阳县| 漯河市| 仁怀市| 鹤岗市| 古浪县| 双牌县| 城步|