login.password=密码
login.tip=请登陆
login.btn=登陆
login.username=用户名
login.rememberme=记住我
login.password=password
login.tip=Please sign in
login.btn=sign in
login.username=username
login.rememberme=Rememberme
login.password=密码
login.tip=请登陆
login.btn=登陆
login.username=用户名
login.rememberme=记住我
@AutoConfiguration
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {
private static final Resource[] NO_RESOURCES = {};
@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
}
spring:
messages:
basename: i18n.login #消息转换配置
Signin Template for Bootstrap
@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME)
public LocaleResolver localeResolver() {
if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.webProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.webProperties.getLocale());
return localeResolver;
}
public class AcceptHeaderLocaleResolver implements LocaleResolver{
@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale();
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale;
}
Locale requestLocale = request.getLocale();
List supportedLocales = getSupportedLocales();
if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
return requestLocale;
}
Locale supportedLocale = findSupportedLocale(request, supportedLocales);
if (supportedLocale != null) {
return supportedLocale;
}
return (defaultLocale != null ? defaultLocale : requestLocale);
}
}
package com.zk.config;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* @author CNCLUKZK
* @create 2022/8/2-1:29
*/
public class MyLocaleResolver implements LocaleResolver {
//自定义地区国际化解析
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求的语言参数
String l = request.getParameter("l");
Locale locale = Locale.getDefault(); //如果没有就使用默认的国际化
//若请求携带了国际化参数
if (StringUtils.hasText(l)) {
//国家、地区
locale = new Locale(l.split("_")[0], l.split("_")[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
/**
* @Bean相当于beans中的单个的bean标签,注册到容器中
* 方法名字相当于bean标签的id
* 方法返回值相当于bean标签的Class属性
* 返回要注入的bean对象
* */
package com.zk.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author CNCLUKZK
* @create 2022/7/31-20:19
*/
//扩展springmvc
//如果。你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装魔!
//应为类型要求为WebMvcConfigurer,所以我们实现其接口
//可以使用自定义类扩展springmvc的功能
@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送特殊请求,就会跳转到特定页面
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
//注册自定义的国际化解析bean
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}