如何翻译 .java 源文件中的文本?

6

我有一个像这样的枚举

public enum CheckboxFeature {

    Option1(" choose this"),
    Option2(" or this"),
    Option3(" maybe this"),
    Option4(" lastly this");

    @Getter
    private final String text;

    public static CheckboxFeature fromName(String v) {
        for (CheckboxFeature c: CheckboxFeature.values()) {
            if (c.name().equalsIgnoreCase(v)) {
                return c;
            }
        }
        throw new IllegalArgumentException(v);
    }
}

这将在Web视图中显示四个选项作为复选框。
<form:checkboxes items="${features}" path="enabledFeatures" itemLabel="text" delimiter="<br/>"/>

我该如何翻译这些选项?在Web视图中,我使用fmt:message进行其他翻译。

我尝试过

Option1(" <fmt:message key=\"text.test\"/>"),

and

Option1(" ${option1}"),

使用

<fmt:message key="text.select" var="option1"/>

在 .jsp 文件中,这两种方法都无法起作用,因此似乎不能以这种方式工作。转换字符串的正确方式是什么?最好使用已经安装并在 servlet 的其余部分上工作的 fmt:message 和 i18n 资源(lang.properties 文件)。
2个回答

6

最好从枚举中获取资源键,然后进行查找。

public enum CheckboxFeature {
    Option1("label.option1.key"),
    Option2("label.option2.key"),
    Option1("label.option3.key"),
    Option2("label.option4.key");

    private final String key;
    [...]

我不知道如何在itemLabel属性中嵌套l10n查找,所以我会写出类似以下的代码:

<c:forEach items="Options.values()" var="current">
    <form:checkbox path="selectedOptions" value="${current.name()}"/> <fmt:message key="${current.getKey()}"/>
</c:forEach>

0

1、CheckboxFeature 增加一个类似的方法:

 public static List<String> getAllCheckboxFeature(String v) {
        return Arrays.asList(Option1.getText(),...Option4.getText());
 }


2、然后使用JSTL,例如:

<c:forEach items="${options}" var="option">
    <input type="checkbox" ="${option}">
</c:forEach>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接