目录
1.国际化是什么?
国际化(internationalization):是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。
- 而在程序中写网页则需要使用国际化进行配置不同的请求语言
2.设置国际化配置文件
在resources(配置文件中)中创建名为:i18n(国际化的缩写他的英文字母有18个)的包如:
在其中进行创建配置文件:
- login.properties (表示默认语言)
- login_en_US.properties (表示英语)
- login_zh_CN.properties (表示中文)
注意:这里IDE会自动进行合并文件在一个包中选择OK就好如:
在这些配置文件中在进行网页上的文字配制如(login_zh_CN.properties):
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.Username=用户名
login.English=英语
还需要配置以下SpringBoot的配置文件application.yml如:
spring:
messages:
basename: i18n.login
页面:
<!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>Signin Template for Bootstrap</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">
<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">[[#{login.tip}]]</h1>
<label class="sr-only" th:text="#{login.Username}">Username</label>
<label>
<input type="text" class="form-control" placeholder="Username" required="" autofocus="">
</label>
<label class="sr-only" th:text="#{login.password}">Password</label>
<label>
<input type="password" class="form-control" placeholder="Password" required="">
</label>
<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" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
</form>
</body>
</html>