This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9087|Reply: 0

[Source] Spring Boot MessageSource The pit of internationalized languages

[Copy link]
Posted on 2022-1-27 14:52:03 | | | |
Internationalization, also called i18n, why this name? Because internationalization English is internationalization, there are 18 letters between i and n, so it is called i18n. If our application is internationalized, it can be easily switched in different language environments, the most common is the switch between Chinese and English, and the internationalization function is also quite common.

For example, when sending a specific request, set a key-value pair in the header: "Accept-Language":"zh", through the Accept-Language corresponding value, the server can decide which region language to use, find the corresponding resource file, format it, and return it to the client.

Create a new Spring Boot project, create an i18n file under the resources folder, and create a new configuration file under this folder according to the "Language Abbreviation Table" format, as shown in the figure below:




message.properties

message_en.properties

Remember to add configuration parameters to edit application.properties to make our configuration effective:

Spring definesMessageSourceinterfaces for accessing internationalized information.

getMessage(String code, Object[] args, String defaultMessage, Locale locale)
getMessage(String code, Object[] args, Locale locale)
getMessage(MessageSourceResolvable resolvable, Locale locale)



Create a new controller and try to call a different resource file to return a prompt with the following code:



The project is deployed under a Windows server in Chinese, and there is no problem with Chinese users accessing Chinese. When deploying to the English version of Linux, Chinese users will prompt an English prompt (read the configuration of the English resource file).

The reason for this is because of usThere is no new Chinese .properties file at all, at this time the program will read the resource file that matches the current system language, and if it cannot be read again, it will not be able to read any identified properties file configuration。 When we publish the project to the Linux English system, although the program cannot read the Chinese configuration, it will read the configuration file of _en.properties, so the interface returns the Chinese prompt.

Solution, settingFallbackToSystemLocalefalse, the code is as follows:

(End)





Previous:Practical use cAdvisor to collect Docker container metrics
Next:The front-end JS converts blobs to strings
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com