Forums de discussion

Programatically change language

Miguel Ancochea, modifié il y a 15 années.

Programatically change language

New Member Publications: 7 Date d'inscription: 28/04/08 Publications récentes
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, modifié il y a 15 années.

RE: Programatically change language

New Member Publications: 7 Date d'inscription: 28/04/08 Publications récentes
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, modifié il y a 15 années.

RE: Programatically change language

Junior Member Publications: 55 Date d'inscription: 20/06/08 Publications récentes
Hey there,

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

Thanks!
Affar Le Rafee, modifié il y a 14 années.

RE: Programatically change language

Junior Member Publications: 29 Date d'inscription: 18/01/09 Publications récentes
Has anyone found a solution to this problem???????
Richard Becher, modifié il y a 14 années.

RE: Programatically change language

Junior Member Publications: 55 Date d'inscription: 20/06/08 Publications récentes
Nope....not yet.
T FoX, modifié il y a 13 années.

RE: Programatically change language

Junior Member Publications: 34 Date d'inscription: 02/03/10 Publications récentes
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, modifié il y a 13 années.

RE: Programatically change language

New Member Publications: 3 Date d'inscription: 12/10/10 Publications récentes
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, modifié il y a 12 années.

RE: Programatically change language

Regular Member Publications: 127 Date d'inscription: 14/07/09 Publications récentes
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, modifié il y a 12 années.

RE: Programatically change language

Regular Member Publications: 158 Date d'inscription: 05/03/10 Publications récentes
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, modifié il y a 9 années.

RE: Programatically change language

New Member Publications: 3 Date d'inscription: 09/04/09 Publications récentes
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