简体   繁体   中英

Best practice for entities localization in java spring web applications

i am seeking for the best way to localize dynamic(user generated) content on my web. I am using spring-mvc which i found very good framework but now i need to make several entities available in multiple languages. I found out that for static texts i18n is best way which i agree but if i understood it right it can't be used to localize something stored in database.

There for i have to store both localized and original content within database and now i need to know what is the best way to do so for example i have entity :

@Entity 
public class Article {

    private Long id;

    private String title;

    private String body;

}

how should it look if i want it to support localization?

@Entity 
public class Article {

    private Long id;

    @OneToMany
    private Set<LocalizedTitle> localizedTitles;

    private String body;

}

i don't like this solution tbh but i can't come up with a better way that's why i am coming to this place ... may be there is something built in in jpa/hibernate that i can use?

Thank you for your help

I would make the whole article localizable:

Article container:

@Entity
public class Article{

    @Id
    private Long id;

    @OneToMany(mappedBy="container")
    private Set<LocalizedArticle> localizedArticles;

}

Localized versions:

@Entity
public class LocalizedArticle{

    @ManyToOne
    private Article container;

    @Id
    private Long id;

    private String body;

    private String title;

    private String locale;

}

And in your queries, you would look up the localized versions by locale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM