掲示板

Change langugage programatically

9年前 に Toshiro Mifune によって更新されました。

Change langugage programatically

Regular Member 投稿: 238 参加年月日: 14/12/11 最新の投稿
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
9年前 に Andew Jardine によって更新されました。

RE: Change langugage programatically

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
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.
9年前 に Toshiro Mifune によって更新されました。

RE: Change langugage programatically

Regular Member 投稿: 238 参加年月日: 14/12/11 最新の投稿
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
9年前 に Miroslav Ligas によって更新されました。

RE: Change langugage programatically

Regular Member 投稿: 152 参加年月日: 14/07/29 最新の投稿
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>
9年前 に Toshiro Mifune によって更新されました。

RE: Change langugage programatically

Regular Member 投稿: 238 参加年月日: 14/12/11 最新の投稿
Thank you, indeed ! !!!!!!!!!!!!! This is exactly what I wanted :-) emoticon