Fórumok

Change langugage programatically

Toshiro Mifune, módosítva 9 év-val korábban

Change langugage programatically

Regular Member Bejegyzések: 238 Csatlakozás dátuma: 2014.12.11. Legújabb bejegyzések
Hi all, I tried to change language in my file.vm this way :

$themeDisplay.getLanguageId() // there it is en_EN
$themeDisplay.setLanguageId('sk_SK')
$themeDisplay.getLanguageId() // there it is sk_SK (Slovak)

or this way ...

#set ($locale=$localeUtil.fromLanguageId("sk_SK"))
$themeDisplay.setLocale($locale)
$themeDisplay.getLocale()

I tried to change language of Web Content, where is more translations. So default is English and I want to change it programatically to another language. Ho it is possible?
thumbnail
Andew Jardine, módosítva 9 év-val korábban

RE: Change langugage programatically

Liferay Legend Bejegyzések: 2416 Csatlakozás dátuma: 2010.12.22. Legújabb bejegyzések
Hi Toshiro,

I'm not sure whether or not I have totally understood your question, but if I have, you are looking for force a language. If that is correct, then I think you need to do this on the way in rather than in the template (which is on the way out). That way when the request is processed, the language you want to use is already there.

If this is correct that you can try to do the following --

1. Create a Servlet Filter hook or a Service Pre Action hook.
2. In the hook add something similiar to the following --


	request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.<your-country>);

	LanguageUtil.updateCookie(httprequest, httpresponse, Locale.CANADA);
	ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
	
         if ( Validator.isNotNull(themeDisplay) )
		themeDisplay.setLanguageId( Locale.<your-country>.toString() );
	
	if ( Validator.isNotNull(PortalUtil.getUser(request)) )
		PortalUtil.getUser(httprequest).setLanguageId(Locale.<your-country>.toString());
</your-country></your-country></your-country>


That will set the values on the way in for the request, overriding what is there in the first place. Alternatively, if you know your user has their locale set correctly in their browser, then you can use this property --


 #
    # Set this to true if unauthenticated users get their preferred language
    # from the Accept-Language header. Set this to false if unauthenticated
    # users get their preferred language from their company.
    #
    locale.default.request=false


... Setting it to true will tell Liferay to use whatever the user is passing in as the Accep-Language value for the Http Request header as the language value.
Toshiro Mifune, módosítva 9 év-val korábban

RE: Change langugage programatically

Regular Member Bejegyzések: 238 Csatlakozás dátuma: 2014.12.11. Legújabb bejegyzések
For example : on the top of page are usually buttons where you can switch between languages ... I dont know how this works in liferay ... but similar function of these buttons I try to do ...

I hope this question is more clear
thumbnail
Miroslav Ligas, módosítva 9 év-val korábban

RE: Change langugage programatically

Regular Member Bejegyzések: 152 Csatlakozás dátuma: 2014.07.29. Legújabb bejegyzések
If you need to switch the language us the language portlet. You may include it in the theme to place the flags in a proper space.

           
            <div id="language-portlet">
				#set ($VOID = $velocityPortletPreferences.setValue('portlet-setup-show-borders', 'true'))
				#set ($VOID = $theme.runtime("82", '', $velocityPortletPreferences.toString()))
				#set ($VOID = $velocityPortletPreferences.reset())
            </div>
Toshiro Mifune, módosítva 9 év-val korábban

RE: Change langugage programatically

Regular Member Bejegyzések: 238 Csatlakozás dátuma: 2014.12.11. Legújabb bejegyzések
Thank you, indeed ! !!!!!!!!!!!!! This is exactly what I wanted :-) emoticon