Compare commits
2 Commits
8a37ad4428
...
ed5ac67942
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed5ac67942 | ||
|
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.zhangy.skyeye.jm.controller;
|
||||||
|
|
||||||
|
import com.zhangy.skyeye.common.extend.anno.IgnoreAuth;
|
||||||
|
import com.zhangy.skyeye.common.extend.util.MessageUtils;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* i18n 测试
|
||||||
|
*
|
||||||
|
* @author zhangy
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/test/i18n")
|
||||||
|
public class TestI18nController {
|
||||||
|
|
||||||
|
@IgnoreAuth
|
||||||
|
@GetMapping
|
||||||
|
public String test(String code) {
|
||||||
|
return MessageUtils.message(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,6 +14,9 @@ spring:
|
|||||||
allow-circular-references: true
|
allow-circular-references: true
|
||||||
application:
|
application:
|
||||||
name: @artifactId@
|
name: @artifactId@
|
||||||
|
messages:
|
||||||
|
basename: i18n/messages
|
||||||
|
fallback-to-system-locale: false
|
||||||
profiles:
|
profiles:
|
||||||
# active: dev
|
# active: dev
|
||||||
config:
|
config:
|
||||||
|
|||||||
@ -0,0 +1,3 @@
|
|||||||
|
# Default messages
|
||||||
|
user.login.success=Login success
|
||||||
|
user.login.error=Login error
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
# 中文消息
|
||||||
|
user.login.success=登录成功
|
||||||
|
user.login.error=登录失败
|
||||||
Loading…
Reference in New Issue
Block a user