Question

I'm pretty new to Vaadin framework and I'm trying to make an internationalized application. I'm using NetBeans and I have followed these steps:

  1. Create a new Vaadin project with a single UI class with a single text field on it.
  2. I went to Tools -> Internationalization -> Internationalization Wizard and have followed those steps to internationalize the UI class.

Pretty simple. But when I run the application I get this exception:

javax.servlet.ServletException: com.vaadin.server.ServiceException: java.util.MissingResourceException: Can't find bundle for base name com/mycompany/i18ntest/Bundle, locale es_AR
    com.vaadin.server.VaadinServlet.service(VaadinServlet.java:240)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

The exception suggests Bundle.properties file can't be found but it's present in the same package than UI class:

enter image description here

MyVaadinUI.java

package com.mycompany.i18ntest;

import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import java.util.ResourceBundle;

@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

    private final ResourceBundle bundle = ResourceBundle.getBundle("com/mycompany/i18ntest/Bundle");

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class
                               , widgetset = "com.mycompany.i18ntest.AppWidgetSet")
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        TextField textField = new TextField(bundle.getString("MyVaadinUI.textField.caption"));
        textField.setRequired(true);
        textField.setRequiredError(bundle.getString("MyVaadinUI.textField.requiredError"));

        final FormLayout layout = new FormLayout(textField);
        layout.setMargin(true);
        setContent(layout);       
    }
}

Bundle.properties

MyVaadinUI.textField.caption = User
MyVaadinUI.textField.requiredError = Please input your user name here

Additionaly I've tried re-naming the .properties file to Bundle_es_AR.properties because my browser's default language is spanish (es) Argentina (AR), but no luck, still getting the exception.

Was it helpful?

Solution

Thanks to @AndréSchild comment (thank you) I realized the WAR file wasn't including Bundle.properties. After doing some Google search I have found how to include resources to Maven project by editing pom.xml file in this article. It presents two different ways to solve my problem:

Place .properties file inside resources folder

By default, Maven will look for your project's resources under src/main/resources.

Project 
   |-- pom.xml
   `-- src
       `-- main
           `-- resources

This basically states that I could simply place Bundle.properties file inside resources folder and it will be included in WAR file.

enter image description here

This file will be located at the root of classpath so I have to do this change in MyVaadinUI.java class:

private final ResourceBundle bundle = ResourceBundle.getBundle("Bundle");

Add the folder as resource editing pom.xml file

If we want to add some resource which is not placed in src/main/resources folder we can edit pom.xml file adding a resource folder as follows:

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[our folder here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

So in my case I could do something like this:

<project>
 ...
 <build>
   ...
   <resources>
     <directory>src/main/java/com/mycompany/i18ntest</directory>
     <includes>
       <include>*.properties</include>
     </includes>
   </resources>
   ...
 </build>
 ...
</project>

When WAR file is generated it will include the content of the specified resource folder (in addition to /src/main/resources fodler content) at the root of classpath. Once again I have to do this change:

private final ResourceBundle bundle = ResourceBundle.getBundle("Bundle");

Note: despite NetBeans shows all files in the resource folder, only .properties files will be included in WAR file because of this filter:

     <includes>
       <include>*.properties</include>
     </includes>

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top