likes
comments
collection
share

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

作者站长头像
站长
· 阅读数 1

什么是国际化

国际化(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的国际化功能。

实践出真知

话不多说,上代码。

如何使用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中 如何使用Spring Boot轻松实现国际化和本地化

修改配置文件

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 获取中文国际化环境,测试结果如下图所示: 如何使用Spring Boot轻松实现国际化和本地化

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

总结

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