1

I have two entities with fields that I´d like to localize. However, I´m not sure how to implement that correctly, because I would need to have a reference to the entities as well as a reference to the field that is translated, in order to have a shared "i18n" table.

@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;

}

Second entity

@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;

}

Translation Entity

@Entity
@Table(name = "i18n")
public class Translation {

    private String languageCode;
    private String translation;

    //private String referenceToEntity
    //private String referenceToField


}

Is there a given way to enable internationalization on entity fields in Spring or at least some kind of workaround to make it working without too much overhead?

EDIT

I´ve written a short post about how I solved it using XmlAnyAttribute https://overflowed.dev/blog/dynamical-xml-attributes-with-jaxb/

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
  • Hello. Would those name and shortDescription fields have fixed possible values? Would those be an user input? – Gabriel Robaina Jun 25 '19 at 12:08
  • @GabrielPimenta Hello. It should be flexible and editable for each field by users and not be fixed – Marian Klühspies Jun 25 '19 at 12:24
  • I am doing internationalization usually with a SessionLocaleResolver and a LocaleChangeInterceptor. The data is usually pulled from a properties file. There is one properties file per language. Example here: https://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/ – benji2505 Jun 25 '19 at 12:32
  • 1
    @benji2505 since the data is dynamic, does it make sense to use a translation table on properties? We need a more dynamic solution I guess... – Gabriel Robaina Jun 25 '19 at 12:47

2 Answers2

1

I did some research and found this @Convert JPA annotation. You would need to encapsulate the name and description properties into an object (that implements AttributeConverter), and use a convertion class to specify how it will be translated when persisted, and how will it be translated when retreived.

To execute translations on persistence and retrieval, you can consume Google translate's API.

Here:

@Entity
public class EntityA {

    @Convert(converter = DescriptionConverter.class)
    private Description description

    // getters and setters

},

The encapsulated object, something like:

public class Description {

    private String name;

    private String language;

    private String description;

    // Getters and Setters.

}

And the translation applies here:

@Converter
public class DescriptionConverter implements AttributeConverter<Description, String> {

    @Override
    public String convertToDatabaseColumn(Description description) {
        // consume Google API to persist.
    }

    @Override
    public Document convertToEntityAttribute(String description) {
        // consume Google API to retrieve.
    }

}
Gabriel Robaina
  • 709
  • 9
  • 24
  • Thanks for your research. How would you provide different translations for one field? – Marian Klühspies Jun 25 '19 at 14:37
  • I would first specify in which language the descriptions would be persisted in. Then, whenever a user requests a description, I would use a language property and give it to Google API, along with the original description and the desired translation, for it to translate. – Gabriel Robaina Jun 25 '19 at 14:54
  • I´m not sure if it helps in my case. I would need a list of Translations in each field, it´s not meant to be displayed to a user based on his location, but rather a data model which is used for different exports later – Marian Klühspies Jun 26 '19 at 13:12
  • Since you have dynamic data in each field, it is kinda impossible to have translation tables for those, unless I understood it wrongly. Either way, hope the suggestion was of any help, and that you manage to find a way to solve your problem. – Gabriel Robaina Jun 26 '19 at 19:32
  • That's what I'm also feeling right now. As a workaround I'll now group all translateable fields of one entity into a separate entity, (ex EntityA + EntityATranslations) so I can create one entry across all fields per language. It will be necessary for each individual entity and therefore produce one additional table per entity. Thank you anyway for your help, much appreciated:) – Marian Klühspies Jun 26 '19 at 20:46
0

this tutorial helped me a lot. i hope it will help you too. i used the second way and it's work perfectly.Localized Data – How to Map It With Hibernate