SpringBoot實作國際化中英文翻譯
配置IDEA
1.設定編碼為UTF-8

2.在resources目錄下創建 i18n 檔案夾 internationalization簡寫
? 在檔案夾中創建組態檔login.properties
? 再創建login_zh_CN.properties


配置不同語言的properties
en-US 英國(美國)
zh-CN 中文(簡體,中國大陸)
zh-SG 中文(簡體,新加坡)
zh-HK 中文(繁體,香港)
zh-MO 中文(繁體,澳門)
zh-TW 中文(繁體,臺灣)

IDEA同時配多語言
通過MessageSourceAutoConfiguration這個類
@Bean
public MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
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;
}
進入MessageSourceProperties 得到需要配置 message

在application.properties中配置message 這里需要配置我們默認的位置
在i18n下的login
#我們的組態檔的真實位置
spring.messages.basename=i18n.login
HTML中對鏈接進行請求引數
<a th:href="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{index.html(l='en_US')}">English</a>
<a th:href="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{index.html(l='zh_CN')}">中文</a>
參考原始碼:AcceptHeaderLocaleResolver類中的重寫方法,創建地域決議器
public class MyLocaleResolver implements LocaleResolver {
//決議請求
@Override
public Locale resolveLocale(HttpServletRequest request) {
//獲得請求中的語言引數
String language = request.getParameter("l");
Locale locale = Locale.getDefault();//如果沒有就使用默認的
//如果請求的鏈接攜帶了國際化引數
if(!StringUtils.isEmpty(language)){
//zh_CN
String[] split = language.split("_");
//國家,地區
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
在config中通過Bean 注入
//自定義國際化組件生效
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
HTML
初始Html
<!DOCTYPE html>
<html lang="en">
<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 href="https://www.cnblogs.com/class資源/zzz/asserts/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="https://www.cnblogs.com/class資源/zzz/asserts/css/signin.css" rel="stylesheet">
</head>
<body >
<form action="../../../../../../../class資源/zzz/dashboard.html">
<img src="https://www.cnblogs.com/class資源/zzz/asserts/img/bootstrap-solid.svg" alt="" height="72">
<h1 >Please sign in</h1>
<label >Username</label>
<input type="text" placeholder="Username" required="" autofocus="">
<label >Password</label>
<input type="password" placeholder="Password" required="">
<div >
<label>
<input type="checkbox" value="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/remember-me"> Remember me
</label>
</div>
<button type="submit">Sign in</button>
<p >? 2017-2018</p>
<a >中文</a>
<a >English</a>
</form>
</body>
</html>
thymeleaf修飾后的HTML
thymeleaf官方檔案
- Simple expressions:
- Variable Expressions:
${...} - Selection Variable Expressions:
*{...} - Message Expressions:
#{...} - Link URL Expressions:
@{...} - Fragment Expressions:
~{...}
- Variable Expressions:
- Literals
- Text literals:
'one text','Another one!',… - Number literals:
0,34,3.0,12.3,… - Boolean literals:
true,false - Null literal:
null - Literal tokens:
one,sometext,main,…
- Text literals:
- Text operations:
- String concatenation:
+ - Literal substitutions:
|The name is ${name}|
- String concatenation:
- Arithmetic operations:
- Binary operators:
+,-,*,/,% - Minus sign (unary operator):
-
- Binary operators:
- Boolean operations:
- Binary operators:
and,or - Boolean negation (unary operator):
!,not
- Binary operators:
- Comparisons and equality:
- Comparators:
>,<,>=,<=(gt,lt,ge,le) - Equality operators:
==,!=(eq,ne)
- Comparators:
- Conditional operators:
- If-then:
(if) ? (then) - If-then-else:
(if) ? (then) : (else) - Default:
(value) ?: (defaultvalue)
- If-then:
- Special tokens:
- No-Operation:
_
- No-Operation:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" 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="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{/css/signin.css}" rel="stylesheet">
</head>
<body >
<form action="dashboard.html">
<img th:src="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{/img/bootstrap-solid.svg}" alt="" height="72">
<h1 th:text="#{login.tip}">Please sign in</h1>
<label th:text="#{login.username}">Username</label>
<input type="text" th:placeholder="#{login.username}" required="" autofocus="">
<label th:text="#{login.password}">Password</label>
<input type="password" th:placeholder="#{login.password}" required="">
<div >
<label>
<input type="checkbox" value="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/remember-me" > [[#{login.remember}]]
</label>
</div>
<button type="submit" th:text="#{login.btn}">Sign in</button>
<p >? 2017-2018</p>
<a th:href="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{index.html(l='zh_CN')}">中文</a>
<a th:href="https://www.cnblogs.com/wakanda-forever/archive/2023/03/16/@{index.html(l='en_US')}">English</a>
</form>
</body>
</html>


-
1.首頁配置:所有靜態資源都需要使用thymeleaf接管;
- 2.url @{}
-
2頁面國際化:
- 我們需要配置i18n檔案
- 我們在專案中需要進行按鍵自動切換,我們需要自定義一個組件
LocaleResolver - 記得將自己寫的組件配置到spring容器中
@Bean -
{}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547118.html
標籤:其他
上一篇:性能優化搞得好,Tomcat少不了。| 博學谷狂野架構師
下一篇:obs錄屏核心流程分析
