2012-07-09 166 views
3

如何为我的表单组件正确创建国际化标签,以便在显示反馈消息时显示国际化字段名称而不是java代码中字段的名称?Wicket中表单组件的国际化标签

我读过这样的:

https://cwiki.apache.org/WICKET/everything-about-wicket-internationalization.html

以及检票的XHTML标签的文档:

https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html

<label wicket:for="name"> 
    <wicket:label> 
     <wicket:message key="label.name"/> 
    </wicket:label> 
</label> 
<input wicket:id="name" type="text" wicket:message="placeholder:label.name" /> 

这将导致以下错误:

Last cause: Expected close tag for '<wicket:label>' Possible attempt to embed 
component(s) '<wicket:message key="label.name"/>' in the body of this 
component which discards its body 

如果我用一些任意文本替换wicket:message,它会在任何相关的反馈消息中显示文本。

(有一个相关的JIRA问题:https://issues.apache.org/jira/browse/WICKET-3903但我还是不明白,做了什么来解决这个问题,什么我必须做的......)

刚刚发现有一种方法,在java中做到这一点:

add(new TextField<String>("name").setRequired(true).setLabel(new Model<String>(getString("label.name")))); 

是否有可能以某种方式以更舒适的方式做到这一点?

回答

4

我刚刚测试了以下:

<form wicket:id="form"> 
     <label for="input"><wicket:message key="input">some input</wicket:message></label> 
     <input wicket:id="input" type="text" name="input"> 
     <input type="submit" value="submit"> 
</form> 

并在Java类:

Form<HomePage> form = new Form<HomePage>("form" 
           , new CompoundPropertyModel<HomePage>(this)); 
    wmc.add(form); 

    TextField textField = new TextField("input"); 
    textField.setRequired(true); 
    form.add(textField); 

在我所提供的属性文件:

input=SomeInputField 

这导致了下面的屏幕(如果我将请求的字段留空并按提交。

enter image description here

这是你在找什么?

+0

啊所以诀窍是命名标签完全相同的字段没有李我做的“label.name”不会工作“名称”,但是正在为字符串资源工作。 thx :) – Yashima 2012-07-10 13:13:44

3

这里是一个替代方法@bert's这一直为我工作(不知道的<wicket:label>

FormComponent所示,当发生时通过的FormComponent.setLabel(IModel)来指定验证错误的文本。显示的文本将是IModelgetObject()的结果。

TextField comp = new TextField("comp"); 
// Use internationalized text from XML resource file 
comp.setLabel(new StringResourceModel("formResources.comp.label", this, null)); 

声明本无关与<label>也不FormComponentLabelFormComponentLabel是一个可用于对<label>标签进行建模的组件。

你甚至可以子类FormComponentLabel提供基于FormComponent.getLabel()标签文本,也许输出时,​​需要该领域的额外标志:

public class MyLabel extends SimpleFormComponentLabel{ 
    private boolean required; 
    public MyLabel (String id, LabeledWebMarkupContainer labelProvider) { 
    super(id, labelProvider); 
      if (labelProvider instanceof FormComponent){ 
       required = ((FormComponent)labelProvider).isRequired(); 
     } 
    } 

    protected void onComponentTagBody(final MarkupStream markupStream, 
             final ComponentTag openTag) { 
     String mark = ""; 
     if (required){ 
      // could be for instance "*" 
      mark = getString("formResources.requiredField"); 
     } 
     String text = getModelObjectAsString() + mark; 
     replaceComponentTagBody(markupStream, openTag, text); 
    } 
} 

{ 
TextField component = new TextField("component"); 
component.setRequired(true); 
component.setOutputMarkupId(true); 
IModel labelModel = new StringResourceModel("formResources.component.label", 
              this, null); 
component.setLabel(labelModel); 
add(component); 
add(new MyLabel("componentLabel", component); 
} 

<label wicket:id="componentLabel"/> 
<input type="text" wicket:id="component"/> 

这样,您将有

    清洁方式
  1. FormComponent的文本设置为国际化资源字符串
  2. <label> t重复使用完全相同的资源字符串并根据FormComponent的属性添加自定义标记。
+0

我已经知道setLabel()方法,它确实工作得很好。我正在寻找上面更简单的方法,它不那么冗长 - 在java中。一旦我的应用程序变得更加复杂,但我很确定我会回来看看你的方法。谢谢! – Yashima 2012-07-10 13:19:09

+0

有趣的解决方案。我会继续在脑海中为进一步的参考。 – bert 2012-07-10 19:21:49

3

另一种方法是使用<wicket:label/>的关键属性,就像这样:

<label wicket:for="name"> 
    <wicket:label key="label.name">Placeholder label</wicket:label> 
</label> 
<input wicket:id="name" type="text"/> 

不幸的是这个属性没有文档描述检票口的XHTML标签的wiki页面。在处理标签的类中使用JavaDoc记录所有支持的属性(org.apache.wicket.markup.html.form.AutoLabelTextResolver)。

这种替代方法的优点是不需要额外的编码。

1

Wicket抛出一个例外,告诉您将删除您的<wicket:message>标签,因为<wicket:label>标签的主体被替换。问题是你不能在<wicket:label>标签内嵌套<wicket:message>标签(并且不需要)。

无论这个(选项1):

<label wicket:for="name"> 
    <wicket:label key="label.name"/> 
</label> 
<input wicket:id="name" type="text /> 

或本(选项2):

<label wicket:for="name"> 
    <wicket:message key="label.name"/> 
</label> 
<input wicket:id="name" type="text /> 

应该为你工作,并导致HTML类似如下(假设属性文件包含label.name=Name):

<label for="someMarkupId"> 
    Name 
</label> 
<input id="someMarkupId" type="text" /> 

所不同的是,如果你设置l阿贝尔对于像这样通过Java代码的组件:使用选项1将导致标签

component.setLabel(new Model("value set in code")); 

然后被设置为“值在代码中设置”,而使用选项2仍然会导致标签设置为“名称”。另外,如果标签是通过Java代码设置的,并且属性文件中缺少密钥,则选项2将引发异常,而选项1将仅使用代码中设置的值。

0

我更喜欢这样的:

<label wicket:for="name"><wicket:label />:</label> 
<input type="text" wicket:id="name"></input> 

只要确保设置使用setLabelFormComponent的标签,因此唯一需要的就是java:

add(new TextField("name", nameModel).setLabel(Model.of("i18n.name")));  

这将呈现为(在荷兰):

<label id="name63-w-lbl" for="name63">Naam:</label> 
<input type="text" value="" name="name" id="name63">