springboot-i18n internationalization

Brief introduction

In computing, internationalization and localization are means of adapting computer software to different languages, regional peculiarities and technical requirements of a target locale.

the term

  • i18n

    Internationalization 英 [ˌɪntəˌnæʃnəlaɪ'zeɪʃn], international.

    Because there are 18 characters between the first letter "i" and the end of the letter "n", it is referred to as i18n.

    internationalization means that in order to make an application to adapt to change between different languages ​​and regions without making systematic changes in the design of the measures taken.
  • l10n

    localization, localization.

    Since there are 10 letters between the first letter "l" and the end of the letter "n", so referred to l10n.
    localization refers to the order that applications can use to join the process after local specialized components and translated text in a particular locale or region.
  • locale: refers to the sign language and a particular region in combination

General 语言_地区may determine a particular type of localized information.

基名_语言_地区.properties

  • Represented by the two-letter lowercase language specific code by ISO-639 standard definition.
  • Area represented by two capital letters, by the ISO-3166 standard defined.
  • Base name, basename, general business code. E.g:ValidationLogin.properties

Common configuration:

  • i18n_zh_CN.properties: mainland China, Chinese language resources

  • i18n_en_US.properties: resource the United States, the English language

  • i18n.properties: default resource file, if the request is appropriate resource file does not exist, the use of this resource file

JDK support

  • java.util.Local

SpringBoot support

  • org.springframework.context.MessageSource
    ```
    public interface MessageSource {
    String getMessage(String var1, Object[] var2, String var3, Locale var4);

      String getMessage(String var1, Object[] var2, Locale var3) throws NoSuchMessageException;
    
      String getMessage(MessageSourceResolvable var1, Locale var2) throws NoSuchMessageException;

    }
    ```

  • org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration

  • org.springframework.context.support.AbstractApplicationContext#initMessageSource
  • ResourceBundleMessageSource
  • ReloadableResourceBundleMessageSource

    Understanding these classes for troubleshooting help.

Real

SpringBootAvailable in two configurations.

Java configuration

Redefine org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration#messageSourcethe bean.

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

application configuration

spring.messages.basename=i18n

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration#messageSourceDepending on the configuration file is automatically created bean messageSource.

Code demonstrates

Configuration file:
email.server=mail.163.com

RestController

@RestController
public class MessageSourceTestController {

    @Autowired
    private MessageSource messageSource;

    @GetMapping("/mail")
    public String getUsers() {
        return messageSource.getMessage("email.server", null, Locale.CHINA);
    }

}

Request interface, obtain the value of the configuration file.

Problems encountered

It took over an old project, restfulthe framework of self-study or switch to springboot framework, internationalization configuration encountered some problems.

Summarized as follows.

MessageSource null

org.springframework.context.support.AbstractApplicationContext#initMessageSourceAt initialization, not detected messageSourceBean, it will provide a default empty implementation DelegatingMessageSource.

Solutions directly instantiated a bean. See abovejava配置

@ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)

Should be a bug, refer to the Spring the Boot MessageSourceAutoConfiguration , the actual development can be ignored.

debug=true

application.propertiesAdding debug=true, when the application starts, will automate the configuration, bean and other information printed.

2019-06-13 14:03:42.740 DEBUG 18680 --- [           main] ationConfigEmbeddedWebApplicationContext : Using MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[messages/messages]]
 MessageSourceAutoConfiguration matched:
      - ResourceBundle found bundle URL [file:/D:/git/github/spring-boot-samples/spring-boot-sample-il8n/target/classes/messages/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)
      - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)

Not detected messageSourceBean, will provide an empty default implementation

[2019-06-13 14:19:43.453] [main] [DEBUG] [igServletWebServerApplicationContext:739 ] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3f2ab6ec]

reference

Internationalization and localization introduced a Java program

International resource file naming convention

Guess you like

Origin www.cnblogs.com/wuxinshui/p/11016397.html