springboot国际化使用

对于很多门户网站,可能有很多客户来源于其他国家,这时就需要使用国际化来进行对外的交流。那么,在Spring Boot项目中是如何使用国际化的呢?

接下来使用一个小例子介绍SpringBoot项目如何运用国际化。

本节使用的依赖文件与3.5节使用Thymeleaf所使用的依赖文件以及配置文件完全一致,

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--        取出html严格校验-->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>

server.port=8089
#缓存是否开启,开发时建议关闭
spring.thymeleaf.cache=false
#编码格式
spring.thymeleaf.encoding=UTF-8
#thymeleaf对html的校验很严格,用这个去除严格校验
spring.thymeleaf.mode=LEGACYHTML5
#模板文件前缀
spring.thymeleaf.prefix=classpath:/templates/
#模板文件后缀
spring.thymeleaf.suffix=.html

Spring Boot在默认情况下是支持国际化使用的,首先需要在src/main/resources下新建国际化资源文件,这里为了举例说明,分别创建如下三个文件:

message = 欢迎使用国际化1

messages_en_US.properties(英文配置),内容如代码

message = welcome to internationalization(English)

messages_zh_CN.properties(汉语配置),内容如代码

message = \u6b22\u8fce\u4f7f\u7528\u56fd\u9645\u5316\uff08\u4e2d\u6587\uff09

然后就到了国际化的重头戏,需要进行i18n的配置,这里新建配置类i18nConfig,这个类需要继承WebMvcConfigurerAdapter类。其中,在localeResolver()方法中设置默认使用的语言类型,在localeChangeInterceptor()方法中设置识别语言类型的参数,并且从继承类中实现addInterceptors()方法,用于拦截localeChangeInterceptor()方法,进而实现国际化。i18nConfig类代码如代码

package com.shrimpking.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/12/21 11:32
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class i18nConfig extends WebMvcConfigurerAdapter
{
    @Bean
    public LocaleResolver localeResolver(){
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        //设置默认语言
        sessionLocaleResolver.setDefaultLocale(Locale.US);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        //参数名,用于区别使用的语言类型
        localeChangeInterceptor.setParamName("lang");
        return  localeChangeInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

改造默认生成的启动类,在类上加入SpringMVC注解@Controller,注入MessageSource类获取国际化资源,并且创建方法返回资源文件对应的数据,返回到前台。新增代码如代码

package com.shrimpking;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Locale;

@Controller
@SpringBootApplication
public class Springboot74Application
{

    public static void main(String[] args)
    {
        SpringApplication.run(Springboot74Application.class, args);
    }

    @Autowired
    private MessageSource messageSource;

    @GetMapping("/")
    public String hello(Model model){
        Locale locale = LocaleContextHolder.getLocale();
        model.addAttribute("message",messageSource.getMessage("message",null,locale));
        return "index";

    }
}

在src/main/resources/template下新建index.html,在页面中创建两个按钮,单击按钮切换语言。index.html页面代码如代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="/?lang=en_US">English(US)</a>
<a href="/?lang=zh_CN">简体中文</a><br>
<p><label th:text="#{message}"></label></p>
</body>
</html>

启动项目,在浏览器上访问http://localhost:8089/,显示的内容如图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

虾米大王

有你的支持,我会更有动力

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

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

打赏作者

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

抵扣说明:

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

余额充值