使用 spring boot 国际化

This commit is contained in:
Captain.B 2020-04-17 10:06:09 +08:00
parent de742839a8
commit 4121d011a9
2 changed files with 12 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.config;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.i18n.I18nManager;
import io.metersphere.i18n.Translator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -20,6 +21,12 @@ public class I18nConfig {
return new I18nManager(dirs);
}
@Bean
@ConditionalOnMissingBean
public Translator translator() {
return new Translator();
}
@Bean
@ConditionalOnMissingBean
public CommonBeanFactory commonBeanFactory() {

View File

@ -2,23 +2,21 @@ package io.metersphere.i18n;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class Translator {
private static MessageSource messageSource;
@Resource
public void setMessageSource(MessageSource messageSource) {
Translator.messageSource = messageSource;
}
/**
* 单Key翻译
*/
public static String get(String key) {
return messageSource.getMessage(key, null, "Not Support Key", LocaleContextHolder.getLocale());
}
@Resource
public void setMessageSource(MessageSource messageSource) {
Translator.messageSource = messageSource;
}
}