{"id":5806,"date":"2014-04-07T09:20:32","date_gmt":"2014-04-07T09:20:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/how-to-mark-string-message-in-bean-validation-annotation-for-xgettext-collection-of-common-programming-errors\/"},"modified":"2014-04-07T09:20:32","modified_gmt":"2014-04-07T09:20:32","slug":"how-to-mark-string-message-in-bean-validation-annotation-for-xgettext-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/how-to-mark-string-message-in-bean-validation-annotation-for-xgettext-collection-of-common-programming-errors\/","title":{"rendered":"How to mark string message in bean validation annotation for xgettext?-Collection of common programming errors"},"content":{"rendered":"<p>I am normally not answering my own questions. But for now I came up with following solution:<\/p>\n<p>I am marking my strings as follows in an additional comment (I know not DRY anymore):<\/p>\n<pre><code>\/\/_.trans(\"Please enter a valid string\");\n@NotNull(message=\"Please enter a valid string\")\nString string;\n<\/code><\/pre>\n<p>I am calling following script in my pom:<\/p>\n<pre><code>#!\/bin\/bash\n\n# $1 -&gt; java source directory\n# $2 -&gt; output file\n# $3 -&gt; po directory\n\necho \"Source Directory: $1\"\necho \"Keys File: $2\"\necho \"PO Directory: $3\"\n\nxgettext --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\")\nsed \"s\/\\\/\\\/_\/_\/g\" $(find \"$1\" -name \"*.java\") | xgettext -F --from-code utf-8 -L Java -ktrans -k -j -o \"$2\" -\n\npofiles=$3\/*.po\nshopt -s nullglob\nfor i in $pofiles\ndo\n   echo \"msgmerge $i\"\n   msgmerge --backup=numbered -U $i $2\ndone\n<\/code><\/pre>\n<p>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.<\/p>\n<p>pom.xml &#8211; profile:<\/p>\n<pre><code>    \n        translate\n        \n            \n                \n                    exec-maven-plugin\n                    org.codehaus.mojo\n                    1.2.1\n                    \n                        \n                            xgettext\n                            generate-resources\n                            \n                                exec\n                            \n                            \n                                sh\n                                \n                                    ${project.basedir}\/extractkeys.sh\n                                    src\/main\/java\n                                    src\/main\/resources\/po\/keys.pot\n                                    src\/main\/resources\/po\n                                \n                                ${project.basedir}\n                            \n                        \n                    \n                \n                \n                    org.xnap.commons\n                    maven-gettext-plugin\n                    1.2.3\n                    \n                        ${project.basedir}\/src\/main\/resources\/po\/keys.pot\n                        ${project.basedir}\/src\/main\/resources\n                        properties\n                        ${project.basedir}\/src\/main\/resources\/po\n                        ${project.build.sourceDirectory}\/ch\/sympany\/tourist\n                        en\n                        ${project.groupId}.Messages\n                    \n                    \n                        \n                            \n                                dist\n                            \n                            generate-resources\n                        \n                    \n                \n            \n        \n    \n<\/code><\/pre>\n<p>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.<\/p>\n<p>My messageinterpolator is as follows:<\/p>\n<pre><code>public class GettextMessageInterpolator implements MessageInterpolator {\n\n    private final MessageInterpolator delegate;\n\n    public GettextMessageInterpolator() {\n        this.delegate = new ResourceBundleMessageInterpolator();\n    }\n\n    @Override\n    public String interpolate(String message, Context context) {\n        return this.interpolate(message, context, ClientLocalLocator.get());\n    }\n\n    @Override\n    public String interpolate(String message, Context context, Locale locale) {   \n        I18n i18n = ClientLocalLocator.getI18n();\n        String retVal = i18n.tr(message);\n        if (StringUtils.isNotBlank(retVal))\n            return retVal;\n        return delegate.interpolate(message, context, locale);\n    }\n\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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(&#8220;Please enter a valid string&#8221;); @NotNull(message=&#8221;Please enter a valid string&#8221;) String string; I am calling following script in my pom: #!\/bin\/bash # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5806","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5806"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5806\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}