# 定義國際化資源
- resources下新建i18n文件夾
- 新建xx.properties文件
- 中文:新建xx_zh_CN.properties文件存放對應的中文
-
英文:新建xx_en_US.properties文件存放對應的英文
效果是這樣的:
image.png - 定義需要國際化的內容
image.png
- 在application.yml中配置
spring:
messages:
# 定義國際化文件的文件地址播玖,讀取的原則是順序讀取如果存在同名的則讀取第一個
basename: i18n/login,i18n/errorMessage
- 定義國際化解析器與攔截器
package com.futao.springmvcdemo.foundation.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
/**
* 通用bean注冊管理中心
*
* @author futao
* Created on 2019-01-15.
*/
@org.springframework.context.annotation.Configuration
public class Configuration {
/**
* 國際化爹殊,設置默認的語言為中文
* 將用戶的區(qū)域信息存在session
* 也可以存在cookie,由客戶端保存 {@link org.springframework.web.servlet.i18n.CookieLocaleResolver}
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
/**
* 國際化,設置url識別參數(shù)
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
}
- 國際化配置文件會被spring加載牵触,所以可以通過spring容器獲取到國際化文件里面的內容
- 獲取spring容器
package com.futao.springmvcdemo.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/**
* Spring容器
*
* @author futao
* Created on 2019-01-15.
*/
@Service
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (context == null) {
context = applicationContext;
}
}
/**
* 獲取容器
*
* @return 容器
*/
public static ApplicationContext getContext() {
return context;
}
}
- 在程序中使用
//通過spring容器讀取
SpringUtils.getContext().getMessage(code, null, LocaleContextHolder.getLocale())
//或者
@Resource
private MessageSource messageSource;
return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
- 寫個工具類
package com.futao.springmvcdemo.service.notbusiness;
import com.futao.springmvcdemo.foundation.LogicException;
import com.futao.springmvcdemo.model.system.ErrorMessage;
import com.futao.springmvcdemo.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* 國際化
*
* @author futao
* Created on 2019-01-15.
*/
public class I18nService {
private static final Logger LOGGER = LoggerFactory.getLogger(I18nService.class);
/**
* 獲取消息
* 也可以使用下面的方式:
* Resource private MessageSource messageSource;
* return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
*
* @param code k
* @return v
*/
public static String getMessage(String code) {
try {
return SpringUtils.getContext().getMessage(code, null, LocaleContextHolder.getLocale());
} catch (NoSuchMessageException e) {
LOGGER.error("獲取國際化資源{}失敗" + e.getMessage(), code, e);
throw LogicException.le(ErrorMessage.I18N_RESOURCE_NOT_FOUND, new String[]{code});
}
}
}
- 使用
I18nService.getMessage("welcome")
- 前端在接口后面加上local信息
?lang=zh_CN
或者?lang=en_US
即可
源代碼: https://github.com/FutaoSmile/springbootFramework
或者: https://gitee.com/FutaoSmile/springboot_framework
歡迎star~
我在這里等你:
image.png