[英]Does Pebble templating engine provide any methods to manually designate i18n locale
我正在使用Pebble
處理郵件內容,效果很好,
通常我讓它從環境變量中獲取語言環境:
@Autowired
private Environment env;
PebbleTemplate template = pebbleEngine.getTemplate(templateName);
Writer writer = new StringWriter();
template.evaluate(writer, templateVars, Locale.forLanguageTag(env.getProperty("system-prop.locale")));
String content = writer.toString();
<td>
{{ i18n("messages/mail", "welcome.words") }}
</td>
現在我有一些特別的模板,我想同時放置英文和法文消息,
Pebble
是否提供任何方法來手動指定語言環境,如Handlebars.java
?
<td>
{{i18n "welcome.words" locale="en_US"}}<br />
{{i18n "welcome.words" locale="fr_FR"}}
</td>
感謝@eric-bussieres 的回答,
我想出了自定義 function,它是 i18n 的修改版本。
Output:
<td>
Hello World
Bonjour World
</td>
代碼:
<!-- paragraph.html -->
<td>
{{ i18ncustom("messages/mail", "welcome.words", "en_US", name) }}<br />
{{ i18ncustom("messages/mail", "welcome.words", "fr_FR", name) }}
</td>
#mail_en_US.properties
welcome.words=Hello {0}
#mail_fr_FR.properties
welcome.words=Bonjour {0}
@Autowired
private PebbleEngine pebbleEngine;
public void test() throws IOException {
PebbleTemplate template = pebbleEngine.getTemplate("paragraph");
Writer writer = new StringWriter();
template.evaluate(writer, Collections.singletonMap("name", "World"));
log.info("{}", writer.toString());
}
@Configuration
public class PebbleConfig {
@Bean
public Extension i18nCustomExtension() {
return new I18nCustomExtension();
}
}
public class I18nCustomExtension extends AbstractExtension {
@Override
public Map<String, Function> getFunctions() {
Map<String, Function> functions = new HashMap<>();
functions.put("i18ncustom", new I18nCustomFunction());
return functions;
}
}
public class I18nCustomFunction implements com.mitchellbosecke.pebble.extension.Function {
private final List<String> argumentNames = new ArrayList<>();
public I18nCustomFunction() {
this.argumentNames.addAll(Arrays.asList("bundle", "key", "locale", "params"));
}
@Override
public List<String> getArgumentNames() {
return this.argumentNames;
}
@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
String basename = (String) args.get("bundle");
String key = (String) args.get("key");
String locale = (String) args.get("locale");
Object params = args.get("params");
ResourceBundle bundle = ResourceBundle.getBundle(basename, forLanguageTag(locale), new UTF8Control());
Object phraseObject = bundle.getObject(key);
if (params == null) {
return phraseObject;
}
if (params instanceof List) {
List<?> list = (List<?>) params;
return MessageFormat.format(phraseObject.toString(), list.toArray());
}
return MessageFormat.format(phraseObject.toString(), params);
}
public static Locale forLanguageTag(String languageTag) {
String[] splits = languageTag.split("\\-");
return splits.length > 1 ? new Locale(splits[0], splits[1]) : new Locale(splits[0], "");
}
}
目前,它不受支持。 它始終使用 spring 上下文中的語言環境。 我建議您提供自定義 function 並根據需要使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.