美文网首页
5.springboot 与 国际化

5.springboot 与 国际化

作者: MlLance | 来源:发表于2020-03-22 18:14 被阅读0次

Springboot Internationalization

参考文章:https://phrase.com/blog/posts/spring-boot-internationalization/
https://www.tutorialspoint.com/spring_boot/spring_boot_internationalization.htm

  • 原理:
    国际化是一个过程,可以使您的应用程序适应不同的语言和地区,而无需在源代码上进行工程更改。换句话说,国际化是本地化的准备。
    LocaleResolver
    我们需要确定您的应用程序的默认语言环境。我们需要在我们的Spring Boot应用程序中添加LocaleResolver bean。
    LocaleChangeInterceptor用于根据添加到请求中的language参数的值来更改新的Locale。
    为了实现此效果,我们需要将LocaleChangeInterceptor添加到应用程序的注册表拦截器中。配置类应扩展WebMvcConfigurerAdapter类,并重写addInterceptors()方法。
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  • code
    • 配置类
package com.study.lancestudyspringboot.studyinternatonalization.config;

import org.springframework.context.annotation.Bean;
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.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class Internationalization implements WebMvcConfigurer {
   @Bean
   public LocaleResolver localeResolver() {
      SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
      sessionLocaleResolver.setDefaultLocale(Locale.US);
      return sessionLocaleResolver;
   }
   @Bean
   public LocaleChangeInterceptor localeChangeInterceptor() {
      LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
      localeChangeInterceptor.setParamName("language");
      return localeChangeInterceptor;
   }

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(localeChangeInterceptor());
   }
}
  • 测试配置是否成功
package com.study.lancestudyspringboot.studyinternatonalization.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("internationalizationtst")
public class HomeController {
 
    @GetMapping("/test")
    public String welcome() {
        return "welcome";
    }

    @GetMapping("/t")
    public Integer test() {
        return 1;
    }
}
  • 消息来源
    默认情况下,Spring Boot应用程序从类路径下的src / main / resources文件夹中获取
    消息源。默认语言环境消息文件名应为message.properties,每个语言环境的文件应
    命名为messages_XX.properties。“ XX”代表语言环境代码。

    所有消息属性都应用作密钥对值。如果在语言环境中找不到任何属性,则应用程序
    将使用messages.properties文件中的默认属性。

  • 默认的messages.properties将如下所示-

    welcome.text=Hi Welcome to Everyone

  • 法语的messages_fr.properties将如下所示-
    welcome.text=Salut Bienvenue à tous
    注意 -消息源文件应另存为“ UTF-8”文件格式。

html 放在templates 目录下

image.png
<!DOCTYPE html>
<html>
<head>
    <meta charset = "ISO-8859-1"/>
    <title>Internationalization</title>
</head>
<body>
<h1 th:text = "#{welcome.text}"></h1>
</body>
</html>

相关文章

网友评论

      本文标题:5.springboot 与 国际化

      本文链接:https://www.haomeiwen.com/subject/gwtvyhtx.html