文章目录
什么是I18N 🌼
🌐i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。 在全球化的时代,国际化尤为重要,因为产品的潜在用户可能来自世界的各个角落。通常与i18n相关的还有L10n(“本地化”的简称)。
一般来说,现在很多的网站都会提供语言的切换这个功能,而且对于网站的设计来说,这个功能也是非常的重要的,它主要提供的功能就是“方便不同国家的人都看得懂”,只要你完成了这个功能,那么就非常ok,这就来补一补这篇博客(最近太忙了,私信都没来得及回,在这里说一声抱歉啦,有空我就会回的😉😉😉)
如何使用🌻
如果不喜欢看源码的朋友,可以适当跳过一下哈!
这里使用的编译工具是idea
在使用之前,先下载安装一个插件
这个插件可以让我们后续在实现的时候实现可视化的这样的一个操作,还是很方便的🌭 不安装也没问题🥗
对idea的配置,如果不配置的话,之后展示的页面必将产生乱码问题,打开setting,找到File Encoding:
🌽🌽🌽插一句:
🔺这段时间最大的感受就是:spring boot对于MVC的扩展,在WebMvcAutoConfiguration类中,几乎它所有的方法都是可以被人为的自定义配置,我自认为这就像一种“思想”
源码解析🌿
在WebMvcAutoConfiguration中,存在localeResolver()类
@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;
}
代码都很好理解,if有LocaleResolver那就调用用户自己创建的,如果没有那就new一个默认的,那么是如何new的呢?
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
点进AcceptHeaderLocaleResolver:
发现它继承了LocaleResolver,那么LocaleResolver就是我们要找的接口了,如果想要自己定义,那么你也写一个类然后实现这个接口即可
实战🌱
展示index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>首页</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<input type="text" class="form-control" th:placeholder="#{login.Username}" required="" autofocus="">
<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[ #{login.Remember} ]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<!-- 这里传入参数不需要使用 ?使用 (key=value)-->
<a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">English</a>
</form>
</body>
</html>
编写多语言国际化文件以及配置文件
这里有两种方式,这里展示其中一种,另一种的话呢我在这个系列的二版本再给大家展示啦
这里解释一下:
login.properties:就是自定义默认语言配置文件
en_US:自定义英语国际化文件
zh_CN:自定义中文国际化文件
自定义MyLocalResolver
package com.xmonster.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;
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String lang = request.getParameter("lang");
String header = request.getHeader("Accept-Language");
Locale locale = null;
if(StringUtils.hasLength(lang)){
String[] split = lang.split("_");
locale = new Locale(split[0], split[1]);
}else{
String[] split1 = header.split(",");
String[] split = split1[0].split("-");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
application.yaml
MyMvcConfig
综合一下~🍻
package com.xmonster.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
//怎么测试?------->DispatcherServlet
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
// 试图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
// 自定义的国际化组件就生效了!!!
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
}
好啦,分享到这里🙌💜