【发布时间】:2015-07-03 12:52:38
【问题描述】:
我们正在努力使带有一些脏代码的旧应用程序国际化。例如,我们有一个对象 DTO InstrumentDto:
private String label;
private Quotation quotation;
private ExprQuote quoteExp;
public String getTypeCouponCouru() {
if (this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_CPN_INCLUS)
|| this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_INTERET)) {
return "Coupon attaché";
} else if(this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_PIED_CPN)){
return "Coupon détaché";
} else {
return "";
}
}
public String getFormattedLabel() {
StringBuilder formattedLabel = new StringBuilder(this.label);
Quotation quote = this.quotation;
if (this.quotation != null) {
formattedLabel.append(" ");
formattedLabel.append(FormatUtil.formatDecimal(this.quotation.getCryQuote());
if (this.quoteExp.getType().equals("PERCENT")) {
formattedLabel.append(" %");
} else {
formattedLabel.append(" ");
formattedLabel.append(this.quotation.getCurrency().getCode());
}
formattedLabel.append(" le ");
formattedLabel.append(DateUtil.formatDate(this.quotation.getValoDate()));
}
return formattedLabel.toString();
}
然后,这些方法用于 JSP。例如getFormattedLabel(),我们有:
<s:select name = "orderUnitaryForm.fieldInstrument"
id = "fieldInstrument"
list = "orderUnitaryForm.instrumentList"
listKey = "id"
listValue = "formattedLabel" />
IMO,第一种方法在 DTO 上没有它的位置。我们期望视图管理要打印的标签。而在这个视图(JSP)中,翻译那些话是没有问题的。 另外,这个方法只用在 2 JSP 中。 “重复”条件测试不是问题。
但是getFormattedLabel()比较难:这种方法在很多JSP中都用到了,格式化标签的构建比较“复杂”。而且在 DTO 中不可能有 i18n 服务。
那该怎么做呢?
【问题讨论】:
-
我觉得问题比仅仅使用
text或getText()更复杂(或者我错过了什么?) -
不,这根本不是问题,S2 i18n 多年来一直是该框架的关键特性。但是使用它需要一些技巧。
标签: java jsp struts2 internationalization dto