org.zodiac.template.base.impl.SearchLocalizedTemplatesStrategy Maven / Gradle / Ivy
The newest version!
package org.zodiac.template.base.impl;
import java.util.List;
import java.util.Locale;
import org.zodiac.sdk.toolkit.util.LocaleUtil;
/**
* 根据当前locale,查找最匹配的本地化模板。
*
* 例如:对于模板test.vm
,假如当前locale为zh_CN
,则依次尝试查找:
*
*
* - test_zh_CN.vm
* - test_zh.vm
* - test.vm
*
*/
public class SearchLocalizedTemplatesStrategy implements TemplateSearchingStrategy {
public Object getKey(String templateName) {
return getCurrentLocale();
}
public boolean findTemplate(TemplateMatcher matcher) {
List localizedTemplateNames =
LocaleUtil.calculateBundleNames(matcher.getTemplateNameWithoutExtension(), getCurrentLocale(), true);
for (String name : localizedTemplateNames) {
matcher.setTemplateNameWithoutExtension(name);
if (matcher.findTemplate()) {
return true;
}
}
return false;
}
private Locale getCurrentLocale() {
return LocaleUtil.getContext().getLocale();
}
}