国际化支持

本文介绍了Spring Boot应用中实现国际化的步骤,包括新建国际化配置文件、设置application.properties、使用Thymeleaf模板引擎,以及如何从数据库加载和管理国际化数据,强调了菜单的特殊处理和页面操作的实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 国际化
步骤:
  1. 新建国际化配置java文件
package cn.com.sitech.paas.gsgc.base.configuration;

import java.util.Locale;

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;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        // 默认语言
        sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        // 参数名
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
  1. 在resource目录下新建两个properties文件:

message_zh_CN.properties:

login.username=用户名

message_en_US.properties

login.username=account
  1. application.properties 文件新增配置
spring.messages.basename=message 
  1. 在thymeleaf模板引擎中使用#{}的标签就能调用messages中的内容
//标签中
#{login.username}
//纯文本
[[#{login.username}]]

  1. 前台页面
 <a href="/?lang=en_US">English(US)</a>
 <a href="/?lang=zh_CN">简体中文</a>
MessageSource 从数据库中加载数据:
  1. 创建MyMessageSource类继承自ResourceBundleMessageSource

package cn.com.sitech.paas.gsgc.base.configuration;

import cn.com.sitech.paas.gsgc.base.model.LocaleData;
import cn.com.sitech.paas.gsgc.base.service.locale.LocaleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

public class MyMessageSource extends ResourceBundleMessageSource {
    private  static Properties properties = new Properties();
    //前端取数据时从此方法获取
    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        String result = "";
        System.out.println(locale.toLanguageTag());
        try {
            if("zh-CN".equals(locale.toLanguageTag())) {
                result = ((LocaleData) properties.get(code)).getZh_CN();
            }else if("en-US".equals(locale.toLanguageTag())) {
                result = ((LocaleData) properties.get(code)).getEn_US();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(code);
            result = "";
        }
        return result;
    }
    // 创建时从数据库获取数据
    public MyMessageSource(LocaleDataService localeDataService) {
        //todo 从数据库中获取数据,页面管理化
        HashMap map = new HashMap();
        List<LocaleData> list = new ArrayList<LocaleData>();
        list = localeDataService.selAll(map);

        for (LocaleData localeData:list) {
            properties.put(localeData.getName(),localeData);
        }
    }
    public Properties getProperties() {
        return properties;
    }
}

  1. springboot 启动类 @Bean创建messageSource
@Bean(name="messageSource")
    public MessageSource getMessageSource(@Qualifier("localeDataService") LocaleDataService localeDataService) {
        MyMessageSource myMessageSource = new MyMessageSource(localeDataService);
        return myMessageSource;
    }
  1. 以上就可以将国际化配置数据从数据库中获取。
  2. 还要做一个功能,国际化配置数据页面化,方便管理.
国际化配置数据页面化
  1. 新建表结构
CREATE TABLE `locale_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varbinary(255) NOT NULL,
  `zh_CN` varchar(255) DEFAULT NULL,
  `en_US` varchar(255) DEFAULT NULL,
  `descr` text,
  PRIMARY KEY (`name`),
  KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8
  1. 开发页面功能,包括列表展示、新增、修改、删除、按照参数名、参数值查询等。

  2. 注意点

    1. 菜单需要单独新增字段,页面展示根据当前系统语言展示,判断条件存在session中。
    2. 页面操作,内存实时更新。
if("zh-CN".equals(locale.toLanguageTag())) {
	request.getSession().setAttribute("lang", "zh-CN");
}else if("en-US".equals(locale.toLanguageTag())){
	request.getSession().setAttribute("lang", "en-US");
}
//即时同步到messageSource中
myMessageSource.getProperties().put(localeData.getName(),localeData);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值