Foros de discusión

Programatically change language

Miguel Ancochea, modificado hace 15 años.

Programatically change language

New Member Mensajes: 7 Fecha de incorporación: 28/04/08 Mensajes recientes
Hi,

We want to change the portal language with our own language-switching links. Therefore, we've thought we need either a way of customizing:

$taglibLiferay.language()

so it shows our own text and style for the links OR we need to change the whole portal language programatically. Is it possible? How?.


One last question: once the language is changed portal-wide how can we get the current language as Locale eg. "en_UK" within any portlet?

We're in a bit of hurry with these. Thanks in advance.
Miguel Ancochea, modificado hace 15 años.

RE: Programatically change language

New Member Mensajes: 7 Fecha de incorporación: 28/04/08 Mensajes recientes
Hi again,

We've seen how to solve the second question, that is, how to query for the current Locale from a portlet:

ThemeDisplay themeDisplay =(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
Locale locale = LocaleUtil.fromLanguageId(themeDisplay.getLanguageId());

It's also possible to use LanguageUtil instead of ThemeDisplay for this operation.

Still, we don't know how to change the language portal-wide for example from a theme velocity template (we have our own theme) or from a portlet.

Anyone?
Richard Becher, modificado hace 15 años.

RE: Programatically change language

Junior Member Mensajes: 55 Fecha de incorporación: 20/06/08 Mensajes recientes
Hey there,

Did you ever find an answer on how to change the language at the Theme level?

Thanks!
Affar Le Rafee, modificado hace 14 años.

RE: Programatically change language

Junior Member Mensajes: 29 Fecha de incorporación: 18/01/09 Mensajes recientes
Has anyone found a solution to this problem???????
Richard Becher, modificado hace 14 años.

RE: Programatically change language

Junior Member Mensajes: 55 Fecha de incorporación: 20/06/08 Mensajes recientes
Nope....not yet.
T FoX, modificado hace 13 años.

RE: Programatically change language

Junior Member Mensajes: 34 Fecha de incorporación: 2/03/10 Mensajes recientes
You could simply create an hook to override the docroot\html\taglib\ui\language\page.jsp file and apply your own styling to the language selector.

You could also change the language programmatically by executed following method:

themeDisplay.setLocale(new Locale("nl"));
Deepak Kenchamba, modificado hace 13 años.

RE: Programatically change language

New Member Mensajes: 3 Fecha de incorporación: 12/10/10 Mensajes recientes
Liferay seems to follow the technique of inserting the "2 character" language string to the URL to force a language change to the entire portal.
The language string is inserted right after the scheme://server:port/ part of the HTTP URL

While this can be done via the Liferay API or the Language Portlet (as explained above), it can also be done with a nifty javascript which follows the "2 character" language string insertion technique.
Pick up the current browser URL value - window.location & update it switch to the language of choice & force a reload. Voila, the language switch happens with no portlets involved.


E.g:
Default URL
[indent] http://172.16.2.237:8080/web/testtoday/home1[/indent]
Modified URL
[indent] http://172.16.2.237:8080/ar/web/testtoday/home1[/indent]

Notice that the second URL forces Liferay to render the portal in Arabic!
thumbnail
David García González, modificado hace 12 años.

RE: Programatically change language

Regular Member Mensajes: 127 Fecha de incorporación: 14/07/09 Mensajes recientes
I think it is better to change the language programmatically.

http://www.liferay.com/es/community/forums/-/message_boards/message/9380899
thumbnail
Achmed Tyrannus Albab, modificado hace 12 años.

RE: Programatically change language

Regular Member Mensajes: 158 Fecha de incorporación: 5/03/10 Mensajes recientes
Deepak Kenchamba:
Liferay seems to follow the technique of inserting the "2 character" language string to the URL to force a language change to the entire portal.
The language string is inserted right after the scheme://server:port/ part of the HTTP URL

While this can be done via the Liferay API or the Language Portlet (as explained above), it can also be done with a nifty javascript which follows the "2 character" language string insertion technique.
Pick up the current browser URL value - window.location & update it switch to the language of choice & force a reload. Voila, the language switch happens with no portlets involved.


E.g:
Default URL
[indent] http://172.16.2.237:8080/web/testtoday/home1[/indent]
Modified URL
[indent] http://172.16.2.237:8080/ar/web/testtoday/home1[/indent]

Notice that the second URL forces Liferay to render the portal in Arabic!


Haha genius!
thumbnail
Pawel Kruszewski, modificado hace 9 años.

RE: Programatically change language

New Member Mensajes: 3 Fecha de incorporación: 9/04/09 Mensajes recientes
Hi all,

try to use languageId parameter. In my case I had to overwrite redirect url in my struts action hook as I wanted to change locale after user was redirected:

public class CreateAccountAction extends BaseStrutsPortletAction {
    public final void processAction(
            final StrutsPortletAction originalStrutsPortletAction,
            final PortletConfig portletConfig,
            final ActionRequest actionRequest,
            final ActionResponse actionResponse) throws Exception {

        DynamicActionRequest dynamicActionRequest = new DynamicActionRequest(
                actionRequest);
            String redirect =
                    dynamicActionRequest
                            .getParameter(WebKeys.REDIRECT.toLowerCase());
            redirect = HttpUtil
                    .addParameter(redirect, "languageId", languageId);
            dynamicActionRequest.setParameter(
                    WebKeys.REDIRECT.toLowerCase(), redirect);

        originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                portletConfig, dynamicActionRequest, actionResponse);
}


Works really well.

Greets,
Pawel