掲示板

Change site language programmatically

8年前 に Gerard Szczepanski によって更新されました。

Change site language programmatically

New Member 投稿: 2 参加年月日: 16/03/18 最新の投稿
Hello everyone!
I need to change site language programmatically in Liferay to English and Im stuck. I thought that I can use LanguageUtil class, but I dont know how to do that.

Is there any way, to change Liferay language programmatically? (I know I can do that in Administration Panel, but I need to have this feature in my code also).

I will be gratefull for any help.
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Change site language programmatically

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
The language being displayed is based upon the browser, not determined by the back end...
thumbnail
7年前 に Marcus Hjortzén によって更新されました。

RE: Change site language programmatically

Junior Member 投稿: 45 参加年月日: 12/05/02 最新の投稿
David H Nebinger:
The language being displayed is based upon the browser, not determined by the back end...

Nah, that's not entirely correct. Certainly it is the default behaviour if the user doesn't set a preferred language.

Anyway, one easy way to change language is to prepend the language-identifier to the URL.
I.e. for the URL
http://mydomain.com/web/start/the-page-im-looking-at

generate
http://mydomain.com/en/web/start/the-page-im-looking-at

or (for swedish)
http://mydomain.com/se/web/start/the-page-im-looking-at


There's API's to change the language for the user as well, kind of depends upon if you wish to change the language temporarily or for each login.

Kind regards,
Marcus
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Change site language programmatically

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
But the point is that language choice should be left to the user (their browser), not determined by the back end.

If I hit a site hosted in Canada, even though they may prefer doing the whole site in French, because of my language preference, I should see it in English if it is available.
thumbnail
7年前 に Marcus Hjortzén によって更新されました。

RE: Change site language programmatically

Junior Member 投稿: 45 参加年月日: 12/05/02 最新の投稿
David H Nebinger:
But the point is that language choice should be left to the user (their browser), not determined by the back end.

I'd say that it isn't as easy as that. When talking about public web sites, perhaps.
When talking about for instance, intranets. Well, there's usually a default language that's applicable to that organization and used by most logged in users. Those who are more comfortable with another language can opt out and choose another, manually. But discussions and blogs are probably still only in the default language.

Regarding browser language settings, eventhough I'm native swedish I use all my operating systems in default installations (have you ever seen localized java stacktraces, and tried to google the errors?!), also my browsers are default. Which means english all way through.

I believe a lot of users use their computers/browsers the same way.

So, in theory, yeah, let the browsers negotiate the language!
But in practise there are loads of use cases where the language should be set and persisted, if the user so chooses.

My 2 cents, from a real world example.

//Marcus
thumbnail
7年前 に Olaf Kock によって更新されました。

RE: Change site language programmatically

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Marcus Hjortzén:
My 2 cents, from a real world example.


I guess there's no fully correct answer to this question - at least not for everyone. Everybody is experimenting with stuff - and even you state that some language might be good for "most" logged in users. Once you are defaulted to a language you don't know: Try to find the setting that allows you to change it to another one. Have you ever had someone switch your telephone to a foreign language and tried to find the menu item to switch it back?

I've been through many rounds of websites trying to smartly determine who and where I am, forcing some language onto me - the least annoying method to me was negotiation of the browser. Thinking of all the sites that have different places or options to change the language (Liferay is only one of them - some support it some don't), the only common element is the browser. I'd argue that this is the best place to tackle this problem, but I admit that this is largely a personal preference (of course, as it's my (TM) preference, there must be something to it ;) )