用户定义输入的国际化

时间:2013-05-21 07:54:02

标签: internationalization playframework yaml playframework-2.1 ebean

我正在尝试为用户定义的输入构建localization。示例:用户可以使用多种语言定义类似足球的类别。

模型实体可能有这样的字段:

    @ManyToMany     
    @MapKeyColumn(name = "locale", insertable = false, updatable = false)     
    public Map<String, L18n> titles;

我想存储本地化字符串,如下所示:

@Entity
public class L18n {

    @Id
    @Constraints.Required
    @Formats.NonEmpty
    public Integer id;

    public String key;

    public String locale;

    @Column(columnDefinition = "TEXT")
    public String text;

}

我们使用 yaml 来存储testdata:

    category:

        - !!models.Category
                ...
                titles: 
                    - !!models.L18n
                         key: soccer


    l18n:

         - !!models.L18n
                key: soccer
                locale: de-CH
                text: fdfdfsee

         - !!models.L18n
                key: soccer
                locale: fr-CH
                text: dlfkjsdlfj

使用此解决方案,我收到此错误:

    [error] Caused by: org.yaml.snakeyaml.error.YAMLException: No suitable constructor with 1 arguments found for interface java.util.Map
    [error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:574)
    [error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    [error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:296)
    [error]     ... 65 more

1 个答案:

答案 0 :(得分:0)

我认为问题是您已将标题定义为地图,但不使用YAML映射语法来定义它。 YAML Collections

我认为标题的语法应该类似于使用langauges的示例:YAML Dictionary Example

titles: 
    soccer: - !!models.L18n
                key: soccer
                locale: de-CH
                text: fdfdfsee
    football: - !!models.L18n
              ...

如果您更改.yml文件的格式以便首先定义L18n实例并使用引用,则YAML规范称它们为Alias Nodes,您可以使您的标题看起来更精简:

l18n:

     - &soccerDE !!models.L18n
            key: soccer
            locale: de-CH
            text: fdfdfsee

     - &soccerFR !!models.L18n
            key: soccer
            locale: fr-CH
            text: dlfkjsdlfj

category:

    - !!models.Category
            ...
            titles: 
                soccerDE: *soccerDE
                soccerFR: *soccerFR