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)
|