笔记

万物寻其根,通其堵,便能解其困。
  博客园  :: 新随笔  :: 管理

spring boot中国际化处理/多语言适配

Posted on 2024-06-12 11:07  草妖  阅读(7)  评论(0)    收藏  举报

*\FirstSBDemo\pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <groupId>com.namejr</groupId>
    <artifactId>FirstSBDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.14</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.11</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                    <include>**/*.json</include>
                    <include>**/*.txt</include>
                    <include>**/*.mp3</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <!-- 注册webapp目录为资源目录 -->
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                </configuration>
            </plugin>
            <plugin>
                <!-- 配置jar包打包工具 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${project.basedir}/libs</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

*\FirstSBDemo\src\main\java\com\namejr\base\DealInterNationalConfig.java

package com.namejr.base;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

@Configuration
public class DealInterNationalConfig {
    @Bean
    public MessageSource messageSource(){
        ReloadableResourceBundleMessageSource tempMessageSource=new ReloadableResourceBundleMessageSource();
        // 注:message资源包其实用户创建的目录是不存在的,在idea中会自动显示处理
        tempMessageSource.setBasename("classpath:i18n/message");
        tempMessageSource.setDefaultEncoding("UTF-8");
        return tempMessageSource;
    }
}

*\FirstSBDemo\src\main\java\com\namejr\controller\PublicController.java

package com.namejr.controller;

import com.namejr.serviceImpl.PublicServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
@RequestMapping(value = "/api/public")
@Validated
public class PublicController {
    @Autowired
    private PublicServiceImpl pServiceImpl;
    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/getServerTime", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public String getServerTime() {
        return DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
    }

    @RequestMapping(value = "/getInterNationalMsg", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public String getInterNationalMsg() {
        // 获取自定义语言
        Locale locale =Locale.US;
        // 获取本地配置语言
        // Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage("titleMsg",null,locale)+"\r\n"+messageSource.getMessage("contentMsg",null,locale);
    }
}

*\FirstSBDemo\src\main\resources\i18n\message_zh_CN.properties

titleMsg=这是标题
contentMsg=这是内容

*\FirstSBDemo\src\main\resources\i18n\message_en_US.properties

titleMsg=This is the title.
contentMsg=This is the content.

注:配置文件(.propertirs)的格式是:前缀+"_"+语言+".properties"。1、前缀不一定固定的是message,但是一定要和DealInterNationalConfig中的*.setBasename("classpath:i18n/message");设置一致;2、不知道语言的话,可以直接打开浏览器的开发人员工具--网络那一栏随便进行一个请求,然后查看请求头中的"Accept-Language"即可,需要注意的是,显示的是"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"中的语言"zh-CN"需要将横线"-"换成下横线"_"

访问地址:127.0.0.1:8080/api/public/getInterNationalMsg