add i18n feature similar as ruoyi-plus
This commit is contained in:
parent
8a37ad4428
commit
6e61714019
@ -0,0 +1,21 @@
|
|||||||
|
package com.zhangy.skyeye.common.extend.config;
|
||||||
|
|
||||||
|
import com.zhangy.skyeye.common.extend.core.I18nLocaleResolver;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 国际化配置
|
||||||
|
*
|
||||||
|
* @author zhangy
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class I18nConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver() {
|
||||||
|
return new I18nLocaleResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
package com.zhangy.skyeye.common.extend.core;
|
||||||
|
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求头国际化信息
|
||||||
|
*
|
||||||
|
* @author zhangy
|
||||||
|
*/
|
||||||
|
public class I18nLocaleResolver implements LocaleResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
|
||||||
|
String language = httpServletRequest.getHeader("content-language");
|
||||||
|
Locale locale = Locale.ENGLISH;
|
||||||
|
if (language != null && language.length() > 0) {
|
||||||
|
String[] split = language.split("_");
|
||||||
|
if (split.length > 1) {
|
||||||
|
locale = new Locale(split[0], split[1]);
|
||||||
|
} else {
|
||||||
|
locale = new Locale(split[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package com.zhangy.skyeye.common.extend.util;
|
||||||
|
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.context.NoSuchMessageException;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取i18n资源文件
|
||||||
|
*
|
||||||
|
* @author zhangy
|
||||||
|
*/
|
||||||
|
public class MessageUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据消息键和参数 获取消息 委托给spring messageSource
|
||||||
|
*
|
||||||
|
* @param code 消息键
|
||||||
|
* @param args 参数
|
||||||
|
* @return 获取国际化翻译值
|
||||||
|
*/
|
||||||
|
public static String message(String code, Object... args) {
|
||||||
|
MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
|
||||||
|
try {
|
||||||
|
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
|
||||||
|
} catch (NoSuchMessageException e) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package com.zhangy.skyeye.common.extend.util;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spring工具类 方便在非spring管理环境中获取bean
|
||||||
|
*
|
||||||
|
* @author zhangy
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SpringUtils implements ApplicationContextAware {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前IOC上下文对象
|
||||||
|
*/
|
||||||
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
SpringUtils.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取applicationContext
|
||||||
|
*/
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
return applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过name获取 Bean.
|
||||||
|
*
|
||||||
|
* @param name bean名称
|
||||||
|
* @return bean对象
|
||||||
|
*/
|
||||||
|
public static Object getBean(String name) {
|
||||||
|
return getApplicationContext().getBean(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过class获取Bean.
|
||||||
|
*
|
||||||
|
* @param clazz bean类型
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return bean对象
|
||||||
|
*/
|
||||||
|
public static <T> T getBean(Class<T> clazz) {
|
||||||
|
return getApplicationContext().getBean(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过name,以及Clazz返回指定的Bean
|
||||||
|
*
|
||||||
|
* @param name bean名称
|
||||||
|
* @param clazz bean类型
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return bean对象
|
||||||
|
*/
|
||||||
|
public static <T> T getBean(String name, Class<T> clazz) {
|
||||||
|
return getApplicationContext().getBean(name, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user