欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

springboot國際化配置

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

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

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

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

里面創(chuàng)建messages.properties(默認文件,非中、英文時讀?。?/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=手機號不能為空
  • EmailNotEmpty=郵箱不能為空
  • CodeNotEmpty=驗證碼不能為空
  • ResendCode=請重新發(fā)送驗證碼
  • PhoneRegistered=手機號已注冊
  • EmailRegistered=郵箱已注冊
  • PhoneNotRegistered=手機號未注冊
  • EmailNotRegistered=郵箱未注冊
  • CodeIncorrect=驗證碼不正確
  • RegisterError=注冊失敗,請重試
  • AuthError=授權(quán)失敗,請重試
  • OperationError=操作失敗,請重試
  • BindingError=綁定失敗,請重試
  • PhoneNotExist=手機號不存在
  • EmailNotExist=郵箱不存在
  • PasswordError=密碼錯誤
  • AccountFrozen=賬號已被凍結(jié),請聯(lián)系客服
  • AccountNotAudit=賬號未審核,請聯(lián)系客服
  • SendError=發(fā)送失敗,請重試
  • DownloadError=下載失敗,請重試
  • DeleteError=刪除失敗,請重試
  • RetrieveError=密碼找回失敗,請重試
  • UpdateError=更新失敗,請重試
  • OriginalPasswordError=原密碼錯誤
  • ArticleNotExist=沒有此公告
  • UploadError=上傳失敗,請重試
  • MessageError=留言失敗,請重試
  • ReplyError=回復(fù)失敗,請重試
  • RoomEntryError=房間進入失敗,請重試
  • RoomExitError=房間退出失敗,請重試
  • CreateRoomError=房間創(chuàng)建失敗,請重試

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

如果后面出現(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");
        //默認沒有就是請求地區(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;
        }
        //其余的不正確的默認就是本地的語言
        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,這里注意下,不然錯誤提示是亂碼

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

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

然后

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

總結(jié)

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

相關(guān)文章

  • Java中Parser的用法

    Java中Parser的用法

    這篇文章主要介紹了Java?Parser使用指南,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • SpringCloud?Gateway之請求應(yīng)答日志打印方式

    SpringCloud?Gateway之請求應(yīng)答日志打印方式

    這篇文章主要介紹了SpringCloud?Gateway之請求應(yīng)答日志打印方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中ArrayList的常見用法示例小結(jié)

    Java中ArrayList的常見用法示例小結(jié)

    本文介紹了Java的ArrayList,它是一個動態(tài)數(shù)組,可以自動調(diào)整大小,支持添加、刪除、獲取元素等操作,同時,還討論了如何存儲基本數(shù)據(jù)類型以及在多線程環(huán)境下的使用注意事項,感興趣的朋友一起看看吧
    2025-02-02
  • JAVA JDK8 List分組的實現(xiàn)和用法

    JAVA JDK8 List分組的實現(xiàn)和用法

    今天小編就為大家分享一篇關(guān)于JAVA JDK8 List分組的實現(xiàn)和用法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • IDEA中安裝和使用Lombok插件的方法

    IDEA中安裝和使用Lombok插件的方法

    Lombok是一個可以通過簡單的注解形式來幫助我們簡化消除一些必須有但顯得很臃腫的Java代碼的工具,通過使用對應(yīng)的注解,可以在編譯源碼的時候生成對應(yīng)的方法,本文重點給大家介紹IDEA中安裝和使用Lombok插件的方法,感興趣的朋友一起看看吧
    2021-06-06
  • Java?synchronized同步關(guān)鍵字工作原理

    Java?synchronized同步關(guān)鍵字工作原理

    synchronized作為Java程序員最常用同步工具,很多人卻對它的用法和實現(xiàn)原理一知半解,以至于還有不少人認為synchronized是重量級鎖,性能較差,盡量少用。但不可否認的是synchronized依然是并發(fā)首選工具,本文就來詳細講講
    2023-02-02
  • Spring之Environment類的使用方式

    Spring之Environment類的使用方式

    這篇文章主要介紹了Spring之Environment類的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring Boot編寫攔截器教程實例解析

    Spring Boot編寫攔截器教程實例解析

    這篇文章主要介紹了Spring Boot編寫攔截器教程實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Spring基于Aop實現(xiàn)事務(wù)管理流程詳細講解

    Spring基于Aop實現(xiàn)事務(wù)管理流程詳細講解

    這篇文章主要介紹了Spring基于Aop實現(xiàn)事務(wù)管理流程,事務(wù)管理對于企業(yè)應(yīng)用來說是至關(guān)重要的,即使出現(xiàn)異常情況,它也可以保證數(shù)據(jù)的一致性,感興趣想要詳細了解可以參考下文
    2023-05-05
  • Struts2 Result 參數(shù)詳解

    Struts2 Result 參數(shù)詳解

    這篇文章主要講解Struts2 Result的參數(shù),講的比較詳細,希望能給大家做一個參考。
    2016-06-06

最新評論