java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot国际化配置

springboot如何实现国际化配置

作者:修行者Java

这篇文章主要介绍了springboot如何实现国际化配置问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot国际化配置

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

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

2.在resource下创建i18n文件夹

(internationalization国际化,共18个字母简称i18n),

里面创建messages.properties(默认文件,非中、英文时读取)


这是我用到的内容,可以参考下格式

英文:

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

中文:

我这里直接写的是中文,csdn有很多版本,有的是ascii码,看个人喜好。

如果后面出现中文乱码情况,修改下这里:

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;
    }
    /**
     * 设置当前的返回信息
     *
     * @param request
     * @param code
     * @return
     */
    public String getMessage(HttpServletRequest request, String code) {
        if (messageSource == null) {
            messageSource = initMessageSource();
        }
        String language = request.getHeader("language");
        //默认没有就是请求地区的语言
        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,这里注意下,不然错误提示是乱码

方法中调用:首先注入工具

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

然后

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文