Search code examples
gwtinternationalizationlocale

GWT - internationalization, the default works but when I add locale to URL I still get the default values. Any idea?


I have now seriously started experimenting with GWT, and I am stuck of course. I am trying with a very simple internationalization and when I add locale to my URL as following it doesn't work and always default data is fetched.

http://127.0.0.1:8888/DoTime.html?gwt.codesvr=127.0.0.1:9997&locale=sv_SE

In the following picture you can see structure of the project. The name of the application is "DoTime".

Folder structure

In the file /client/properties/DoTimeConstants_sv_SE.properties I have

localeTest = Vi skriver något på svenska för att testa om vi kan få det på svenska med locale.
appTitle = DoTime på svenska

In the corresponding file /client/DoTimeConstants I have

package com.dotime.client;
import com.google.gwt.i18n.client.Constants;
    public interface DoTimeConstants extends Constants {

    @DefaultStringValue("haha we are writing something in default to test locale")
    String localeTest();

    @DefaultStringValue("DoTime default title")
    String appTitle();
}

I the file /client/properties/DoTimeMessages_sv_SE.properties I have

@DefaultMessage("''{0}'' is not a valid symbol.")
  String invalidSymbol(String symbol);

  @DefaultMessage("Last update: {0,date,medium} {0,time,medium}")
  String lastUpdate(Date timestamp);

In the corresponding file /client/DoTimeMessages I have

package com.dotime.client;

import com.google.gwt.i18n.client.Messages;
import java.util.Date;

public interface DoTimeMessages extends Messages {
    @DefaultMessage("''{0}'' en symbol på svenska.")
    String invalidSymbol(String symbol);

    @DefaultMessage("Senaste uppdatering: {0,date,medium} {0,time,medium}")
    String lastUpdate(Date timestamp);
}

In the file DoTime.gwt.xml I have add locale="sv_SE" and added inherit I18N:

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0//EN"
        "http://google-web-toolkit.googlecode.com/svn/releases/2.0/distro-source/core/src/gwt-module.dtd">
<module rename-to="DoTime">

    <!-- Inherit the core Web Toolkit stuff.                  -->
    <inherits name='com.google.gwt.user.User'/>

    <!-- Specify the app entry point class.                   -->
    <entry-point class='com.dotime.client.DoTime'/>

    <inherits name="com.google.gwt.i18n.I18N"/>

    <extend-property name="locale" values="sv_SE"/>

    <!-- Specify the app servlets.                   -->
    <servlet path='/DoTimeService' class='com.dotime.server.DoTimeServiceImpl'/>

</module>

In my entry point method I fetch appTitle which returns the default value correctly

String appTitle = constants.appTitle();

but when I change my url by adding &locale=sv_SE nothing happens and I still have the default value.

I did try first by only having locale file extension locale=sv without any luck and later changed to sv_SE but it didn't work anyway. What is it I am missing? Do you have any idea? Many thanks for some clues.


Solution

  • Your properties files must be name the exact same as the Constants or Messages interfaces, except for the locale suffix and file extension. Specifically here, they must be in the same package; you cannot put them in some other package (the properties subpackage in this case) and expect GWT to find them.


    Side notes:

    • I think the locale in the gwt.xml and in the URL has to be written in the Java notation, using a dash as separator rather than an underscore.
    • you shouldn't use the <servlet> tag in your gwt.xml; they're used by the GWTTestCase runtime, but then you'd rather make a test-specific module that <inherits> your app module, rather than put those declarations in your app module.