How can i change language in jira from Jira API in Java ?

Mik November 13, 2013

Hi Guys.

How can i change default language for user in jira from Jira API in Java ?

I must do it in my own plugin.

Thanks for any help.

Regards

Mik

3 answers

2 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 13, 2013

Are you sure this is necessary? The default language is a one-off setting which your admin will generally set during installation. It's rare to need to change it again, and even then you really really want your admins to do it because they'll think about it. I'd never use a plugin that did that because I absolutely do not want the default language to change without me doing it explicitly.

1 vote
Frédéric Tardieu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2013

Hi Mik,

Here's how I do:

private Locale getLocale(User user)
	{
		if (user == null)
			return Locale.getDefault();
		
		String userLocaleString = getUserPreferencesManager().getPreferences(user).getString(PreferenceKeys.USER_LOCALE);
		
		if (userLocaleString == null)
			return Locale.getDefault();
		
		StringTokenizer tokenizer = new StringTokenizer(userLocaleString, "_");
		String language = tokenizer.nextToken();
		String region = tokenizer.nextToken();
		
		if (language != null && region != null)
			return new Locale(language, region);
		else
			return Locale.getDefault();
		
	}

As you can see my entry point is a User (com.atlassian.crowd.embedded.api.User), and from this I get its locale (its locale is the language the user set in its preferences).

Then I instantiate a I18NHelper with this locale:

ComponentAccessor.getI18nHelperFactory().getInstance(getLocale(user));

So that the language used to display 18N Strings is the one set by the user if any, or JIRA server language if user language is not set,

Hope this helps,

Fred

Frédéric Tardieu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2013

I understood the need was to display strings depending on the language set by user...

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 13, 2013

I suspect your understanding was better than mine. The use of the word "default" made me jump because it's rather upsetting for admins to have things they've chosen changed without warning.

But, setting an individual user's preferred language from their chosen region is a friendly and useful thing to do.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 13, 2013

This doesn't change the default language for a user, it sets their preference for them based on their chosen location. (Which is really nice)

Frédéric Tardieu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2013

Yes I agree, I find this more logical to have a language set for the whole instance, and then let users define another one if they want to. The code I pasted is to make a plugin sensitive to this user setting.

0 votes
Mik November 14, 2013

Hi Frédéric,

This is exectly what I needed.

Thanks for discusion guys.

Regards

Mik

Suggest an answer

Log in or Sign up to answer