在Spring应用中实现国际化(i18n)意味着使应用能够根据不同区域设置显示不同的语言和格式。Spring框架通过MessageSource
接口提供了本地化消息的支持,允许开发者在应用中轻松实现国际化。下面是实现Spring应用中的国际化支持的步骤:
1. 本地化资源文件
将你的消息定义在属性文件中,为每种语言/地区创建一个属性文件。文件名通常采用如下格式:
- messages.properties (默认消息)
- messages_en.properties (英语)
- messages_fr.properties (法语)
- messages_zh_CN.properties (简体中文)
- messages_es_ES.properties (西班牙语)
属性文件中定义键值对格式的本地化字符串,例如:
# messages.properties
greeting=Hello
# messages_fr.properties
greeting=Bonjour
2. 配置MessageSource
在你的Spring配置文件中定义一个MessageSource
bean,并告诉Spring你的资源文件在哪里。
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class InternationalizationConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
3. 配置LocaleResolver
为了确定应该使用哪种语言,需要配置一个LocaleResolver
。Spring提供了多种LocaleResolver
实现,例如SessionLocaleResolver
和CookieLocaleResolver
。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class InternationalizationConfig {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
}
4. 使用Interceptor设置Locale
可以使用拦截器如LocaleChangeInterceptor
来允许用户通过请求参数更改locale,比如使用?lang=fr
来切换到法语。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class InternationalizationConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}
}
5. 在应用中使用国际化的消息
你可以在Spring MVC控制器、Thymeleaf模板或其他组件中使用国际化的消息。
在控制器中:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Locale;
@RestController
public class HomeController {
@Autowired
private MessageSource messageSource;
@GetMapping("/")
public String index(Locale locale) {
return messageSource.getMessage("greeting", null, locale);
}
}
在Thymeleaf模板中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<p th:text="#{greeting}">Hello</p>
</body>
</html>
通过上述步骤,你的Spring应用将支持国际化,并能根据用户的地区设置展示不同的消息。确保为你的应用提供了完善的属性文件,并将所有需要本地化的字符串外部化到这些文件中。