Can the framework output information support multiple languages
Recently, the project is internationalizing user operation logs. Why do user operations need logs? When working as a cloud platform, you need to record the user's behavior of operating resources. For example, when creating a host, you need to record it and show it to users in the log module. Such a log module can help users check whether the operation is successful and view the operation history, You can also query the latest operation records by time. Now there is a demand that this cloud platform needs to be used not only for Chinese users, but also for foreign users. The display language used by the system needs to be dynamically switched. You need to dynamically switch between Chinese and English according to the needs of users. We focus on the log. The log needs to record the user's operation name (for example), but the operation names in Chinese and English are different. Internationalization is needed here.
Chinese internationalization documents:
monitor.alarm=盘古 create.host=盘古host
International documents in English:
monitor.alarm=pangu create.host=pangu
The two internationalization files are actually stored in the form of key value. The log table records the key value, such as create.host. Then, when it is displayed to the user, first read the key value from the database, get the corresponding Chinese and English value from the key to the Chinese or English file, and then send the converted value back to the front end for display. In this way, the display can be switched well. After all, the value passed in the database is key. When log display is required, send a request to the back end, and the back end takes out the key value from the database. Generally, go back to load the Chinese and English internationalization file before system startup, and save the key value pair in the map. When the back end takes out the key from the database, you can directly take out the corresponding Chinese and English from the Chinese map or English map, and then send it back to the front end.
The above describes the key value stored in the database. How can the front end match the log query in the database. For example, the user will query the record of "create host", so the value passed back from the front end is "create host", and the value stored in the database is "create. Host". This does not match completely. In this case, you need to convert "create host" to "create. Host". My solution is as follows: first, the project is a spring project. I will first define a static variable and assign the value as follows: ` import java.util.Map;
public class Constants {
public static Map<String, String> ENGLISH_KEY_MAP = PropertiesUtils.readPropertiesAndReturnMap("/messages/messages_en.properties", false);
public static Map<String, String> CHINESE_KEY_MAP = PropertiesUtils.readPropertiesAndReturnMap("/messages/messages_zh.properties", false);
public static Map<String, String> CONFIG_MAP = PropertiesUtils.readPropertiesAndReturnMap("config.properties", true);
}`
@tombaeyens @tijsrademakers