<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                國際化就是根據需要能夠將網站的語言切換為當地語言信息,如可以將網站在中英文之間進行切換。 ![](https://img.kancloud.cn/75/7b/757b303ef465cebba75d251a98062b78_654x291.gif) 我們可以實現根據瀏覽器使用的默認語言來實現網站語言的切換,或定制我們的區域信息解析器來實現點擊鏈接切換語言。 <br/> 步驟如下: **1. 明確需要國際化的地方** 假如我需要將`templates/international.html`頁面進行國際化。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <!-- international.title 是下面國際化配置文件的屬性名 --> <title th:text="#{international.title}">國際化</title> </head> <body> <h1 th:text="#{international.location}">當前在中國</h1> <!-- 需要傳遞一個l的參數讓后臺知道要使用的是哪種語言 --> <a th:href="@{/international(l=zh_CN)}">中文</a> <a th:href="@{/international(l=en_US)}">English</a> </body> </html> ``` **2. 編寫國際化配置文件** 國際化配置文件統一使用`.properties`來編寫,命令規則為:`頁面名_語言_國家編碼`,如中文配置文件:`international_zh_CN.properties`、英文(美國)`international_en_US.properties`。 先創建三個空的國際化配置文件,如下圖: ![](https://img.kancloud.cn/cc/e2/cce2c38d9c19f4f690a6cfafedc6cd25_1227x670.png) `international.properties`的作用是當沒有提供對應的國際化配置文件時,默認采用該配置文件。 ![](https://img.kancloud.cn/ef/25/ef25b08cfe17be95c9615f3c1ee7e65d_915x464.png) 最終填寫如下: **`resources/i18n/international.properties`** ```xml international.location=當前在中國~~~ international.title=國際化~~~~ ``` **`resources/i18n/international_zh_CN.properties`** ```xml international.location=當前在中國 international.title=國際化 ``` **`resources/i18n/international_en_US.properties`** ```xml international.location=Currently in the United States international.title=International ``` **3. 指定國際化配置文件的位置** 國際化配置文件默認在`resources`目錄,但是我們更改為`resources/i18n/`目錄,所以需要指定。 **`resources/application.properties`** ```xml # 當你有多個國際化配置文件時用 , 隔開 spring.messages.basename=i18n.login, i18n.international ``` **4. 啟動項目訪問`international.html`頁面** 當你訪問到該頁面,由瀏覽器的默認語言來決定使用哪個配置文件。下圖為我的谷歌瀏覽器默認的語言為簡體中文,所以采用是`international_zh_CN.properties`的配置信息。 ![](https://img.kancloud.cn/90/14/90143c46b240fc9e102af3c1932ff6f1_919x224.png) 上面網站的語言由瀏覽器當前的默認語言決定,但是我們可以在頁面放置鏈接任意在中英文之間切換。 其原理是:可以使用區域信息解析器LocaleResolver來獲取國際化Locale,它的部分源碼如下: ```java -----WebMvcAutoConfiguration----- public LocaleResolver localeResolver() { // 如果沒有指定區域信息,則使用默認的FIXED if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) { return new FixedLocaleResolver(this.mvcProperties.getLocale()); } else { // 如果沒有指定,則從AcceptHeaderLocaleResolver獲取區域信息 AcceptHeaderLocaleResolver localeResolver = ▲▲▲▲new AcceptHeaderLocaleResolver();▲▲▲▲ localeResolver.setDefaultLocale(this.mvcProperties.getLocale()); return localeResolver; } } -----AcceptHeaderLocaleResolver----- public Locale resolveLocale(HttpServletRequest request) { Locale defaultLocale = this.getDefaultLocale(); if (defaultLocale != null && request.getHeader("Accept-Language") == null) { return defaultLocale; } else { // 從請求頭中獲取區域信息 ▲▲▲▲Locale requestLocale = request.getLocale();▲▲▲▲ List<Locale> supportedLocales = this.getSupportedLocales(); if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) { Locale supportedLocale = this.findSupportedLocale(request, supportedLocales); if (supportedLocale != null) { return supportedLocale; } else { return defaultLocale != null ? defaultLocale : requestLocale; } } else { return requestLocale; } } } ``` 在瀏覽器中按F12,找到任意一個請求,可以看到請求頭部的語言信息,我們需要定制自己的信息解析器來解析的請求頭部信息。 ![](https://img.kancloud.cn/cd/4b/cd4bd3d01810271a217e2c3f1854a017_965x396.png) **5. 實現我們自己的區域信息解析器** *`com.example.webresult.component.MyLocaleResolver`* ```java package com.example.webresult.component; import org.apache.tomcat.jni.Local; import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale; public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { // 獲取從前端請求的參數l String l = request.getParameter("l"); // 如果沒有請求參數l,則使用默認區域 Locale local = Locale.getDefault(); if (!StringUtils.isEmpty(l)) { String[] split = l.split("_"); // 創建我們自己的區域,(國家語言, 國家編碼) local = new Locale(split[0], split[1]); } return local; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } } ``` **6. 將定制的區域信息解析器注冊到IoC容器中** 需要將上面定制的區域信息解析器注冊到IoC容器中,讓我們的區域信息解析器起效,覆蓋SpringMVC默認的解析器。 *`com.example.webresult.config.MyMvcConfig`* ```java package com.example.webresult.config; import com.example.webresult.component.MyLocaleResolver; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; // 如果使用@EnableWebMvc則是完全取代該父類的自動配置 // @EnableWebMvc @Configuration public class MyMvcConfig implements WebMvcConfigurer { /** * 將我們的區域解析器注入到IoC容器中,覆蓋SpringMVC默認的解析器 * @return */ @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } } ``` **7. 我編寫的controller如下** *`com.example.webresult.controller.IndexController`* ```java package com.example.webresult.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping(value = "/international") public String international() { return "international"; } } ``` 啟動項目后訪問:http://localhost:8080/international 顯示如下: ![](https://img.kancloud.cn/75/7b/757b303ef465cebba75d251a98062b78_654x291.gif)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看