简体   繁体   中英

How to create a i18n web application in netbeans 12?

I create a web application in netbenas 12 and I try to do basic stuff for i18n. The structure of my project is: 在此处输入图片说明

In the TestDriver.java I have the following code:

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);
}
 

}

When I execute this class using right click>run file i get the following error:

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

I dont know how to solve this error. ANY SUGGESTION?

Download and unzip the InternationalizeDemo.zip project to any location on your computer.

Choose File > Open Project, navigate to the InternationalizeDemo project that you extracted in the last step, and click Open. The project folder might be in a containing folder that is also called InternationalizeDemo .

Expand Source Packages > Demo and double-click FindDialog.java . The sample form opens in the GUI Builder.

To download it refer to this official documentation-https://netbeans.apache.org/kb/docs/java/gui-automatic-i18n.html

I reproduced your problem with a Maven project in NetBeans, but if I create an Ant project instead it works fine. Therefore, the issue is not with your Java code, but with the Maven configuration.

You get the MissingResourceException because the properties files in your demo directory are not being copied to the target directory during the Maven build process. You can see this if you build the project, then click the Files tab and expand the target directory in NetBeans; there are no properties files! This explains the "Can't find bundle for base name demo.test, locale fr_FR" error message at the start of the stack trace.

There are multiple approaches to resolve this, but the simplest is to edit pom.xml to specify that all properties files under src/main/java should be included during the copying phase of the Maven build. Just insert this immediately after the <build> tag:

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

This results in the properties files being copied to target during the build process so that they are accessible at runtime:

复制道具

Then, when you run file TestDriver.java everything should work.

See Including and excluding files and directories and Specifying resource directories in the Maven documentation for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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