1、照葫芦来个瓢,写一个模版指令
public class I18nDirective extends Directive { private Expr keyExpr; private List<String> params = new ArrayList<>(); @Override public void exec(Env env, Scope scope, Writer writer) { String i18nState = (String)scope.getRootData().get("i18nState"); try { // String key = (String)this.keyExpr.eval(scope); String key = ((Const)this.keyExpr).getStr(); String msg = params.isEmpty() ? I18nKit.get(i18nState, key) : I18nKit.format(i18nState, key, params.toArray()); writer.write(msg); } catch (IOException e) { throw new RuntimeException(e); } } public void setExprList(ExprList exprList) { int paraNum = exprList.length(); if (paraNum == 0) { throw new ParseException("参数个数不对,至少传入key", location); } if (paraNum == 1) { this.keyExpr = exprList.getExpr(0); } else { for (int i = 1; i < paraNum; i++) { Expr expr = exprList.getExpr(i); params.add(((Const)expr).getStr()); } } } }
2、JFinalConfig添加指令
me.addDirective("i18n", I18nDirective.class);
3、使用
#i18n("key")
照葫芦画瓢的也使用上了,背后的其所以然还不清楚,先用上,以后再琢磨。进来的各位看官,我上述使用有问题也给指点指点。