Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using i18n in groovy script

T I
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.
July 24, 2013

Hi,

could someone tell me how to use i18n in groovy script on this example:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
 
 def locales = [new Locale("en", "US"), new Locale("de", "DE")]
  
// loop all versions
for (int i = 0; i < issue.getFixVersions().size(); i++) {
    if ((issue.getFixVersions().get(i).released)) {
        //Only execute if the fix version is released
        invalidInputException = new InvalidInputException("assignee", "Issue could not be reopened. Version \"" + issue.getFixVersions().get(i).name + "\" has already been released.")
        break;
    }
}

I would like to translate the following: "Issue could not be reopened. Version \"" + issue.getFixVersions().get(i).name + "\" has already been released." .

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
JamieA
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.
July 24, 2013

Same as the java way probably. You should add your strings to a resource bundle, then you use the i18nHelper... for example (to test this you need greenhopper installed), you can run the following in the script console;

import com.atlassian.jira.component.ComponentAccessor

ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getText("gh.boards.linefieldcomponentboard")

If you change your language to French via your profile you will get different text.

If you want to do it the hacky way (I approve) and handle it all in your script, you can get the language code like:

ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getLocale().getLanguage()

T I
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.
July 25, 2013

Ok, I'm somehow using it wrong.

checkVersionIsReleased.groovy:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor

def firstPart = ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getText("com.test.checkVersionIsReleased.firstPart") 
def secondPart = ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getText("com.test.checkVersionIsReleased.secondPart") 
  
// loop all versions
for (int i = 0; i < issue.getFixVersions().size(); i++) {
    if ((issue.getFixVersions().get(i).released)) {
        //Only execute if the fix version is released
        invalidInputException = new InvalidInputException("assignee", firstPart + issue.getFixVersions().get(i).name + secondPart)
        break;
    }
}

checkVersionIsReleased.properties:

com.test.checkVersionIsReleased.firstPart = "Issue could not be reopened. Version \""
com.test.checkVersionIsReleased.secondPart = "\" has already been released."

checkVersionIsReleased_de.properties:

com.test.checkVersionIsReleased.firstPart = "Der Vorgang konnte nicht geöffnet werden. Version \""
com.test.checkVersionIsReleased.secondPart = "\" wurde schon veröffentlicht."

The result is:

"com.test.checkVersionIsReleased.firstPartTest The Released Version com.test.checkVersionIsReleased.secondPart"

T I
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.
July 25, 2013

You meant ? :

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
   
// loop all versions
for (int i = 0; i < issue.getFixVersions().size(); i++) {
    if ((issue.getFixVersions().get(i).released)) {
        //Only execute if the fix version is released
        invalidInputException = new InvalidInputException("assignee", ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getText("com.test.checkVersionIsReleased.firstPart") + issue.getFixVersions().get(i).name + ComponentAccessor.getJiraAuthenticationContext().getI18nHelper().getText("com.test.checkVersionIsReleased.secondPart"))
        break;
    }
}

All files are in: C:\Program Files\Atlassian\Application Data\JIRA

JamieA
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.
July 25, 2013

You can put parameters in your i18n messages btw.

So it's failing to find the resource bundle clearly. Where have you put your properties files?

JamieA
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.
July 25, 2013

I meant https://developer.atlassian.com/static/javadoc/jira/6.0/reference/com/atlassian/jira/util/I18nHelper.html#getText(java.lang.String, java.lang.String) or one of its friends.

The resource bundle needs to be on the classpath, that location isn't afaik. You prob need to restart as well to have jira pick it up.

TAGS
AUG Leaders

Atlassian Community Events