Spring Boot-国际化创建与使用

Spring Boot-国际化创建与使用

1. IDEA中国际化文件的创建

resource目录下新建i18n文件夹,右键->new->Resource Bundle

在这里插入图片描述

首次创建时需手动按"+"添加,创建成功后如下图:

在这里插入图片描述

选择demo.properties文件打开,在编辑页面的最下方有Text与Resource Bundle两个选项,选择Resource Bundle切换视图:

在这里插入图片描述

2. 使用

配置文件配置国际化文件,多个使用逗号隔开:

spring.messages.basename=i18n.demo

2.1 后端获取国际化值

配置项目默认的国际化语言:

package com.example.config;

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

import java.util.Locale;

/**
 * 配置默认国际化语言
 * @author wyp
 */
@Configuration
public class LocaleConfig {
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        //这里设置为US
        resolver.setDefaultLocale(Locale.US);
        return resolver;
    }
}

编写获取国际化值的工具类:

package com.example.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * @author wyp
 */
@Component
public class InternationalizedGetValue {

    private static MessageSource messageSource;

    public InternationalizedGetValue(MessageSource messageSource) {
        InternationalizedGetValue.messageSource = messageSource;
    }

    /**
     * 通过key与语言环境获取国际化值
     */
    public static String getValueByLocale(String key, Locale locale) {
        try {
            return messageSource.getMessage(key, null, locale);
        } catch (Exception e) {
            e.printStackTrace();
            return key;
        }
    }

    /**
     * 获取本地默认的国际化值
     */
    public static String getValue(String key) {
        return getValueByLocale(key,LocaleContextHolder.getLocale());
    }

    public static String getCnValue(String key) {
        //zh-CN
        return getValueByLocale(key,Locale.SIMPLIFIED_CHINESE);
    }

    public static String getTwValue(String key) {
        //zh-TW
        return getValueByLocale(key,Locale.TRADITIONAL_CHINESE);
    }

    public static String getUsValue(String key) {
        //en-US
        return getValueByLocale(key,Locale.US);
    }
}

其中的 String getMessage(String code, @Nullable Object[] args, Locale locale) 方法介绍:

参数:

code – 要查找的消息代码,例如“calculator.noRateSet”。鼓励 MessageSource 用户将消息名称基于合格的类或包名称,以避免潜在的冲突并确保最大的清晰度。
args – 为消息中的参数填充的参数数组(参数在消息中类似于“{0}”、“{1,date}”、“{2,time}”),如果没有则为null
locale – 进行查找的语言环境

有对应的code则返回对应的值,否则抛出NoSuchMessageException异常。

测试:

System.out.println(InternationalizedGetValue.getValue("demo.name"));

输出默认设置的国际化语言即上面所设置的US,所以得到结果为:name

System.out.println(InternationalizedGetValue.getCnValue("demo.name"));

输出得到结果为:姓名

2.2 Thymeleaf使用国际化

具体前往:https://blog.csdn.net/xiaochen_2715/article/details/125413905

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小辰~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值