springboot国际化⽂件
springboot 国际化⽂件
国际化:根据locale的不同,显⽰不同的信息
***********************
相关类与接⼝
SessionLocaleResolver:国际化解析器
public class SessionLocaleResolver extends AbstractLocaleContextResolver {
public static final String LOCALE_SESSION_ATTRIBUTE_NAME = Name() + ".LOCALE";
public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = Name() + ".TIME_ZONE"; private String localeAttributeName;
private String timeZoneAttributeName;
public SessionLocaleResolver() {
this.localeAttributeName = LOCALE_SESSION_ATTRIBUTE_NAME;
this.timeZoneAttributeName = TIME_ZONE_SESSION_ATTRIBUTE_NAME;
}
AbstractLocaleContextResolver
public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolver implements LocaleContextResolver {
@Nullable
private TimeZone defaultTimeZone;
AbstractLocaleResolver
public abstract class AbstractLocaleResolver implements LocaleResolver {
@Nullable
private Locale defaultLocale;
public AbstractLocaleResolver() {
}
public void setDefaultLocale(@Nullable Locale defaultLocale) { //设置默认Locale
this.defaultLocale = defaultLocale;
}
@Nullable
protected Locale getDefaultLocale() {
return this.defaultLocale;
}
}
ReloadableResourceBundleMessageSource:加载国际化属性⽂件
ReloadableResourceBundleMessageSource
public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements ResourceLoaderAware { private static final String PROPERTIES_SUFFIX = ".properties";
private static final String XML_SUFFIX = ".xml";
public ReloadableResourceBundleMessageSource() {
}
AbstractResourceBasedMessageSource
public abstract class AbstractResourceBasedMessageSource extends AbstractMessageSource {
private final Set<String> basenameSet = new LinkedHashSet(4);
@Nullable
private String defaultEncoding;
private boolean fallbackToSystemLocale = true;
@Nullable
private Locale defaultLocale;
private long cacheMillis = -1L; //默认不重新加载⽂件
********
常⽤⽅法
public void setBasename(String basename) {
public void basenames) { //属性⽂件名前缀
public void setCacheSeconds(int cacheSeconds) {
public void setCacheMillis(long cacheMillis) { //加载⽂件的时间间隔
LocaleChangeInterceptor:国际化
public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
public static final String DEFAULT_PARAM_NAME = "locale";
protected final Log logger = Class());
private String paramName = "locale";
********
构造⽅法
public LocaleChangeInterceptor() {
}
********
常⽤⽅法
public void setParamName(String paramName) { //设置拦截的参数名称
***********************
⽰例
***************
config 层
DataConfig:配置localeResolver、messageSource、localeChangeResolver
@Bean("localeResolver")
public LocaleResolver initSessionLocaleResolver(){
SessionLocaleResolver sessionLocaleResolver=new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
return sessionLocaleResolver;
}
@Bean("messageSource")
public MessageSource initReloadableResourceBundleMessageSource(){
ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:/static/properties/msg");
messageSource.setDefaultEncoding("utf-8");
messageSource.setCacheSeconds(60);
return messageSource;
}
@Bean
public LocaleChangeInterceptor initLocaleChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
}
WebConfig:配置拦截路径
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Resource
private LocaleChangeInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor).addPathPatterns("/change/**");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/hello").setViewName("hh");
}
}
***************
controller 层
HelloController
@RequestMapping("/change")
public void change(){
}
}
********************
前端页⾯
<script>
$(function () {
$("#b1").click(function () {
$.post({
url: "/change?language=zh_CN" });
load();
});
$("#b2").click(function () {
$.post({
url: "/change?language=en_US" });
load();
springboot中文})
})
</script>
<body>
<div th:align="center">
<span th:text="#{hello}"></span><br> <button id="b1">中⽂</button><br>
<button id="b2">english</button>
</div>
</html
***********************
属性⽂件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论