Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:15:12+00:00 2026-06-09T08:15:12+00:00

I am using bean validation and gettext for i18n. How can I mark the

  • 0

I am using bean validation and gettext for i18n. How can I mark the message string for translation, so it gets extracted with xgettext?
For example

@NotNull(message="Please enter a valid string")
String string;

Normall I call i18n.tr, but how to mark a constant?

Kind regards
Christian

Edit:
At runtime I am using a custom message interpolator for translation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:15:14+00:00Added an answer on June 9, 2026 at 8:15 am

    I am normally not answering my own questions. But for now I came up with following solution:

    I am marking my strings as follows in an additional comment (I know not DRY anymore):

    //_.trans("Please enter a valid string");
    @NotNull(message="Please enter a valid string")
    String string;
    

    I am calling following script in my pom:

    #!/bin/bash
    
    # $1 -> java source directory
    # $2 -> output file
    # $3 -> po directory
    
    echo "Source Directory: $1"
    echo "Keys File: $2"
    echo "PO Directory: $3"
    
    xgettext --from-code utf-8 -L Java --force-po -ktrc:1c,2 -ktrnc:1c,2,3 -ktr -kmarktr -ktrn:1,2 -k -o "$2" $(find "$1" -name "*.java")
    sed "s/\/\/_/_/g" $(find "$1" -name "*.java") | xgettext -F --from-code utf-8 -L Java -ktrans -k -j -o "$2" -
    
    pofiles=$3/*.po
    shopt -s nullglob
    for i in $pofiles
    do
       echo "msgmerge $i"
       msgmerge --backup=numbered -U $i $2
    done
    

    This script first calls xgettext normally and then calls sed to remove the comment slashes and pipes to xgettext. Thus I have all my keys in keys.pot.

    pom.xml – profile:

        <profile>
            <id>translate</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>exec-maven-plugin</artifactId>
                        <groupId>org.codehaus.mojo</groupId>
                        <version>1.2.1</version>
                        <executions>
                            <execution>
                                <id>xgettext</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>sh</executable>
                                    <arguments>
                                        <argument>${project.basedir}/extractkeys.sh</argument>
                                        <argument>src/main/java</argument>
                                        <argument>src/main/resources/po/keys.pot</argument>
                                        <argument>src/main/resources/po</argument>
                                    </arguments>
                                    <workingDirectory>${project.basedir}</workingDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.xnap.commons</groupId>
                        <artifactId>maven-gettext-plugin</artifactId>
                        <version>1.2.3</version>
                        <configuration>
                            <keysFile>${project.basedir}/src/main/resources/po/keys.pot</keysFile>
                            <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                            <outputFormat>properties</outputFormat>
                            <poDirectory>${project.basedir}/src/main/resources/po</poDirectory>
                            <sourceDirectory>${project.build.sourceDirectory}/ch/sympany/tourist</sourceDirectory>
                            <sourceLocale>en</sourceLocale>
                            <targetBundle>${project.groupId}.Messages</targetBundle>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>dist</goal>
                                </goals>
                                <phase>generate-resources</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    I know the build is not platform independent anymore but in a separate profile I can live with it. However, it works also on cygwin for the windows guys.

    My messageinterpolator is as follows:

    public class GettextMessageInterpolator implements MessageInterpolator {
    
        private final MessageInterpolator delegate;
    
        public GettextMessageInterpolator() {
            this.delegate = new ResourceBundleMessageInterpolator();
        }
    
        @Override
        public String interpolate(String message, Context context) {
            return this.interpolate(message, context, ClientLocalLocator.get());
        }
    
        @Override
        public String interpolate(String message, Context context, Locale locale) {   
            I18n i18n = ClientLocalLocator.getI18n();
            String retVal = i18n.tr(message);
            if (StringUtils.isNotBlank(retVal))
                return retVal;
            return delegate.interpolate(message, context, locale);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using two validation annotations on a property in the bean: @NotEmpty(message =
I'm trying to integrate bean validation using hibernate validator to my web application :
I'm using JSF 2.0. I have a managed bean which I can access through
I am using JSR 303 Bean validation in my JSF 2.0 web application and
When using annotation based validation for a form bean, what is the best practice
In JSF you can display validation error's using <h:messages /> . But what I
I have some questions about Bean Validation and JSF validation, currently I am using
I am using Hibernate Validation annotations in my JSF managed bean. When I use
I am trying to add a regExp to my Bean using Hibernate bean Validation.here
I want to call a method after creating the bean using spring, for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.