springboot-15-页面国际化

Spring Boot国际化

① 国际化介绍

国际化(Internationalization 简称 I18n,其中“I”和“n”分别为首末字符,18 则为中间的字符数)是指软件开发时应具备多种语言和地区的功能。换句话说就是,开发的软件需要能同时应对不同国家和地区的用户访问,并根据用户地区和语言习惯,提供相应的、符合用户阅读习惯的页面和数据。

② 实现国际化

Spring Boot实现国际化,准备工作如下:

1.IDEA设置编码问题

要统一设置properites的编码问题,不然会出现乱码

打开setting-File Encoding
在这里插入图片描述
编写国际化配置文件之前,需要抽取需要显示的国际化页面信息,明确哪些内容需要编写国际化配置。

2.编写国际化资源(配置)文件

在Spring Boot项目resource目录下创建i18n目录,存放国际化配置文件。

建立一个login.properties文件,还有一个login_zh_CN.properties;然后通过以下方式再添加一个login_en_US.properties

在这里插入图片描述
安装Resource Bundle插件,在Setting->Plugins
在这里插入图片描述

  • login.properties:无语言设置时生效
  • login_en_US.properties :英语时生效
  • login_zh_CN.properties:中文时生效

打开任意一个国际化资源文件,并切换为 Resource Bundle 模式,然后点击“+”号,创建所需的国际化属性,如下图。

在这里插入图片描述

3.使用ResourceBundleMessageSource管理国际化资源文件

Spring Boot 已经对 ResourceBundleMessageSource 提供了默认的自动配置 MessageSourceAutoConfiguration

找到 MessageSourceAutoConfiguration

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
//只有容器中没有messageSource这个Bean才生效
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
//满足ResourceBundleCondition类的条件才有效
@EnableConfigurationProperties
// 开启配置属性
public class MessageSourceAutoConfiguration {
	// 保存的国际化资源配置文件
	private static final Resource[] NO_RESOURCES = {};
	// 将 MessageSourceProperties 以组件的形式添加到容器中
    // MessageSourceProperties 下的每个属性都与以 spring.messages 下的所有属性一一对应,双向绑定
	@Bean
	@ConfigurationProperties(prefix = "spring.messages")
	public MessageSourceProperties messageSourceProperties() {
		return new MessageSourceProperties();
	}
	//Spring Boot 会从容器中获取 MessageSourceProperties
    // 读取国际化资源文件的 basename(基本名)、encoding(编码)等信息
    // 并封装到 ResourceBundleMessageSource 中
	@Bean
	public MessageSource messageSource(MessageSourceProperties properties) {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		       //读取国际化资源文件的 basename (基本名),并封装到 ResourceBundleMessageSource 中
		if (StringUtils.hasText(properties.getBasename())) {
			messageSource.setBasenames(StringUtils
					.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
		}
		
        //读取国际化资源文件的 encoding (编码),并封装到 ResourceBundleMessageSource 中
		if (properties.getEncoding() != null) {
			messageSource.setDefaultEncoding(properties.getEncoding().name());
		}
		messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
		Duration cacheDuration = properties.getCacheDuration();
		if (cacheDuration != null) {
			messageSource.setCacheMillis(cacheDuration.toMillis());
		}
		messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
		messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
		return messageSource;
	}
	
	//条件类
	protected static class ResourceBundleCondition extends SpringBootCondition {
		
		private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
		//获取匹配basename的结果
		@Override
		public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
			String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
			ConditionOutcome outcome = cache.get(basename);
			if (outcome == null) {
				outcome = getMatchOutcomeForBasename(context, basename);
				cache.put(basename, outcome);
			}
			return outcome;
		}
		private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
			ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle");
			for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
				for (Resource resource : getResources(context.getClassLoader(), name)) {
					if (resource.exists()) {
						return ConditionOutcome.match(message.found("bundle").items(resource));
					}
				}
			}
			return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
		}
		//获取所有带有基本名的国际化配置文件
		private Resource[] getResources(ClassLoader classLoader, String name) {
			String target = name.replace('.', '/');
			try {
				return new PathMatchingResourcePatternResolver(classLoader)
						.getResources("classpath*:" + target + ".properties");
			}
			catch (Exception ex) {
				return NO_RESOURCES;
			}
		}

	}

}

通过源码分析可以知道

  • Spring Boot 将 MessageSourceProperties 以组件的形式添加到容器中;
    MessageSourceProperties 的属性与配置文件中以“spring.messages”开头的配置进行了绑定;
  • Spring Boot 从容器中获取 MessageSourceProperties 组件,并从中读取国际化资源文件的 basename(文件基本名)、encoding(编码)等信息,将它们封装到 ResourceBundleMessageSource 中;
  • Spring Boot 将 ResourceBundleMessageSource 以组件的形式添加到容器中,进而实现对国际化资源文件的管理。
  • 只有满足了ResourceBundleCondition条件, MessageSourceAutoConfiguration才能生效,通过basename去获得国际化资源配置文件的位置

查看 MessageSourceProperties 类,其代码如下。


public class MessageSourceProperties {
    private String basename = "messages";
    private Charset encoding;
    @DurationUnit(ChronoUnit.SECONDS)
    private Duration cacheDuration;
    private boolean fallbackToSystemLocale;
    private boolean alwaysUseMessageFormat;
    private boolean useCodeAsDefaultMessage;
    public MessageSourceProperties() {
        this.encoding = StandardCharsets.UTF_8;
        this.fallbackToSystemLocale = true;
        this.alwaysUseMessageFormat = false;
        this.useCodeAsDefaultMessage = false;
    }
    ...
}

解析以上代码,可以知道:

  • MessageSourceProperties 为 basename、encoding 等属性提供了默认值;
  • basename 表示国际化资源文件的基本名,其默认取值为“message”,即 Spring Boot 默认会获取类路径下的 message.properties 以及 message_XXX.properties 作为国际化资源文件;
  • 在 application.porperties/yaml 等配置文件中,使用配置参数“spring.messages.basename”即可重新指定国际化资源文件的基本名,来获取国际化资源配置文件

4.在页面获取国际化内容

在页面使用Thymeleaf模板引擎,我们可以通过#{…}来获取国际化内容

注意:要在templates目录下,才有用,其他目录下国际化内容配置不生效

在templates目录下创建一个index.html,代码如下


<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <style>
        html,
        body {
            height: 100%;
        }

        body {
            display: -ms-flexbox;
            display: -webkit-box;
            display: flex;
            -ms-flex-align: center;
            -ms-flex-pack: center;
            -webkit-box-align: center;
            align-items: center;
            -webkit-box-pack: center;
            justify-content: center;
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #f5f5f5;
        }

        .form-signin {
            width: 100%;
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
        }
        .form-signin .checkbox {
            font-weight: 400;
        }
        .form-signin .form-control {
            position: relative;
            box-sizing: border-box;
            height: auto;
            padding: 10px;
            font-size: 16px;
        }
        .form-signin .form-control:focus {
            z-index: 2;
        }
        .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
        }
        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
        }

    </style>
</head>
<body class="text-center">
    <form class="form-signin">
        <img class="mb-4" src="https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
        <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
        <label for="inputUsername" class="sr-only">Username</label>
        <input type="username" id="inputUsername" class="form-control" th:placeholder="#{login.username}" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" class="form-control" th:placeholder="#{login.password}" required>
        <div class="checkbox mb-3">
            <label>
                <input type="checkbox" value="remember-me" th:text="#{login.remember}">
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
        <p class="mt-5 mb-3 text-muted">0 2020-2021</p>
        <a class="btn btn-sm">中文</a>
        <a class="btn btn-sm">English</a>
    </form>
    </body>
</div>
</body>
</html>

运行结果

在这里插入图片描述

③ 手动切换语言

实现中英文语言的转换,是页面国际化的体现

1.分析区域信息解析器自动配置

Spring MVC进行国际化时有两个十分重要的对象

  • Locale:区域信息对象
  • LocaleResolver: 区域信息解析器,容器中的组件,负责获取区域信息对象

查看WebMvcAutoConfiguration

找到了一下组件

	@Override
		@Bean
		@ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME)
		//点开LOCALE_RESOLVER_BEAN_NAME,跳到了public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"
		//发现只要容器中有localResolver标识的Bean,默认的LocalResolver就会失效
		public LocaleResolver localeResolver() {
		//查看容器中是否有用户配置的,没有的话就使用默认的
		//查看WebProperties中获得的LocaleResolver是否是固定的LocaleResoler
			if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
				return new FixedLocaleResolver(this.webProperties.getLocale());
			}
			//接收头国际化分解
			AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
			localeResolver.setDefaultLocale(this.webProperties.getLocale());
			return localeResolver;
		}

WebProperties 是Spring MVC的配置类

AcceptHeaderLocaleResolver这个类中有一个方法

@Override
	public Locale resolveLocale(HttpServletRequest request) {
		//获得默认的区域信息对象
		Locale defaultLocale = getDefaultLocale();
		if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
			return defaultLocale;
		}
		//获取请求的区域化信息对象
		Locale requestLocale = request.getLocale();
		//得到所有支持的区域化信息对象
		List<Locale> supportedLocales = getSupportedLocales();
		//返回请求的区域化信息对象
		if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
			return requestLocale;
		}
		//返回寻找到的支持的区域化信息对象
		Locale supportedLocale = findSupportedLocale(request, supportedLocales);
		if (supportedLocale != null) {
			return supportedLocale;
		}
		///三目表达式,如果默认defaultLocale为空,则返回请求的区域对象
		return (defaultLocale != null ? defaultLocale : requestLocale);
	}

我们想要我们的国际化资源生效,需要我们自己的Locale生效。

2.自定义一个LocaleResolver

根据需求来生成一个LocaleResolver,要实现LocaleResolver,代码如下:


public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取携带"accept-language"的请求头
        String language = request.getParameter("l");
        //获得一个默认的locale,当没有请求时使用默认的
        Locale locale = Locale.getDefault();
        //如果请求的language不为空
        if(!StringUtils.isEmpty(language))
        {
            String [] s = language.split("_");
            System.out.println(s[0]);
            System.out.println(s[1]);
            locale =new Locale(s[0],s[1]);
        }
        return  locale;
    }


    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

3.加入我们自定义的LocaleResolver

需要通过组件的形式加入我们自定义的LocaleResolver,代码如下:

@Configuration
public class MyLocaleConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }

    //创建一个国际化信息解析器以组件的形式返回,让我们的SpringMVC能使用自己定义的LocaleResolver
    @Bean
    public LocaleResolver localeResolver()
    {
        return new MyLocaleResolver();
    }
}

注意:我们加入的LocaleResolver,需要Bean的id(也就是方法名)要为localeResolver 。

实现结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值