SpringBoot复习:(36)国际化

一、Resources目录下建立一个目录(比如international)来存储资源文件

message.properties

空的,但不能没有

message_zh_CN.properties

复制代码
hello=您好

message_en_us.properties

复制代码
hello=hello world

二、自动配置类MessageSourceAutoConfiguration

常量MESSAGE_SOURCE_BEAN_NAME为messageSource,也就是有这个名字的bean,则自动配置失效。

因为有@Conditional(ResourceBundleCondition)注解,

还要满足ResourceBundleCondition这个类的match方法返回true,自动配置才会生效

默认加载的资源文件为resources目录下的messages.properties,有这个文件match返回true,否则返回false.

还可以在application.properties中配置spring.messages.basename来指定国际化资源文件的位置,如

spring.messages.basename=international.message

条件满足后,MessageSourceAutoConfiguration自动配置一个Message Source bean

三、有了Message Resource,我们还需要LocaleResolver来对Message Resource进行解析

WebMvcAutoConfiguration中配置了一个LocalResolver bean

当没有配置LOCAL_RESOLVER_BEAN_NAME(常量值为localeResolver)这个bean时,自动配置的这个LocaleResolver生效

四、controller中返回国家化信息

复制代码
package cn.edu.tju.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
public class InternationalController {
    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/int", produces = "txt/html;charset=utf-8")
    public String getInt(){
        return messageSource.getMessage("hello", null, Locale.SIMPLIFIED_CHINESE);
    }

    @RequestMapping("/int2")
    public String getInt2(){
        return messageSource.getMessage("hello", null, Locale.US);
    }

    @RequestMapping("/int3")
    public String getInt3(){
        return messageSource.getMessage("hello", null, LocaleContextHolder.getLocale());
    }
}

国际化时,@RequestMapping注解要加produces来设置编码来防止乱码。

####################################################

可以自定义LocaleResolver来覆盖WebMvcAutoConfiguration中自动装配的LocaleResolver

首先自定义WebMvcConfigurer来添加LocaleChangeInterceptor拦截器

复制代码
package cn.edu.tju.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("YES!!!!!!!!!!!!!!!!!");
        //添加拦截器
        registry.addInterceptor(new MyInterceptor())
                //.addPathPatterns("/api")
                .excludePathPatterns("/test");
        registry.addInterceptor(new LocaleChangeInterceptor())
                .addPathPatterns("/**");
    }



}

其次,配置一个CookieLocaleChangeResolver

复制代码
package cn.edu.tju.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class CookieResolverConfig {
    @Bean(name="localeResolver")
    public CookieLocaleResolver getResolver(){
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setCookieMaxAge(60*60*1000);
        cookieLocaleResolver.setCookieName("myLocale");
        return cookieLocaleResolver;
    }


}

请求中的locale这个参数会被LocaleChangeInterceptor拦截,

它最终会把请求参数对应的Locale设置到我们所配置的CookieLocaleResolver,这样同样也为国际化做好了准备

相关推荐
WenGyyyL30 分钟前
《Android 应用开发基础教程》——第十三章:权限管理机制与运行时权限请求(以拍照/存储为例)
android·java·权限·极限编程
yy鹈鹕灌顶3 小时前
LeetCode 字符串类题目解析与 Java 实现指南(深度优化版)
java·开发语言·算法·leetcode
这里是小悦同学呀!4 小时前
python学习day2
java·python·学习
菠萝崽.5 小时前
RabbitMQ高级篇-MQ的可靠性
java·分布式·后端·消息队列·rabbitmq·异步编程
万叶学编程6 小时前
@RequestParam 和 @RequestBody、HttpServletrequest 与HttpServletResponse
java
zfj3217 小时前
H2数据库源码学习+debug, 数据库 sql、数据库引擎、数据库存储从此不再神秘
java·数据库·sql·学习·数据库底层原理
编程、小哥哥8 小时前
Java面试实战:从Spring Boot到分布式缓存的深度探索
java·spring boot·redis·微服务·grpc·缓存技术·面试技巧
在未来等你8 小时前
互联网大厂Java求职面试:Spring AI与大模型交互的高级模式与自定义开发
java·微服务·云原生·大模型·spring ai
androidwork8 小时前
Android Kotlin权限管理最佳实践
android·java·kotlin
sakoba8 小时前
Tomcat简述介绍
java·tomcat