SpringBoot 国际化的使用

首先在打开HTML 引用Thymeleaf 模板

`

Signin Template for Bootstrap
<body class="text-center">
	<form class="form-signin" action="dashboard.html">
		<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
		<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.btn}">Please sign in</h1>
		<input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
		<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
		<div class="checkbox mb-3">
			<label>
				<input type="checkbox" value="remember-me" >[[#{login.rememberMe}]]
    </label>
		</div>
		<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn2}]]</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>
`

在Resource 里面创建文件夹 i18n
之后在里面创建login.properties以及后面的两个
在这里插入图片描述login.properties里面的信息
在这里插入图片描述
解析器里面把网页传过来的参数l 解析,根据地区进行选择语言
在这里插入图片描述

package com.yhj.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;


//
public class MyLocalResolver implements LocaleResolver {


    // 解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
//        获取请求中的语言参数
        String language = request.getParameter("l");
        Locale locale = Locale.getDefault();// 如果没有就使用默认的
//        如果请求的链接携带了国际化的参数
        if(!StringUtils.isEmpty(language)){
//            zh_CN
            String[] split =language.split("_");
            // 国家  地区
          locale= new Locale(split[0], split[1]);


        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}




视图解析器进行样式加载

在这里插入图片描述

package com.yhj.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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@EnableWebMvc
//如果标注了就会被springmvc全面接管
public class MymvcConfig implements WebMvcConfigurer {

//    样式加载不进来加一句话
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //前端主页控制实现,可以加载样式
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }


//     自定义的国际化组件就生效了
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }



}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot国际化需求是指在Spring Boot应用程序中实现多语言支持的功能。通过国际化,我们可以根据用户的语言设置来展示相应的文本信息,从而提供更好的用户体验。 在实现Spring Boot国际化时,有两种常见的方式。第一种方式是使用Nacos增强Spring Boot国际化,这种方式主要是通过Nacos配置中心来动态更新国际化配置。第二种方式是基于Nacos的动态国际化,这种方式是自己实现一个MessageSource接口的实现类,来读取并缓存国际化配置。 在Spring Boot中,默认的国际化自动装配类是MessageSourceAutoConfiguration,而其默认的实现类是ResourceBundleMessageSource。ResourceBundleMessageSource主要负责读取国际化配置的properties文件并缓存国际化数据。 然而,由于Spring Boot中的默认实现与读取Nacos配置并动态更新的需求不符合,我们需要自己实现一个MessageSource接口的实现类。其中,ReloadableResourceBundleMessageSource是一个可重加载国际化配置文件的实现,它使用两级缓存来提高性能,分别是文件名-国际化配置数据缓存和时区-对应的配置缓存。 因此,根据需求,我们需要实现一个自定义的MessageSource接口的实现类来读取并动态更新Nacos配置的国际化信息。这样,在用户切换语言设置时,我们可以通过重新加载配置文件来实现动态更新国际化文本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Springboot国际化使用Nacos做动态配置](https://blog.csdn.net/lye0530/article/details/129010788)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿岳316

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值