二、多国语言设置

647 阅读1分钟

项目的主页文件是home.html,在thymeleafs里就把它当做一个模板。以下为home.html内容:

<!DOCTYPE html>

<html>

  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
    <link rel="stylesheet" type="text/css" media="all" href="/gtvg/css/gtvg.css" />
  </head>

  <body>
  
   <p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

  </body>

</html>

th:utext="#{home.welcome}"\footnotesize\color{red}\texttt{"\#\{home.welcome\}"}, 红色的代码片段就相当于一个变量,该变量的值来自于你的配置文件(thymeleafexamples-gtvg-3.1-master-jakarta\src\main\webapp\WEB-INF\templates\home.properties\footnotesize\color{red}\texttt{home.properties})。该变量的值会替换掉<p></p>中的内容。

home.properties内容:

    home.welcome=Welcome to our grocery store!
    logo=Good Thymes Virtual Grocery logo
    date.format=MMMM dd'','' yyyy

实现多国语言: 每一个html文件都可以设置一个对应的后缀为properties的配置文件,#{}中变量的取值就从配置文件中来。现在默认的配置文件中的内容是英语,如果想要改为中文,亦或是其他某一个国家的某一种语言,就需要每一种语言对应一个配置文件。例如:西班牙语对应home_es.properties,中文对应home_zh_CN.properties,葡萄牙语对应home_pt.properties。浏览器发送的请求会有国家信息,thymeleaf会自动识别你是来自哪个国家,然后找到对应的配置文件。