【发布时间】:2019-11-07 05:11:03
【问题描述】:
我有两个实体的字段我想本地化。但是,我不确定如何正确实现它,因为我需要对实体的引用以及对已翻译字段的引用,才能拥有一个共享的“i18n”表。
@Entity
public class EntityA {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Translation> name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Translation> description;
}
第二个实体
@Entity
public class EntityB {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Translation> name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Translation> shortDescription;
}
翻译实体
@Entity
@Table(name = "i18n")
public class Translation {
private String languageCode;
private String translation;
//private String referenceToEntity
//private String referenceToField
}
是否有一种给定的方法可以在 Spring 中启用实体字段的国际化,或者至少有某种解决方法可以使其在没有太多开销的情况下工作?
编辑
我写了一篇关于我如何使用 XmlAnyAttribute https://overflowed.dev/blog/dynamical-xml-attributes-with-jaxb/ 解决它的短文
【问题讨论】:
-
你好。这些 name 和 shortDescription 字段是否具有固定的可能值?这些会是用户输入吗?
-
@GabrielPimenta 你好。它应该是用户对每个字段的灵活和可编辑的,而不是固定的
-
我通常使用 SessionLocaleResolver 和 LocaleChangeInterceptor 进行国际化。数据通常是从属性文件中提取的。每种语言都有一个属性文件。此处示例:mkyong.com/spring-mvc/spring-mvc-internationalization-example
-
@benji2505 由于数据是动态的,在属性上使用转换表是否有意义?我猜我们需要一个更动态的解决方案......
标签: java spring hibernate spring-boot internationalization