繁体   English   中英

如何在 netbeans 12 中创建 i18n Web 应用程序?

[英]How to create a i18n web application in netbeans 12?

我在 netbenas 12 中创建了一个 Web 应用程序,并尝试为 i18n 做一些基本的事情。 我的项目结构是: 在此处输入图片说明

TestDriver.java我有以下代码:

public class TestDriver {

public static void main(String [] args){
    String lang = "fr";
    String country = "FR";
    Locale locale = new Locale(lang, country);
    
    ResourceBundle myBundle = ResourceBundle.getBundle("demo.test", locale);
    String key = myBundle.getString("wish");
    System.out.println(key);
}
 

}

当我使用右键单击>运行文件执行此类时,出现以下错误:

    -----------------------< com.mycompany:demoI18n >-----------------------
Building demoI18n 1.0-SNAPSHOT
--------------------------------[ war ]---------------------------------

--- exec-maven-plugin:1.5.0:exec (default-cli) @ demoI18n ---
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name demo.test, locale fr_FR
    at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
    at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
    at i18n.TestDriver.main(TestDriver.java:24)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:764)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:711)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:289)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  3.097 s
Finished at: 2021-10-11T23:23:30-04:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project demoI18n: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我不知道如何解决这个错误。 有什么建议吗?

下载 InternationalizeDemo.zip 项目并将其解压缩到您计算机上的任何位置。

选择 File > Open Project,导航到您在上一步中提取的 InternationalizeDemo 项目,然后单击 Open。 项目文件夹可能位于也称为 InternationalizeDemo 的包含文件夹中。

展开 Source Packages > Demo 并双击 FindDialog.java 。 示例表单在 GUI Builder 中打开。

要下载它,请参阅此官方文档-https://netbeans.apache.org/kb/docs/java/gui-automatic-i18n.html

我用 NetBeans 中的 Maven 项目重现了您的问题,但是如果我创建了一个 Ant 项目,它可以正常工作。 因此,问题不在于您的 Java 代码,而在于 Maven 配置。

您收到MissingResourceException是因为在 Maven 构建过程中您的演示目录中的属性文件没有被复制到target目录。 如果您构建项目,然后单击“文件”选项卡并在 NetBeans 中展开target目录,您就可以看到这一点; 没有属性文件! 这解释了堆栈跟踪开始处的“无法找到基本名称 demo.test,语言环境 fr_FR 的包”错误消息。

有多种方法可以解决此问题,但最简单的方法是编辑pom.xml以指定在 Maven 构建的复制阶段应包含src/main/java下的所有属性文件。 只需在<build>标签后立即插入:

<resources>
    <resource>
        <directory>src/main/java/</directory>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>        

这会导致在构建过程中将属性文件复制到target ,以便在运行时可以访问它们:

复制道具

然后,当您运行文件TestDriver.java 时,一切正常。

有关更多信息,请参阅包含和排除文件和目录以及 Maven 文档中的指定资源目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM