GWT SafeHtmlTemplate 国际化

GWT SafeHtmlTemplate Internationalization

我正在对我的 gwt 应用程序进行国际化,我有几个正在使用的 SafeHtmlTemplate,但我不确定如何翻译。在仍然使用 SafeHtmlTemplate 方法的同时,是否有一种方便的方法来翻译这些内容?否则,我是否会像往常一样将星期几的名称传递给模板并使用 Constants 翻译它们?

    @Template("<table>" + 
            "<tr><td>Sunday:</td>{6}</tr>" +
            "<tr><td>Monday:</td>{0}</tr>" +
            "<tr><td>Tuesday:</td>{1}</tr>" +
            "<tr><td>Wednesday:</td>{2}</tr>" +
            "<tr><td>Thursday:</td>{3}</tr>" +
            "<tr><td>Friday:</td>{4}</tr>" +
            "<tr><td>Saturday:</td>{5}</tr>" +
            "</table>")
    SafeHtml hoursHtml(SafeHtml mon, SafeHtml tue, SafeHtml wed, SafeHtml thu, SafeHtml fri, SafeHtml sat, SafeHtml sun);

由于无法像使用 UiBinder 模板那样在模板内使用 Constants 对象,我想说的是您将翻译后的字符串放在模板外的方法,然后将它们作为参数传递将是下一个更简单的选择。

此外,在 GWT 的 SafeHtml 文档中一再提到,用于构建 SafeHtml 对象的字符串必须在编译时确定:

public interface SafeHtmlTemplates
A tag interface that facilitates compile-time binding of HTML templates to generate SafeHtml strings.

public static SafeHtml fromSafeConstant(java.lang.String s)
Returns a SafeHtml constructed from a safe string, i.e., without escaping the string.
Important: For this method to be able to honor the SafeHtml contract, all uses of this method must satisfy the following constraints:
1. The argument expression must be fully determined at compile time.
2. The value of the argument must end in "inner HTML" context and not contain incomplete HTML tags.

SafeHtml 模板中使用 Constants 可能会干扰此规则。
有人可能会争辩说所有常量都应该是内联的,并且代码的国际化排列已经在编译时生成,因此在模板内使用常量时克服这些限制应该相对容易;但话又说回来,Google 可能以不允许安全混合两种机制的方式实现它,因此存在限制。

因此(可能)将翻译后的字符串作为参数传递是可行的方法。