Search This Blog

Tuesday, April 12, 2011

Internalization in OpenLayers 2.10

The way offered in OpenLayers to localize the messages is very simple. It is implemented through the OpenLayers.Lang class and the extensions for each language.

If you want to use another locale different from the default (English), you have to import in your web pages the javascript file corresponding to the desired language. For example, to include Galician, it is necessary to import the gl.js file in your context:

<script type="text/javascript" src="OpenLayers/Lang/gl.js">

The second step to localize your elements with OpenLayers is to indicate the to locale to be used:

OpenLayers.Lang.setCode('gl');

Moreover, it is also posible to define new messages in each language through the extension of the corresponding OpenLayers locale classes with our own definitions:

OpenLayers.Util
     .extend(OpenLayers.Lang.gl, {
           'My layer' : 'A miña capa',
           'My control' : 'O meu control',
           ...
});

To use the new messages that you have defined, use the function OpenLayers.i18n. An example could be:

var mylayer = new OpenLayers.Layer.WMS.Post(
            OpenLayers.i18n("My layer"),
            ...
};

If the message for the specified locale is not found, the parameter of the function will be shown without changes.

No comments:

Post a Comment