【WEEK11】 【DAY1】Employee Management System Part 2【English Version】

2024.5.6 Monday
Continuing from 【WEEK10】 【DAY2】Employee Management System Part 1【English Version】

Contents

  • 10.3. Page Internationalization
    • 10.3.1. Preparation
    • 10.3.2. Configuration File Writing
      • 10.3.2.1. Create an i18n (abbreviation for internationalization) directory under the resources folder
      • 10.3.2.2. Create the files login.properties and login_zh_CN.properties in the i18n folder
      • 10.3.2.3. Import Configuration Files
      • 10.3.2.4. Add Key-Value Pairs
    • 10.3.3. Configuration File Activation Exploration
    • 10.3.4. Configuring Page Internationalization Values
      • 10.3.4.1. Modify the index.html page
    • 10.3.5. Configuring Internationalization Resolution
      • 10.3.5.1. In Spring, there is an internationalization Locale (regional information object)
      • 10.3.5.2. Go to the webmvc auto-configuration file
      • 10.3.5.3. Modify the index.html
      • 10.3.5.4. Create MyLocaleResolver.java
      • 10.3.5.5. Modify MyMvcConfig.java
      • 10.3.5.6. Restart and Refresh
    • 10.3.6. Summary

10.3. Page Internationalization

Sometimes, our website will involve switching between Chinese and English or even multiple languages. That’s when we need to learn about internationalization!

10.3.1. Preparation

First, set the encoding for properties files to UTF-8 in IDEA! (File | Settings | Editor | File Encodings)
Change them all to UTF-8
Insert image description here
Write the internationalization configuration file to extract the internationalized page messages that need to be displayed on the page. We can go to the login page to see what content we need to write internationalization configurations for!

10.3.2. Configuration File Writing

10.3.2.1. Create an i18n (abbreviation for internationalization) directory under the resources folder

To store internationalization configuration files

10.3.2.2. Create the files login.properties and login_zh_CN.properties in the i18n folder

IDEA will automatically recognize and generate the new folder
Insert image description here

*If these two files are not automatically merged: you need to download the Resource Bundle Editor plugin
Insert image description here
Insert image description here

10.3.2.3. Import Configuration Files

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Find that login_en_US.properties was generated
Insert image description here

10.3.2.4. Add Key-Value Pairs

Click Resource Bundle to enter a visual interface
Insert image description here
Insert image description here

Similarly
Insert image description here

Finally, five sets of values were added, and the corresponding files will automatically generate code:
login.properties

login.button=Login
login.password=Password
login.remember=Remember Me
login.tip=Please Login
login.username=Username

login_en_US.properties

login.button=Sign in
login.password=Password
login.remember=Remember me
login.tip=Please sign in
login.username=Username

login_zh_CN.properties

login.button=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名

Insert image description here

10.3.3. Configuration File Activation Exploration

Let’s take a look at SpringBoot’s autoconfiguration for internationalization! Here it involves a class: MessageSourceAutoConfiguration.java
There is a method inside, and here we find that SpringBoot has already automatically configured a component to manage our internationalization resource files, ResourceBundleMessageSource

@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {return new MessageSourceProperties();
}// Get the values passed from properties to make judgments
@Bean
public MessageSource messageSource(MessageSourceProperties properties) {ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();if (StringUtils.hasText(properties.getBasename())) {
// Set the base name of the internationalization file (excluding the language and country code)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;
}

As the actual configuration is placed in the i18n directory, it is necessary to configure the path for these messages -> modify the application.properties file

spring.application.name=springboot-03-web2
#spring.mvc.favicon.enabled=false this method is already deprecated#Turn off the cache of the template engine
spring.thymeleaf.cache=false#Modify the access URL, at this time to access the homepage, the URL that needs to be entered has changed to: http://localhost:8080/111/
#server.servlet.context-path=/111#Modify the recognition address of basename (from the default MessageSourceProperties file to i18n.login)
spring.messages.basename=i18n.login

10.3.4. Configuring Page Internationalization Values

To retrieve internationalization values on the page, consult the Thymeleaf documentation to find that the message retrieval operation is: #{…}.

10.3.4.1. Modify the index.html page

Lines to be modified: 18~22, 25, 28.

<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">Sign in</button>

After restart, the login page:
Insert image description here

10.3.5. Configuring Internationalization Resolution

Goal: Automatically switch between Chinese and English according to the button

10.3.5.1. In Spring, there is an internationalization Locale (regional information object)

There is a resolver called LocaleResolver (to obtain regional information object)!

10.3.5.2. Go to the webmvc auto-configuration file

Find the default SpringBoot configuration:
Line466, click line473 AcceptHeaderLocaleResolver to jump,

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {// If there is no container, configure it yourself; if there is, use the user's configurationif (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver(this.mvcProperties.getLocale());}// Accept header internationalization breakdownAcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();localeResolver.setDefaultLocale(this.mvcProperties.getLocale());return localeResolver;
}

It can be seen that there is a method on line19

public Locale resolveLocale(HttpServletRequest request) {Locale defaultLocale = this.getDefaultLocale();// The default is to obtain Locale for internationalization based on the regional information brought by the request headerif (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;}}
}

-> If we now want to click on a link to make our internationalization resources take effect, we need to make our own Locale take effect -> try to overwrite it.

10.3.5.3. Modify the index.html

Lines 30, 31

<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>

10.3.5.4. Create MyLocaleResolver.java

Insert image description here

package com.P14.config;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 {// Resolve the request@Overridepublic Locale resolveLocale(HttpServletRequest request) {// Get the language parameter from the requestString language = request.getParameter("l");// Use the default if no custom one is definedLocale locale = Locale.getDefault();// If the request link carries internationalization parametersif (!StringUtils.isEmpty(language)){// Split the request parametersString[] split = language.split("_");// Country, regionlocale = new Locale(split[0],split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {}
}

10.3.5.5. Modify MyMvcConfig.java

package com.P14.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
//@EnableWebMvc   // This imports a class, DelegatingWebMvcConfiguration, which acquires all the webMvcConfig from the container
public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// Redirect the following two URLs to the index page fileregistry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");}// Make the custom internationalization component effective@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}
}

10.3.5.6. Restart and Refresh

Chinese:
Insert image description here

English:
Insert image description here

10.3.6. Summary

  1. Homepage configuration:
    • Note that all pages’ static resources need to be managed by Thymeleaf
    • URL: @{}
  2. Page Internationalization
    • We need to configure i18n files
    • If we want to automatically switch buttons in the project, we need to define a component LocaleResolver
    • Remember to configure the components you write into the Spring container with @Bean
    • #{}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/833411.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

专业的保密网文件导入导出系统,让文件流转行为更可控安全

军工单位因其涉及国防安全和军事机密&#xff0c;对保密工作有极高的要求&#xff0c;通常会采取严格的网络隔离措施来保护敏感信息和提高网络安全性。常见的方式是通过物理隔离将网络彻底分隔开来&#xff0c;比如保密网和非保密网。网络隔离后&#xff0c;仍有数据交换的需求…

GORM的常见命令

文章目录 一、什么是GORM&#xff1f;二、GORM连接mysql以及AutoMigrate创建表三、查询1、检索此对象是否存在于数据库&#xff08;First,Take,Last方法&#xff09;2、Find()方法检索3、根据指定字段查询 四、更新1、Save() 保存多个字段2、更新单个字段 五、删除 一、什么是G…

Python中设计注册登录代码

import hashlib import json import os import sys # user interface 用户是界面 UI """ 用户登录系统 1.注册 2.登陆 0.退出 """ # 读取users.bin def load(path): return json.load(open(path, "rt")) # 保存user.bin def save(dic…

Figma 高效技巧:设计系统中的图标嵌套

Figma 高效技巧&#xff1a;设计系统中的图标嵌套 在设计中&#xff0c;图标起着不可或缺的作用。一套便捷易用的图标嵌套方法可以有效提高设计效率。 分享一下我在图标嵌套上走过的弯路和经验教训。我的图标嵌套可以分三个阶段&#xff1a; 第一阶段&#xff1a;建立图标库 一…

目标检测实战(八): 使用YOLOv7完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)

文章目录 一、目标检测介绍二、YOLOv7介绍三、源码/论文获取四、环境搭建4.1 环境检测 五、数据集准备六、 模型训练七、模型验证八、模型测试九、错误总结9.1 错误1-numpy jas mp attribute int9.2 错误2-测试代码未能跑出检测框9.3 错误3- Command git tag returned non-zero…

Apple OpenELM设备端语言模型

Apple 发布的 OpenELM&#xff08;一系列专为高效设备上处理而设计的开源语言模型&#xff09;引发了相当大的争论。一方面&#xff0c;苹果在开源协作和设备端AI处理方面迈出了一步&#xff0c;强调隐私和效率。另一方面&#xff0c;与微软 Phi-3 Mini 等竞争对手相比&#xf…

森林消防新利器:高扬程水泵的革新与应用/恒峰智慧科技

随着全球气候变化的加剧&#xff0c;森林火灾的频发已成为威胁生态安全的重要问题。在森林消防工作中&#xff0c;高效、快速的水源供给设备显得尤为重要。近年来&#xff0c;高扬程水泵的广泛应用&#xff0c;为森林消防工作带来了新的希望与突破。 一、高扬程水泵的技术优势 …

探索Baidu Comate:编程世界中的新利器

文章目录 Baidu Comate 介绍Baidu Comate的优势Baidu Comate安装过程Baidu Comate实战演练代码调优代码解释代码生成注释生成 总结 Baidu Comate 介绍 随着GPT的大火&#xff0c;衍生了各种AI工具&#xff0c;这些AI工具遍布在各行业各领域中&#xff0c;有AI写作、AI办公、AI…

向各位请教一个问题

这是菜鸟上的一道题目&#xff0c;单单拿出来问问大家&#xff0c;看看能不能解惑 &#xff0c;谢谢各位&#xff01; 题目25&#xff1a;求12!3!...20!的和 解题思路&#xff1a;这个题不知道为什么我用DEV C 5.11显示出来为0.000000&#xff0c;可能版本有问题&#xff1f;&a…

jenkins部署服务到windows系统服务器

1、安装openSSH windows默认不支持ssh协议&#xff0c;需要下载安装&#xff0c;主要适用于jenkins传输文件已经执行命令使用 点击查看下载openSSH 2、项目配置 这里简单说说怎么配置&#xff0c;主要解决点就是ssh执行cmd或shell命令时不能开启新窗口导致应用部署失败或者断…

【论文阅读笔记】MAS-SAM: Segment Any Marine Animal with Aggregated Features

1.论文介绍 MAS-SAM: Segment Any Marine Animal with Aggregated Features MAS-SAM&#xff1a;利用聚合特征分割任何海洋动物 Paper Code(空的) 2.摘要 最近&#xff0c;分割任何模型&#xff08;SAM&#xff09;在生成高质量的对象掩模和实现零拍摄图像分割方面表现出卓越…

海云安受邀参加诸子云 4.27南京「金融互联网」私董会

4月27日&#xff0c;“安在新媒体网安用户行业活动”第四期私董会在南京顺利举办。活动以“金融&互联网”为主题&#xff0c;邀请十余位业内资深的甲方用户以及典型厂商代表。摒弃传统的议题分享&#xff0c;采取“随时问答&#xff0c;自由讨论”的形式&#xff0c;提问题…

Poisson_Image-Editing

1.算法介绍 快速泊松图像编辑&#xff08;Fast Poisson Image Editing&#xff09;是一种图像处理算法&#xff0c;用于将源图像的某个区域无缝地嵌入到目标图像中。它基于泊松方程的性质&#xff0c;通过求解离散化的泊松方程来实现图像的融合。该算法的核心思想是&#xff0c…

信息系统项目管理师0092:项目管理原则(6项目管理概论—6.4价值驱动的项目管理知识体系—6.4.1项目管理原则)

点击查看专栏目录 文章目录 6.4价值驱动的项目管理知识体系6.4.1项目管理原则1.原则一:勤勉、尊重和关心他人2.原则二:营造协作的项目管理团队环境3.原则三:促进干系人有效参与4.原则四:聚焦于价值5.原则五:识别、评估和响应系统交互6.原则六:展现领导力行为7.原则七:根…

在家中访问一个网站的思考

在家中访问一个网站的思考 1、家庭网络简介2、家庭WLAN DHCP2.1、家庭路由器PPPOE拨号2.2、DHCP&#xff08;动态主机配置协议&#xff09;2.3、接入家庭网的主机IP地址2.4、家庭总线型以太网2.5、Mac地址2.6、ARP协议2.7、IP协议 & UDP/TCP协议2.8、NAT&#xff08;Netwo…

沙盘Sandboxie v5.56.4

菜鸟高手裸奔工具沙盘Sandboxie是一款国外著名的系统安全工具&#xff0c;它可以让选定程序在安全的隔离环境下运行&#xff0c; 只要在此环境中运行的软件&#xff0c;浏览器或注册表信息等都可以完整的进行清空&#xff0c;不留一点痕迹。同时可以防御些 带有木马或者病毒的…

OpenHarmony usb打开报错“usb fail error code = -3, error msg = LIBUSB_ERROR_ACCESS”

一、前言&#xff1a;最近公司项目需求&#xff0c;定位要求使用国产系统&#xff0c;国产系统无非就是 统信os &#xff0c;麒麟OS, 还有这两年比较热的 OpenHarmony。于是&#xff0c;老板要求公司产品适配OpenHarmony , 跟上时代步伐。 二、在开发中使用 usb 通讯时&#x…

Unity射击游戏开发教程:(12)使用后处理

后处理 后期处理是向您的游戏场景添加一个或多个滤镜,确实可以为您的游戏提供精美的外观。在本文中,我们将讨论如何在 Unity 中设置后处理系统,从那里您可以探索和试验 Unity 提供的所有过滤器。 首先,我们需要从包管理器添加后处理器堆栈。包管理器是 Unity 产品的集合,…

淘宝数据分析——Python爬虫模式♥

大数据时代&#xff0c; 数据收集不仅是科学研究的基石&#xff0c; 更是企业决策的关键。 然而&#xff0c;如何高效地收集数据 成了摆在我们面前的一项重要任务。 本文将为你揭示&#xff0c; 一系列实时数据采集方法&#xff0c; 助你在信息洪流中&#xff0c; 找到…

怎么在家访问公司内网?

在当前的疫情情况下&#xff0c;越来越多的公司开始允许员工在家办公&#xff0c;这就需要解决一个问题&#xff1a;如何在家访问公司的内网资源呢&#xff1f;今天我将介绍一种解决方案——使用【天联】组网&#xff0c;它具有许多优势。 【天联】组网的优势 无网络限制&#…