Spring Boot实现国际化

src\main\resources\i18n\messages_zh_CN.properties
message.hello=你好,世界!
message.welcome=欢迎!
src/main/resources/i18n/messages_en_US.properties
message.hello=Hello World!
message.welcome=Welcome!
默认语言
src\main\resources\i18n\messages.properties
message.hello=Hello World!
message.welcome=Welcome!

config

@Configuration
public class WebConfig implements WebMvcConfigurer {
   

    @Bean
    public LocaleResolver localeResolver() {
   
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言设置为英文
        slr.setDefaultLocale(Locale.US);//ENGLISH
        return slr;
    }

    // 如果还需要拦截器来处理locale切换请求
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
   
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        registry.addInterceptor(interceptor);
    }
}

controller

@Autowired
    private MessageSource messageSource;

    @GetMapping("/switch-language")
    public String switchLanguage(@RequestParam("lang") String lang, HttpServletRequest request) {
   
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale(lang));
        return "redirect:/";
    }

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

在Thymeleaf模板中引用国际化消息:

<!-- home.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Internationalization Example</title>
</head>
<body>
    <h1 th:text="#{message.hello}"></h1>
    <p th:text="#{message.welcome}"></p>
    <a th:href="@{/switch-language?lang=en_US}" th:text="English">English</a>
    <a th:href="@{/switch-language?lang=zh_CN}" th:text="中文">中文</a>
</body>
</html>
来切换语言
switch-language?lang=zh_CN
switch-language?lang=en_US

相关推荐

  1. SpringBoot 国际化-自定义 LocaleResolver

    2024-01-12 13:00:03       64 阅读
  2. Spring Boot实现国际化

    2024-01-12 13:00:03       58 阅读
  3. 【前端】国际化实现过程

    2024-01-12 13:00:03       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 13:00:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 13:00:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 13:00:03       87 阅读
  4. Python语言-面向对象

    2024-01-12 13:00:03       96 阅读

热门阅读

  1. 【数据库学习】hive

    2024-01-12 13:00:03       52 阅读
  2. 深度学习中的正则化指的是什么?

    2024-01-12 13:00:03       61 阅读
  3. vue3 组件内判断是从哪个页面过来的

    2024-01-12 13:00:03       67 阅读
  4. golang常见算法题

    2024-01-12 13:00:03       59 阅读
  5. JPA的乐观锁实现并发执行SQL案例

    2024-01-12 13:00:03       49 阅读
  6. 测试工程师常用的ChatGPT通用提示词模板

    2024-01-12 13:00:03       54 阅读
  7. LeetCode [103] 二叉树的锯齿形层序遍历

    2024-01-12 13:00:03       62 阅读
  8. 安全加密算法

    2024-01-12 13:00:03       65 阅读
  9. 新版cnpmcore部署私有npm源全教程

    2024-01-12 13:00:03       47 阅读