package net.watermelon.demo.vo; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.support.AbstractMessageSource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; public class MessageResource extends AbstractMessageSource implements ResourceLoaderAware { @SuppressWarnings("unused") private ResourceLoader resourceLoader; /** * Map split character */ protected final String MAP_SPLIT_CODE = "|"; private final Map<String, String> properties = new HashMap<String, String>(); publicMessageResource() { reload(); } public void reload() { properties.clear(); properties.putAll(loadTexts()); } /** * * Description: TODO * Query data virtual data, you can read information from the database, omitted here * @return */ private List<Resource> getResource() { List<Resource> resources = new ArrayList<Resource>(); Resource re = new Resource(); Resource re1 = new Resource(); re.setResourceId(1); re.setName("common.name"); re.setText("name"); re.setLanguage("en"); resources.add(0, re); re1.setResourceId(2); re1.setName("common.name"); re1.setText("\u59D3\u540D"); re1.setLanguage("zh"); resources.add(1, re1); return resources; } /** * * Description: TODO * Load data * @return */ protected Map<String, String> loadTexts() { Map<String, String> mapResource = new HashMap<String, String>(); List<Resource> resources = this.getResource(); for (Resource item : resources) { String code = item.getName() + MAP_SPLIT_CODE + item.getLanguage(); mapResource.put(code, item.getText()); } return mapResource; } /** * * Description: TODO * @param code * @param locale locale language * @return */ private String getText(String code, Locale locale) { String localeCode = locale.getLanguage(); String key = code + MAP_SPLIT_CODE + localeCode; String localeText = properties.get(key); String resourceText = code; if (localeText != null) { resourceText = localeText; } else { localeCode = Locale.ENGLISH.getLanguage (); key = code + MAP_SPLIT_CODE + localeCode; localeText = properties.get(key); if (localeText != null) { resourceText = localeText; } else { try { if (getParentMessageSource() != null) { resourceText = getParentMessageSource().getMessage( code, null, locale); } } catch (Exception e) { logger.error(e); } } } return resourceText; } public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); } @Override protected MessageFormat resolveCode(String code, Locale locale) { String msg = getText(code, locale); MessageFormat result = createMessageFormat(msg, locale); return result; } @Override protected String resolveCodeWithoutArguments(String code, Locale locale) { String result = getText(code, locale); return result; } }
Just configure a custom MessageResource
Add the following code to the bean definition
@Bean public ResourceBundleMessageSource propertiesMessageSource(){ ResourceBundleMessageSource rs = new ResourceBundleMessageSource(); rs.setBasenames("messages"); return rs; } @Bean public MessageSource messageSource(){ MessageResource mrs = new MessageResource(); mrs.setParentMessageSource(propertiesMessageSource()); return mrs; }
Here setParentMessageSource can read the multilingual processing in the properties configuration file as well, increasing the multilingual processing of the database, which is more convenient for management and development.