Google Web Toolkit
Search Google Code:

# Specifying a Locale

GWT represents `locale` as a client property whose value can be set either using a meta tag embedded in the host page or in the query string of the host page's URL. Rather than being supplied by GWT, the set of possible values for the `locale` client property is entirely a function of your module configuration.

If that sounded like gibberish (and it probably did), a quick digression into the purpose of client properties is in order...

## Client Properties and the GWT Compilation Process

Client properties are key/value pairs that can be used to configure GWT modules. User agent, for example, is represented by a client property. Each client property can have any number of values, but all of the values must be enumerable when the GWT compiler runs.

GWT modules can define and extend the set of available client properties along with the potential values each property might assume when loaded in an end user's browser. At compile time, the GWT compiler determines all the possible permutations of a module's client properties, from which it produces multiple compilations. Each compilation is optimized for a different set of client properties and is recorded into a file ending with the suffix `.cache.html`.

In deployment, the end-user's browser only needs one particular compilation, which is determined by mapping the end user's client properties onto the available compiled permutations. Thus, only the exact code required by the end user is downloaded, no more. 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 Default Locale

The `com.google.gwt.i18n.I18N` module defines only one locale by default, called `default`. This default locale is used when the `locale` client property goes unspecified in deployment. The default locale is used internally as a last-resort match between a Localizable interface and a localized resource or class.

## Adding Locale Choices to a Module

In any real-world application, you will define at least one locale in addition to the default locale. "Adding a locale" means extending the set of values of the `locale` client property using the `<extend-property>` element in your module XML.

For example, the following module adds multiple locale values:

    <!--                                                                        -->
    <!-- Copyright 2007 Google Inc.                                             -->
    <!-- Licensed under the Apache License, Version 2.0 (the "License"); you    -->
    <!-- may not use this file except in compliance with the License. You may   -->
    <!-- may obtain a copy of the License at                                    -->
    <!--                                                                        -->
    <!-- http://www.apache.org/licenses/LICENSE-2.0                             -->
    <!--                                                                        -->
    <!-- Unless required by applicable law or agreed to in writing, software    -->
    <!-- distributed under the License is distributed on an "AS IS" BASIS,      -->
    <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        -->
    <!-- implied. License for the specific language governing permissions and   -->
    <!-- limitations under the License.                                         -->

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

      <!-- French language, independent of country -->
      <extend-property name="locale" values="fr"/>

      <!-- French in France -->
      <extend-property name="locale" values="fr_FR"/>

      <!-- French in Canada -->
      <extend-property name="locale" values="fr_CA"/>

      <!-- English language, independent of country -->
      <extend-property name="locale" values="en"/>
    </module>

## Choosing a Locale at Runtime

The locale client property can be specified using either a meta tag or as part of the query string in the host page's URL. If both are specified, the query string takes precedence.

To specify the `locale` client property using a meta tag in the host page, embed a meta tag for `gwt:property` as follows:

    <meta name="gwt:property" content="locale=x_Y">

For example, the following host HTML page sets the locale to "ja_JP":

    <html>
      <head>
        <meta name="gwt:module" content="com.google.gwt.examples.i18n.ColorNameLookupExample">
        <meta name="gwt:property" content="locale=ja_JP">
      </head>
      <body>
        <script src="gwt.js"></script>
      </body>
    </html>

To specify the `locale` client property using a query string, specify a value for the name `locale`. For example,

    http://www.example.org/myapp.html?locale=fr_CA