如何使用Spring Boot轻松实现国际化和本地化

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

什么是国际化

国际化(Internationalization) 是指为了适应不同语言、文化和地区的用户,使软件能够方便地进行本地化修改的过程。
国际化(Internationalization) 简称i18n,其中 “i”Internationalization的首字母 ,“n” 是最后一个字母 , “18” 代表了中间省略的18个字母。

SpringBoot 国际化

SpringBoot也提供了国际化的功能,在Spring Boot中,国际化通常涉及以下几个关键组件:

  1. 资源文件(Properties文件):这些文件包含了不同语言的文本消息,每个语言对应一个资源文件。通常,资源文件的命名采用messages_语言代码.properties的格式,例如messages_en.properties(英语)、messages_zh_CN.properties(简体中文)等。

  2. MessageSource接口:这是Spring框架提供的一个核心接口,定义了获取文本消息的方法。它的实现类负责加载并解析资源文件,并根据语言和代码来返回相应的消息。

  3. LocaleResolver接口:这是Spring框架提供的另一个接口,用于解析用户的语言偏好。根据用户的设置,LocaleResolver可以确定要使用哪个语言。

  4. 组件中使用的文本消息:在应用程序的界面和代码中,您可以使用特定的消息代码来引用资源文件中的文本消息。Spring Boot会根据用户的语言偏好选择合适的消息进行显示。

通过配置MessageSource和LocaleResolver,以及在应用程序中使用相应的消息代码,就可以实现Spring Boot的国际化功能。

实践出真知

话不多说,上代码。

新建Properties文件

Resource目录下新建Properties文件

  • 中文properties文件 messages_zh_CN.properties
hello=你好
welcome=欢迎关注公众号, {0}!
  • 英文properties文件 messages_en.properties
hello=hi
welcome=Welcome to follow WeChat Public Number, {0}!

创建完文件idea会自动将国际化文件归类到Resource Bundle中
Resource Bundle

修改配置文件

application.properties

# 国际化
# 默认名称,可以写多个,用逗号分隔
spring.messages.basename=messages_zh_CN
spring.messages.encoding=UTF-8
# 找不到对应区域的语言时,是否回退到系统区域的语言,默认 true
spring.messages.fallback-to-system-locale=false
# 找不到code时,是否直接返回code值,而不是抛异常,默认false,抛异常
spring.messages.use-code-as-default-message=true
# 是否始终使用MessageFormat格式化国际化消息,即使没有国际化消息参数,默认false
spring.messages.always-use-message-format=false
# 加载国际化资源后的过期时间,不设置不过期,单位秒
#spring.messages.cache-duration=

测试

@Resource
private MessageSource messageSource;

@Test
void testMessageSource() {

    Locale china = Locale.CHINA;
    System.out.println("\n中文环境");
    //中文语言
    String hello_zh = messageSource.getMessage("hello", null, china);
    System.out.println(hello_zh);
    // 占位符替换
    String welcome_zh = messageSource.getMessage("welcome", new String[]{"索码理"}, china);
    System.out.println(welcome_zh);

    //英文语言
    Locale english = Locale.ENGLISH;
    System.out.println("\n英文环境");
    String hello_en = messageSource.getMessage("hello", null, english);
    System.out.println(hello_en);
    String welcome_en = messageSource.getMessage("welcome", new String[]{"suncodernote"}, english);
    System.out.println(welcome_en);

    System.out.println("\n没有对应语言的国际化属性,返回code");
    //没有对应语言的国际化属性,返回code
    String hello_test = messageSource.getMessage("hello-test", null, china);
    System.out.println(hello_test);

    System.out.println("\n没有对应语言的国际化区域时,返回默认语言");
    //没有对应语言的国际化区域时,返回默认
    String hello_fr = messageSource.getMessage("hello", null, Locale.FRANCE);
    System.out.println(hello_fr);
}

测试结果:

中文环境
你好
欢迎关注公众号, 索码理!

英文环境
你好
欢迎关注公众号, suncodernote!

没有对应语言的国际化属性,返回code
hello-test

没有对应语言的国际化区域时,返回默认语言
你好

获取所有国际化资源

上面的测试我们都是只能根据一个code获取一个国际化信息,我们在切换语言使用国际化时,总不能每次进行国际化的时候都调用一次接口吧,这肯定是不行的。
下面是获取指定语言的所有的国际化信息的代码示例。

定义一个I18nService 接口:

public interface I18nService {

    /**
     * 获取指定语言所有国际化信息
     * @param locale
     * @return
     */
    Map<String, String> getAllMessages(Locale locale);
}

I18nService 接口实现类

@Service
public class I18nServiceImpl implements I18nService{

    @Autowired
    private MessageSource messageSource;

    @Override
    public Map<String, String> getAllMessages(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }
        //存放所有message
        Map<String, String> messages = new HashMap<>();

        ResourceBundleMessageSource bundleMessageSource = (ResourceBundleMessageSource) messageSource;
        String[] basenames = bundleMessageSource.getBasenameSet().toArray(new String[0]);

        for (String basename : basenames) {
            //从缓存中获取资源文件
            ResourceBundle resourceBundle = ResourceBundle.getBundle(basename, locale);
            //获取资源文件的所有code
            Set<String> keys = resourceBundle.keySet();

            for (String key : keys) {
                //根据code获取对应的message
                String message = messageSource.getMessage(key, null, locale);
                messages.put(key, message);
            }
        }
        return messages;
    }
}

getAllMessages方法中,拿到了指定资源文件的所有code,有了code再做一些操作就很方便了。比如获取以xxx开头的code。

I18nController

@RequestMapping("/i18n")
@RestController
public class I18nController {

    @Resource
    private I18nService i18nService;

    @RequestMapping("/messages")
    public Map<String, String> getAllMessages(@RequestHeader(name = "Accept-Language" , required = false) Locale locale) {
        return i18nService.getAllMessages(locale);
    }
}

通过postman调用接口,将请求头 Accept-Language 设置为 zh 获取中文国际化环境,测试结果如下图所示:
接口调用结果

以上就是SpringBoot 国际化一个简单的实现的操作步骤,感兴趣的可以动手试试。

总结

本文介绍了SpringBoot 国际化功能的简单使用,通过在资源文件中配置国际化字段,然后获取对应区域的国际化信息。这些操作都是静态的,要预先配置好国际化信息才能进行一系列的操作,不够灵活,下篇文章将介绍一下动态的国际化配置,敬请关注。

  • 41
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

索码理

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

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

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

打赏作者

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

抵扣说明:

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

余额充值