codeforce # 786 E-Breaking the Wall
阿新 • • 發佈:2022-05-05
頁面交給thymeleaf
先在IDEA中統一設定properties的編碼問題
在resources資原始檔下新建一個i18n目錄
建立一個login.properties檔案,還有一個login_zh_CN.properties;
en_US
這裡idea 可能需要安裝外掛Resource bundles editor
配置yaml檔案
spring:
messages:
basename: i18n.login
html中 前端頁面字型中英通過 thymeleaf獲取的 th:text="#{在login.properties配置的中英鍵}
前端中英切換按鈕 這個還需要去自定義 處理的元件類
<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>
public class MyLocaleResolver implements LocaleResolver { //解析請求 @Override public Locale resolveLocale(HttpServletRequest request) { String language= request.getParameter("l"); Locale locale = Locale.getDefault(); // 如果沒有獲取到就使用系統預設的 //如果請求連結不為空 if (!StringUtils.isEmpty(language)){ //分割請求引數 String[] split = language.split("_"); //國家,地區 locale = new Locale(split[0],split[1]); }return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
在MvcConofig下新增bean
@Configuration public class MyMvcConfig implements WebMvcConfigurer {//自定義配置 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
可以切換
搬運狂神..