Best practice for localizing objects in java spring web applications

I am looking for the best way to localize dynamic (user created) content on my website. I am using spring -mvc, which has found a very good structure, but now I need to make several objects available in several languages. I found out that for static texts i18n is the best way that I agree, but if I understood it correctly, it cannot be used to localize anything stored in the database.

There I have to store both localized and original content in the database, and now I need to know that this is the best way to do this, for example, I have an entity:

@Entity public class Article { private Long id; private String title; private String body; } 

What does it look like 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 tbh solution, but I can't think of a better way why I come to this place ... maybe there is something built into jpa / hibernate that I can use?

thanks for the help

+4
source share
1 answer

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 will search for localized versions by language.

+6
source

Source: https://habr.com/ru/post/1332347/


All Articles