Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: aui localized form: how to write/read localized content?
Hi all,
I'm trying to develop an MVC portlet that manages localized content using Liferay 7.1 CE GA 1.
After many attempts (and many thread in this forum) I ended up with half solution: I'm able to use an aui:form to acquire localized content; but I cannot save the data in any form that is recognized by aui.
I mean:
- in the browser in a field (field1) I enter english and italian translation
- debugging the portlet for edit action I can see that the english and italian translation are there
- I store the field as a map <Locale,String> converted to string
- when I try to edit the record, the field1 content is the strored string assigned to the default language and the translation is lost
How can I write and read localized content so that aui:form is happy and show all translations?
TL;DR
This is what I have and what I attempted.
In service.xml I have
<column name="field1" type="String" localized="true" />
In edit_foo.jsp I have
<aui:form
action="<%= (javax.portlet.ActionURL)renderResponse.createActionURL() %>"
name="fm1"
>
[snip]
<aui:fieldset>
<aui:input name="field1" localized="<%= true %>" />
[snip]
In the browser I typed two translation - see aui-field1.png attachment
You can see that en_US is the default language and that it_IT is translated (BTW when it_IT is selected under the input field the en_US translation is showed)
In MsbOnePortlet.java I have
[snip]
protected void updateFoo(ActionRequest actionRequest) throws Exception {
long fooId = ParamUtil.getLong(actionRequest, "fooId");
boolean field2 = ParamUtil.getBoolean(actionRequest, "field2");
int field3 = ParamUtil.getInteger(actionRequest, "field3");
Set<Locale> locales = LanguageUtil.getAvailableLocales();
Map<Locale, String> map1 = new HashMap<Locale, String>();
for (Locale locale : locales) {
String languageId = LocaleUtil.toLanguageId(locale);
String x1 = "field1";
String localeParameter = x1.concat(StringPool.UNDERLINE).concat(languageId);
String y1 = ParamUtil.getString(actionRequest,localeParameter);
map1.put(locale, y1);
}
String field1 = map1.toString();
System.out.println("mappato field1");
[snip]
Debugging this code I see that field1 contains
{es_ES=, it_IT=Campo 1 testo in italiano, fr_FR=, en_US=Field 1 english text, de_DE=}
See the map and the field in the debug-field1.png attachment
When I try to edit the the row, the edit_foo.jsp page shows the string as the default language translation and that there is no italian translation. See aui-field1-edit.png attachment
I tried also to see how the journal article was developed but I lost myself in the code (https://github.com/liferay/liferay-portal/tree/master/modules/apps/journal )
What I have to do?
Solved. With this code I can use an aui:form to read/write localized content.
In service.xml I have
<column name="field1" type="String" localized="true" />
In edit_foo.jsp I have
<aui:form
action="<%= (javax.portlet.ActionURL)renderResponse.createActionURL() %>"
name="fm1"
>
[snip]
<aui:fieldset>
<aui:input name="field1" localized="<%= true %>" />
[snip]
In MsbOnePortlet.java I have
[snip]
protected void updateFoo(ActionRequest actionRequest) throws Exception {
long fooId = ParamUtil.getLong(actionRequest, "fooId");
[snip]
Set<Locale> locales = LanguageUtil.getAvailableLocales();
Map<Locale, String> field1Map = new HashMap<Locale, String>();
for (Locale locale : locales) {
String languageId = LocaleUtil.toLanguageId(locale);
String x1 = "field1";
String localeParameter = x1.concat(StringPool.UNDERLINE).concat(languageId);
String y1 = ParamUtil.getString(actionRequest,localeParameter);
field1Map.put(locale, y1);
}
[snip]
foo.setField1Map(field1Map);
This create the field1 content:
<?xml version='1.0' encoding='UTF-8'?> <root available-locales="en_US,it_IT," default-locale="en_US"> <Field1 language-id="en_US">f1 english</Field1> <Field1 language-id="it_IT">f1 italiano</Field1> </root>
This content is correctly interpretaded by aui:form which show the field content in all translated languages