Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在xgettext的bean验证注释中标记字符串消息?_Java_Internationalization_Gettext_Bean Validation - Fatal编程技术网

Java 如何在xgettext的bean验证注释中标记字符串消息?

Java 如何在xgettext的bean验证注释中标记字符串消息?,java,internationalization,gettext,bean-validation,Java,Internationalization,Gettext,Bean Validation,我正在为i18n使用bean验证和gettext。如何标记要翻译的消息字符串,以便使用xgettext提取它? 比如说 @NotNull(message="Please enter a valid string") String string; Normall我称为i18n.tr,但如何标记常量 问候 基督教徒 编辑: 在运行时,我使用一个自定义消息插值器进行翻译。我不确定gettext与Java的集成。也许你可以解释一下它是如何工作的 从Bean验证的角度来看,i18n是通过资源文件处理的。

我正在为i18n使用bean验证和gettext。如何标记要翻译的消息字符串,以便使用xgettext提取它? 比如说

@NotNull(message="Please enter a valid string")
String string;
Normall我称为i18n.tr,但如何标记常量

问候 基督教徒

编辑:
在运行时,我使用一个自定义消息插值器进行翻译。

我不确定gettext与Java的集成。也许你可以解释一下它是如何工作的

从Bean验证的角度来看,i18n是通过资源文件处理的。您可以执行以下操作,而不是将消息直接添加到代码中:

@NotNull(message="{my.notNull.message}")
String string;
然后在ValidationMessages.properties及其特定于语言的计数器部件中定义消息。不确定gettext在这里的图片中的位置

编辑

如果您真的想使用xgettext,我发现问题在于gettext查找gettext形式的标记(“随便什么”)。通过xgettext可以做的事情是通过-k选项为gettext指定不同的关键字。但在这种情况下,这并没有帮助。如果您通过命令行执行所有这些操作,我可以想象您使用sed为xgettext准备输入。比如:

find . -name "*.java" | xargs sed -e 's/message="\(.*\)"/gettext("\1")/' | xgettext 
类似这样的东西。

您可以尝试构建一个委托给gettext的代理。如果您使用的是Hibernate验证器,那么从中派生插值器实现以重用实际的插值逻辑可能是有意义的


这就是说,我对这个结果非常感兴趣。也许你可以分享你最终采取的方法?我可以想象其他人也会对此感兴趣。

我通常不会回答自己的问题。但目前我提出了以下解决方案:

我在附加注释中标记我的字符串如下(我知道不再干燥):

我在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
该脚本首先正常调用xgettext,然后调用sed以删除注释斜杠和指向xgettext的管道。所以我所有的钥匙都在钥匙罐里

pom.xml-配置文件:

    <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>

我有一个GITHUB项目(),它从java字节码中提取消息而不是源,它不应该太难修改它来考虑注释值。让我知道这是否是一个有用的补充。这肯定是一个很好的图书馆。如果有可能考虑注释值,就应该为完整性做这件事。也许只有带花括号的注释值?此外,还应添加GettextMessageInterpolator以实现JSR303兼容性。我正在使用自定义MessageInterpolator。这不是问题所在。问题是关于xgettext,如何标记字符串,这样xgettext就可以识别它,并且字符串会自动进入为翻译人员生成的po文件中。我正在使用自定义MessageInterpolator来翻译消息。我希望对所有内容(javascript、java、注释等)采用统一的i18n方法。
    <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>
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);
    }

}