SpringBoot之web国际化

1、新建项目

##新建项目我们选择建SpringBoot项目的创建,选择web和thymeleaf模板
在这里插入图片描述

接下来我们来看web的国际化

1、我们把我们需要的页面放到templates目录下

2、我们来建我们的标准文件

login.properties
login_en_US.properties
login_zh_CN.properties
我们来编写我们的这三个文件

打开我们的三个文件中的一个,点击Resource Bundle
在这里插入图片描述
进入到如下页面
在这里插入图片描述
这样我们就可以同时修改三个文件了,依据我的login文件我们要添加的标签为
在这里插入图片描述
我们来修改login页面

<!DOCTYPE html>
<!-- 引入thymeleaf约束 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<title>Signin Template for Bootstrap</title>
		<!-- Bootstrap core CSS -->
		<!-- 在这里我引用的是webjars下的bootstrap,我们需要导入一下maven依赖-->
		<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link href="asserts/css/signin.css" th:href="@{/css/signin.css}" rel="stylesheet">
	</head>

	<body class="text-center">
		<form class="form-signin" action="dashboard.html">
			<img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
			<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" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
			<label class="sr-only" th:text="#{login.password}">Password</label>
			<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
			<div class="checkbox mb-3">
				<label>
				<-- input标签这么取值-->
          <input type="checkbox" value="remember-me"> [[#{login.remember}]]
        </label>
			</div>
			<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
			<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
			<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>
		</form>

	</body>

</html>

css文件

html,
body {
  height: 100%;
}

body {
  display: -ms-flexbox;
  display: -webkit-box;
  display: flex;
  -ms-flex-align: center;
  -ms-flex-pack: center;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  padding-top: 40px;
  padding-bottom: 40px;
  /*background-color: #f5f5f5;*/
}

.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.form-signin .checkbox {
  font-weight: 400;
}
.form-signin .form-control {
  position: relative;
  box-sizing: border-box;
  height: auto;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

webjars的maven依赖

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>4.0.0</version>
</dependency>

3、我们见一个类来配置我们页面的加载路径

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

 @Bean
 public  WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
     WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {


         @Override
         public void addViewControllers(ViewControllerRegistry registry) {
             registry.addViewController("/").setViewName("login");
             registry.addViewController("index.html").setViewName("login");
         }
     };
     return  webMvcConfigurerAdapter;
 }

4、我们再建一个类重写来实现我们自己的国际化资源调用

public class MyLocaleResolver implements LocaleResolver {
 @Override
 public Locale resolveLocale(HttpServletRequest request) {
     String l = request.getParameter("l");
     Locale locale = Locale.getDefault();
     if(!StringUtils.isEmpty(l)){
         String[] split = l.split("_");
         locale = new Locale(split[0],split[1]);
     }
     return locale;
 }

我们将这个类注入到我们的组件中

 @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

5、启动项目

在这里插入图片描述
为什么会这样?是因为我们的那个个资源文件没有被读取
那么我们需要配置他的路径,因为我们不进行配置,他会加载默认的路径

spring.messages.basename=

把我们的三个文件所在位置配上,中间使用·隔开
我们现在来看一下
在这里插入图片描述
正常了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring Boot项目的国际化(i18n)支持,你可以按照以下步骤进行配置: 1. 在 `src/main/resources` 目录下创建一个名为 `messages.properties` 的文件,用于存储默认的文本消息。 2. 在 `messages.properties` 文件中,添加需要国际化的文本消息,例如: ``` greeting.message=Hello! ``` 3. 创建其他语言的资源文件,例如 `messages_en.properties`(英语)、`messages_fr.properties`(法语)等,并在这些文件中提供对应语言的翻译。例如: ``` greeting.message=Bonjour! ``` 4. 在 Spring Boot 的配置文件(通常是 `application.properties` 或 `application.yml`)中添加以下配置: - 对于 `.properties` 文件: ``` spring.messages.basename=messages ``` - 对于 `.yml` 文件: ``` spring: messages: basename: messages ``` 5. 在需要使用国际化文本的地方,使用 `@Value` 注解来注入对应的消息。例如,在一个控制器类中: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { @Value("${greeting.message}") private String greetingMessage; @GetMapping("/greeting") public String greeting() { return greetingMessage; } } ``` 6. 运行应用程序,访问 `/greeting` 路径,将会根据请求头中的 `Accept-Language` 自动返回对应语言的文本消息。 这是一个简单的示例,你可以根据实际需求进行更复杂的国际化配置和使用。希望对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值