The embedded option you provide is not an option for us. We don’t know what languages will be used at run-time and adding an extra column for every language in general is smelly.
I tried multiple strategies today with a jpa AttributeConverter/Embeddables/Non-persistent/… but I can’t get it working in Cuba.
my latest attempt fails with:
java.lang.IllegalStateException: Can’t find range class ‘com.company.sample.entity.LocalizedString’ for property ‘sample$Customer.description’
(see code below, attributeconverter) because the type is not known to Cuba.
This is a pretty huge deal for us, and we need a solution. We also need front-end support (web). I tried looking for the component that is used in the entity designer for localization, but that is not included in the framework. The documentation is lacking in regards to making custom components. Id like to see an example on how to make something like that component (inputfield and button leading to a popup)
Can you please provide support for a decent implementation and/or a timeline on build-in support.
Here my latest attempt. (preview messes up syntax highlighting for some reason)
public class TranslatedString {
private String isoCode, message;
public TranslatedString(final String isoCode, final String message) {
this.isoCode = isoCode;
this.message = message;
}
public String getIsoCode() {
return isoCode;
}
public String getMessage() {
return message;
}
}
public class LocalizedString {
private Map<Locale, String> translations = new HashMap<>();
private Locale currentlocale;
public LocalizedString(Map<Locale, String> translations, Locale locale) {
currentlocale = locale;
this.translations = Maps.newHashMap(translations);
}
public LocalizedString(Map<Locale, String> translations) {
this(translations, null);
}
public String getCurrentLocaleMessage() {
return translations.get(currentlocale);
}
public String getMessage(Locale locale) {
return translations.get(locale);
}
public void setCurrentLocale(final Locale locale) {
this.currentlocale = locale;
}
public Map<Locale, String> getTranslations() {
return translations;
}
}
@Converter
public class LocalizedStringToJsonConverter implements AttributeConverter<LocalizedString, String> {
@Override
public String convertToDatabaseColumn(final LocalizedString localized) {
final Gson gson = new GsonBuilder().create();
return gson.toJson(localized);
}
@Override
public LocalizedString convertToEntityAttribute(final String json) {
final Gson gson = new GsonBuilder().create();
return gson.fromJson(json, LocalizedString.class);
}
}
Could you provide a small test project containing your experiments and an explanation what works and what doesn’t. We would help you to fix it and it would be a common ground for other experiments, including visual components.
The application allows registering cats and dogs. (I’m gonna make on this)
approach 1: using tables
For dogs, the breed is localized. this works but has some drawbacks, one is having to register a datasource at the dog level in every screen. If the number of localizable messages gets high this is gonna be annoying, and it’s easy to forget.
There is an LocalizableEntityMessageListener that sets the user language before detach.
approach 2: jpa custom converter
For cats, their favourite food is localized. (fish/poisson/vis/isda)
This code is commented out in the Cat entity because deployment of the war fails with the IllegalstateException this ticket is created for. Also, this type is simply not accepted in studio. There would be a need to be a able to register custom datatypes in studio. Or more specifically, Cuba should support jpa AttributeConverters.
Solution 2 is most suited for my needs, and feels simpler.
My ideal solution would be:
transparant, except for adding a property to an entity, no extra actions should be taken/
linked to a dedicated ui component for editing the values
integrated in the session, user’s current language
integrated in reporting, as a parameter (user language != report reader language)
Hopefully, you can help me out here. As said this is an important requirement for me.
Regards,
Tom
Unfortunately, the datatype alone is not enough to display the values in UI in a sensible manner. It will just convert between JSON and object representation.
So I’ve created a straightforward implementation of UI using field/cell generators which allows a user to view and edit a translated value in the current locale. A more convenient “no boilerplate” solution can be created by implementing a specific UI components. But it’s a more difficult task and it depends on how it should look from the user’s perspective.