how to get locale information on a GWT application

2019-04-22 09:02发布

问题:

In GWT I have to specify what locales are supported in my application. The code get compiled in various files, one for each locale (beside other versions), but I have to give my clients one only URL. This URL is supposed to be a page that should be displayed according to the locale preferred by the browser. I dont't want to have an HTTP parameter for the locale since I want to forse the locale preferred by the browser. How can this be coded in GWT?

Should I try to to this using apache rewrite rules? I thied it, but I think I cannot access such parameter easely in a rewrite rule.

Thanks a lot, Giuseppe

回答1:

I had the same problem as you, but as I really need to know the current locale (I'm requesting a second server for data that I want to be localizable) I found this class: com.google.gwt.i18n.client.LocaleInfo#getCurrentLocale(). That should give you what GWT uses currently.



回答2:

GWT has good support for internationalization. See this link. The i18nCreator command can help you to set up the internationalization infrastructure for similar to the way projectCreator and applicationCreator set up the GWT application.

If you have static strings (i.e. Invalid Entry!) that need to be internationalized, you don't need any additional flag to i18nCreator command to create the properties files and infrastructure.

If you have strings that need to accept parameters (i.e. Hello {0}), you need to pass the -createMessages flag to i18nCreator command to create the properties files and infrastructure.

Now your module needs to include the i18n module in your MyApplication.gwt.xml:

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

Define a Java interface in the same package as your property files that extends Constants or Messages and defines methods (name matches the property entries) that all return string.

MyConstants.properties contains:

errorMessage=Invalid Entry!

MyConstants.java contains:

import com.google.gwt.i18n.client.Constants;

public interface myConstants extends Constants {
    String errorMessage();
}

Now to access these internationalized Strings from you application:

public class MyApplication implements EntryPoint {
    private static final MyConstants constants = (MyConstants)GWT.create(MyConstants.class);

    public void onModuleLoad() {
        final Label errorMessage = new Label(constants.errorMessage);
    }
}

GWT implements the interface for you automagically.

You can get messages in a similar way.

Hopefully this can help you get started.



回答3:

Unless I am reading the documentation incorrectly I don't think you have to do anything.

GWT and Locale

By making locale a client property, the standard startup process in gwt.js chooses the appropriate localized version of an application, providing ease of use (it's easier than it might sound!), optimized performance, and minimum script size.

The way I read it, as long as your module has added all the locale choices to it, it should be automatic?



回答4:

Check this com.google.gwt.i18n.client.LocaleInfo.getCurrentLocale()



回答5:

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

<!-- Use browser-specified locale for i18n -->
<set-configuration-property name="locale.useragent" value="Y"/>

<!-- Specify locales your application support -->
<extend-property name="locale" values="en"/>
<extend-property name="locale" values="de_DE"/>
<extend-property name="locale" values="ru_RU"/>


标签: url gwt locale