1. 程式人生 > >[Java中實現國際化] - 配合thymeleaf實現中英文自動切換(多語言)

[Java中實現國際化] - 配合thymeleaf實現中英文自動切換(多語言)

-type charset please menu register times ase http word

MOOC該鏈接第三章第二節

尚矽谷SpringBoot全集 web開發國際化 xjbo (7天,過期可以留言索取)

技術分享圖片

resources下建立文件

上到下為: 默認的,英語(美國),中文(中國)

en

login.btn=Sign In
login.password=PassWord
login.remember=Remember Me
login.tip=Please sign in
login.username=UserName

zh

login.btn=登錄
login.password=密碼
login.remember=記住我
login.tip
=請登錄 login.username=用戶名

頁面:

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

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1"
> <meta name="description" content=""> <meta name="author" content=""> <title>登錄頁面</title> <!-- Bootstrap Core CSS --> <link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- MetisMenu CSS --> <link
href="/vendor/metisMenu/metisMenu.min.css" rel="stylesheet"> <!-- Custom CSS --> <link href="/dist/css/sb-admin-2.css" rel="stylesheet"> <!-- Custom Fonts --> <link href="/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn‘t work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="container"> <div class="row"> <div class="col-md-4 col-md-offset-4"> <div class="login-panel panel panel-default"> <div class="panel-heading"> <h3 class="panel-title" th:text="#{login.tip}">Please sign in</h3> </div> <div class="panel-body"> <form role="form"> <fieldset> <div class="form-group"> <input class="form-control" placeholder="UserName" name="UserName" type="text" th:placeholder="#{login.username}" autofocus> </div> <div class="form-group"> <input class="form-control" placeholder="PassWord" name="PassWord" type="password" th:placeholder="#{login.password}"> </div> <div class="checkbox"> <label> <input name="remember" type="checkbox" value="Remember Me">[[#{login.remember}]] </label> </div> <!-- Change this to a button or input when using this as a form --> <a href="index.html" class="btn btn-lg btn-success btn-block" th:text="#{login.btn}">Login</a> </fieldset> </form> </div> </div> </div> </div> </div> <!-- jQuery --> <script src="/vendor/jquery/jquery.min.js"></script> <!-- Bootstrap Core JavaScript --> <script src="/vendor/bootstrap/js/bootstrap.min.js"></script> <!-- Metis Menu Plugin JavaScript --> <script src="/vendor/metisMenu/metisMenu.min.js"></script> <!-- Custom Theme JavaScript --> <script src="../dist/js/sb-admin-2.js"></script> </body> </html>

別忘記引入thymeleaf依賴以及配置它

pom

        <!--thymeleaf依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

properties

#thymeleaf
spring.thymeleaf.prefix=/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

spring.messages.basename=i18n.login
spring.messages.basename=i18n.register
spring.messages.basename=i18n.other

技術分享圖片

技術分享圖片

待加入非thymeleaf的html. jsp使用方式

[Java中實現國際化] - 配合thymeleaf實現中英文自動切換(多語言)