springboot如何實(shí)現(xiàn)國(guó)際化配置
springboot國(guó)際化配置
1.在yml文件的spring下加入下面代碼
messages: encoding: UTF-8 basename: i18n/messages cache-second: 3600
2.在resource下創(chuàng)建i18n文件夾
(internationalization國(guó)際化,共18個(gè)字母簡(jiǎn)稱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ī)號(hào)不能為空EmailNotEmpty
=郵箱不能為空CodeNotEmpty
=驗(yàn)證碼不能為空ResendCode
=請(qǐng)重新發(fā)送驗(yàn)證碼PhoneRegistered
=手機(jī)號(hào)已注冊(cè)EmailRegistered
=郵箱已注冊(cè)PhoneNotRegistered
=手機(jī)號(hào)未注冊(cè)EmailNotRegistered
=郵箱未注冊(cè)CodeIncorrect
=驗(yàn)證碼不正確RegisterError
=注冊(cè)失敗,請(qǐng)重試AuthError
=授權(quán)失敗,請(qǐng)重試OperationError
=操作失敗,請(qǐng)重試BindingError
=綁定失敗,請(qǐng)重試PhoneNotExist
=手機(jī)號(hào)不存在EmailNotExist
=郵箱不存在PasswordError
=密碼錯(cuò)誤AccountFrozen
=賬號(hào)已被凍結(jié),請(qǐng)聯(lián)系客服AccountNotAudit
=賬號(hào)未審核,請(qǐng)聯(lián)系客服SendError
=發(fā)送失敗,請(qǐng)重試DownloadError
=下載失敗,請(qǐng)重試DeleteError
=刪除失敗,請(qǐng)重試RetrieveError
=密碼找回失敗,請(qǐng)重試UpdateError
=更新失敗,請(qǐng)重試OriginalPasswordError
=原密碼錯(cuò)誤ArticleNotExist
=沒有此公告UploadError
=上傳失敗,請(qǐng)重試MessageError
=留言失敗,請(qǐng)重試ReplyError
=回復(fù)失敗,請(qǐng)重試RoomEntryError
=房間進(jìn)入失敗,請(qǐng)重試RoomExitError
=房間退出失敗,請(qǐng)重試CreateRoomError
=房間創(chuàng)建失敗,請(qǐng)重試
我這里直接寫的是中文,csdn有很多版本,有的是ascii碼,看個(gè)人喜好。
如果后面出現(xiàn)中文亂碼情況,修改下這里:
3.后臺(tái)公共方法
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ǐng)求地區(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設(shè)計(jì)模式之備忘錄模式原理與用法詳解
這篇文章主要介紹了JAVA設(shè)計(jì)模式之備忘錄模式,簡(jiǎn)單說明了備忘錄模式的概念、原理并結(jié)合實(shí)例形式分析了java備忘錄模式的具體定義及使用方法,需要的朋友可以參考下2017-08-08mybatis 在typeAliases別名時(shí)報(bào)錯(cuò)的解決
這篇文章主要介紹了mybatis 在typeAliases別名時(shí)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)
這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04javaCV開發(fā)詳解之推流器和錄制器的實(shí)現(xiàn)
這篇文章主要介紹了javaCV開發(fā)詳解之推流器和錄制器實(shí)現(xiàn),對(duì)JavaCV感興趣的同學(xué),可以參考下2021-04-04基于java+springboot+mybatis+laiyu實(shí)現(xiàn)學(xué)科競(jìng)賽管理系統(tǒng)
這篇文章主要介紹了基于java+springboot+mybatis+laiyu實(shí)現(xiàn)的學(xué)科競(jìng)賽管理系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Java8使用stream實(shí)現(xiàn)list中對(duì)象屬性的合并(去重并求和)
這篇文章主要介紹了Java8使用stream實(shí)現(xiàn)list中對(duì)象屬性的合并(去重并求和),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01