Spring国际化配置通过使用MessageSource
、LocaleResolver
和LocaleChangeInterceptor
等组件来实现。以下是一个详细的示例,展示如何配置Spring项目以支持国际化:
1. 配置MessageSource
首先,配置一个MessageSource
bean,用于加载国际化资源文件。这些资源文件通常位于src/main/resources
目录下。
@Configuration
public class LocaleConf {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
//classpath的目录下指的是src/main/resources,可设置多个配置文件
source.setBasenames("classpath:i18n/messages",
"classpath:i18n/financeManage",
"classpath:i18n/menuFields",
"classpath:i18n/systemManage"
);
source.setDefaultEncoding("UTF-8");
return source;
}
}
2. 配置LocaleResolver
LocaleResolver
用于解析和存储用户的语言环境。可以使用SessionLocaleResolver
或CookieLocaleResolver
。resolver.setDefaultLocale(Locale.US);设置默认语言。
@Configuration
public class LocaleConf {
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver slr = new CookieLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.US);
slr.setCookieName("sis_lang");
return slr;
}
}
3. 配置LocaleChangeInterceptor
LocaleChangeInterceptor
用于拦截请求并根据请求参数更改语言环境。
Java配置:
@Configuration
public class LocaleConf {
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
};
}
}
xml配置
<!-- spring-mvc.xml -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
4. 配置国际化资源文件
在src/main/resources
目录下创建国际化资源文件,例如messages_en_US.properties
和messages_zh_CN.properties
。
messages_en_US.properties:
common.greeting=Hello
common.farewell=Goodbye
messages_zh_CN.properties:
common.greeting=你好
common.farewell=再见
5. 使用国际化资源文件
创建MessageUtill类,在代码中通过MessageSource
来获取国际化消息。调用MessageUtils中的getMessage函数根据当前语言环境获取对应的国际化资源消息。
public class MessageUtils {
private static MessageSource messageSource;
public MessageUtils(MessageSource messageSource) {
this.messageSource = messageSource;
}
public static String getMessage(String msgKey, Object[] args) {
return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale());
}
public static String getMessage(String msgKey) {
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
}
}
6.前端中使用国际化
使用 FreeMarker 模板引擎与 Spring Boot 集成来实现国际化功能,可以通过 <#import "/spring.ftl" as spring>
标签导入 Spring FreeMarker 标签库。以下是如何在 FreeMarker 模板中使用国际化消息。
<#import "/spring.ftl" as spring>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>例子</title>
</head>
<body>
<h1>
<@spring.message "common.greeting" />
</h1>
<button onclick="changeLanguage('en_US')">English</button>
<button onclick="changeLanguage('zh_CN')">中文</button>
<script>
function changeLanguage(lang) {
window.location.href = "http://localhost:8080/?"lang="+lang;
}
</script>
</body>
</html>