需求
重写ActionSupport国际化getText以适应接口传递语种参数返回对应语种内容信息
分析
要想实现根据前端语种返回,即需传递语种参数(例如:en_US,zh_CN)给到TextProviderFactory以便于struts找到相应后缀名的properties文件(例如:common_zh_CN.properties,common_en_US.properties)
但TextProviderFactory接口的createInstance方法只传递Class。因此新建MyTextProviderFactory接口以及其实现类MyStrutsTextProviderFactory设定新的locale信息的方式
实现
继承ActionSupport的子类重写getText
以及getTextProvider
方法
public class BaseController extends ActionSupport {
·····
public String getText(String aTextName) {
return this.getTextProvider().getText(aTextName);
}
protected TextProvider getTextProvider() {
if (this.textProvider == null) {
Container container = this.getContainer();
MyTextProviderFactory tpf = (MyTextProviderFactory)container.getInstance(MyTextProviderFactory.class);
this.textProvider = tpf.createInstance(this.getClass(),user.getLanguage());//传递前端语种参数
}
return this.textProvider;
}
······
}
重写TextProviderFactory接口
package com.opensymphony.xwork2;
public interface MyTextProviderFactory {
TextProvider createInstance(Class clazz, String language);
TextProvider createInstance(ResourceBundle bundle, String language);
}
重写StrutsTextProviderFactory实现类
package com.opensymphony.xwork2;
public class MyStrutsTextProviderFactory implements MyTextProviderFactory {
protected LocaleProviderFactory localeProviderFactory;
protected LocalizedTextProvider localizedTextProvider;
protected TextProvider defaultTextProvider;
@Inject
public void setLocaleProviderFactory(LocaleProviderFactory localeProviderFactory) {
this.localeProviderFactory = localeProviderFactory;
}
@Inject
public void setLocalizedTextProvider(LocalizedTextProvider localizedTextProvider) {
this.localizedTextProvider = localizedTextProvider;
}
@Inject(required = false)
public void setDefaultTextProvider(TextProvider defaultTextProvider) {
this.defaultTextProvider = defaultTextProvider;
}
@Override
public TextProvider createInstance(Class clazz, String language) {
TextProvider instance = getTextProvider(clazz);
if (instance instanceof ResourceBundleTextProvider) {
((ResourceBundleTextProvider) instance).setClazz(clazz);
((ResourceBundleTextProvider) instance).setLocaleProvider(new MyLocaleProvider(language));//设置Locale语种
}
return instance;
}
@Override
public TextProvider createInstance(ResourceBundle bundle, String language) {
TextProvider instance = getTextProvider(bundle);
if (instance instanceof ResourceBundleTextProvider) {
((ResourceBundleTextProvider) instance).setBundle(bundle);
((ResourceBundleTextProvider) instance).setLocaleProvider(new MyLocaleProvider(language));//设置Locale语种
}
return instance;
}
protected TextProvider getTextProvider(Class clazz) {
if (defaultTextProvider != null) {
return defaultTextProvider;
}
return new TextProviderSupport(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
}
protected TextProvider getTextProvider(ResourceBundle bundle) {
if (defaultTextProvider != null) {
return defaultTextProvider;
}
return new TextProviderSupport(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
}
}
替换获取locale信息的方式
package com.opensymphony.xwork2;
public class MyLocaleProvider implements LocaleProvider {
private final static Logger LOG = LogManager.getLogger(DefaultLocaleProvider.class);
private String language;
public MyLocaleProvider (String language){
this.language = language;
}
@Override
public Locale getLocale() {
if (language != null) {
String[] locales = language.split("_");
return ctx.getLocale(locales[0],locales[1]);
} else {
LOG.debug("Action context not initialized");
return new Locale("zh","CN");
}
}
@Override
public boolean isValidLocaleString(String localeStr) {
Locale locale = null;
try {
locale = LocaleUtils.toLocale(StringUtils.trimToNull(localeStr));
} catch (IllegalArgumentException e) {
LOG.warn(new ParameterizedMessage("Cannot convert [{}] to proper locale", localeStr), e);
}
return isValidLocale(locale);
}
@Override
public boolean isValidLocale(Locale locale) {
return LocaleUtils.isAvailableLocale(locale);
}
}