Categories
Mastering Development

How can I implement I18n as a JSON object for a JPA or Hibernate entity?

I have a Spring-boot application that uses JPA and Hibernate. You can find the whole code on this GitHub repository.

My question is how can I add internationalization functionality to a specific column without any foreign keys and by using JSON structure?

For example I would like to define a JPA entity like this:

@Entity
class Book {

    @Id
    private int id;

    private Author author;

    @I18n  //<- this annotation is something that I am looking for 
    private String title;

}

and then the data in title column would be stored like the following for en and de locales:

{"en":"Cologne","de":"Köln"}

And then when the current locale is de the Köln and when the en is set as locale then Cologne fetch in the time of reading data!

Also when we store the data, the passed string is stored in the relevant property in the JSON format. For example if the locale is set to es and user passes Kolne then we have to have the following data in the DB:

{"en":"Cologne","de":"Köln","es":"Kolne"}

It is interesting for me that most of the solutions in the web for hibernate and JPA is based on an old method that we have languages and translations tables. Something like here or here.

However what I am looking for is some solutions like this one which is suggested for Laravel and store the translations exactly in the way that I explained (i.e. in a JSON object and in the same column)!

The only solution that I found and could be somehow relevant (Not 100%) is this one, however it does not working when I tried to test it and it seems does not supported anymore!

Leave a Reply

Your email address will not be published. Required fields are marked *