引用 Wicket i18n 属性文件中的特定页面

Refering to a specific page in Wicket i18n properties file

我正在构建我的第一个 Wicket 项目,我发现我的代码库中的属性文件数量正在快速增长。理想情况下,我希望将每个 language/region 的所有国际化内容包含在一个文件中。就是为了方便找东西。

我发现我的应用程序属性文件可能非常适合这个。我的应用程序属性文件名为 ApiAdminApplication.properties。现在我正在尝试将我的可译文件添加到这个文件中,而不是把事情弄得一团糟。

根据 ComponentStringResourceLoader 的 javadoc,这应该是可能的。显然查找顺序如下:

  page1.properties => form1.input1.Required
  page1.properties => Required
  form1.properties => input1.Required
  form1.properties => Required
  input1.properties => Required
  myApplication.properties => page1.form1.input1.Required
  myApplication.properties => Required

倒数第二行包含我正在寻找但无法开始工作的行为。

我有一个名为 CustomerEditPage 的页面,其中又包含一个 ID 为 customerForm

的表单

所以这是我要添加到 ApiAdminApplication.properties 的内容,我认为根据上面的代码片段应该可以工作:

CustomerEditPage.customerForm.name=Customer name

遗憾的是,这不起作用。但是,我可以通过省略页面名称并以 customerForm 开头来实现它,但这不是我想要的。我希望每页国际化包含在一个文件中。

任何人都可以给我一些建议吗?谢谢。

我认为 ComponentStringResourceLoader 的 javadoc 是错误的,应该修复。

要完成您的需要,您需要扩展 ClassStringResourceLoader 并覆盖 getResourcePath()。在您的实现中,您必须在结果前加上拥有作为参数传递的组件的页面的名称。

然后您需要在 ApiAdminApplication#init() 方法中注册您的加载器:

getResourceSettings().getStringResourceLoaders().add(new MyClassStringResourceLoader(ApiAdminApplication.class))

查看 defaults

请在 https://issues.apache.org/jira/projects/WICKET/issues 提交错误报告,以便解决 javadoc 问题(或者比我更了解如何完成此操作的其他人可以向我们解释)。

报告错误后,我最终按照 martin-g 的建议进行了操作,并扩展了 ClassStringResourceLoader。为了您的方便,这是我所做的:

public class PrefixedStringResourceLoader extends ClassStringResourceLoader {

    public PrefixedStringResourceLoader(Class<?> clazz) {
        super(clazz);
    }

    protected String getResourcePath(final Component component) {
        final Class<? extends Page> parentClass = component.getPage().getClass();

        final String resPath = super.getResourcePath(component);

        if (!resPath.isEmpty())
            return String.format("%s.%s", parentClass.getSimpleName(), resPath);

        return parentClass.getSimpleName();
    }

}

这有一个小问题。它总是要求您使用完整的资源路径。这可能有点棘手,我在使用下面的代码片段时遇到了一些问题:

<input type="submit" wicket:id="save" wicket:message="value:save" />

计算结果为 CustomerEditPage.customerForm.save.save,我预计它会变成:CustomerEditPage.customerForm.save。事实并非如此,因为 wicket:message 实际上成为保存表单输入的子项。

我最终选择了:

<input type="submit" wicket:id="save" wicket:message="value:caption" />

计算结果为 CustomerEditPage.customerForm.save.caption,我觉得它更具可读性。当然,您可以推出自己的更高级的资源加载器,但这个对我来说已经足够好了。