1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Console.js
10 * Namespace: OpenLayers.Lang
11 * Internationalization namespace. Contains dictionaries in various languages
12 * and methods to set and get the current language.
18 * {String} Current language code to use in OpenLayers. Use the
19 * <setCode> method to set this value and the <getCode> method to
25 * APIProperty: defaultCode
26 * {String} Default language to use when a specific language can't be
27 * found. Default is "en".
32 * APIFunction: getCode
33 * Get the current language code.
36 * The current language code.
39 if(!OpenLayers.Lang.code) {
40 OpenLayers.Lang.setCode();
42 return OpenLayers.Lang.code;
46 * APIFunction: setCode
47 * Set the language code for string translation. This code is used by
48 * the <OpenLayers.Lang.translate> method.
51 * code - {String} These codes follow the IETF recommendations at
52 * http://www.ietf.org/rfc/rfc3066.txt. If no value is set, the
53 * browser's language setting will be tested. If no <OpenLayers.Lang>
54 * dictionary exists for the code, the <OpenLayers.String.defaultLang>
57 setCode: function(code) {
60 code = (OpenLayers.Util.getBrowserName() == "msie") ?
61 navigator.userLanguage : navigator.language;
63 var parts = code.split('-');
64 parts[0] = parts[0].toLowerCase();
65 if(typeof OpenLayers.Lang[parts[0]] == "object") {
69 // check for regional extensions
71 var testLang = parts[0] + '-' + parts[1].toUpperCase();
72 if(typeof OpenLayers.Lang[testLang] == "object") {
77 OpenLayers.Console.warn(
78 'Failed to find OpenLayers.Lang.' + parts.join("-") +
79 ' dictionary, falling back to default language'
81 lang = OpenLayers.Lang.defaultCode;
84 OpenLayers.Lang.code = lang;
88 * APIMethod: translate
89 * Looks up a key from a dictionary based on the current language string.
90 * The value of <getCode> will be used to determine the appropriate
91 * dictionary. Dictionaries are stored in <OpenLayers.Lang>.
94 * key - {String} The key for an i18n string value in the dictionary.
95 * context - {Object} Optional context to be used with
96 * <OpenLayers.String.format>.
99 * {String} A internationalized string.
101 translate: function(key, context) {
102 var dictionary = OpenLayers.Lang[OpenLayers.Lang.getCode()];
103 var message = dictionary[key];
105 // Message not found, fall back to message key
109 message = OpenLayers.String.format(message, context);
118 * APIMethod: OpenLayers.i18n
119 * Alias for <OpenLayers.Lang.translate>. Looks up a key from a dictionary
120 * based on the current language string. The value of
121 * <OpenLayers.Lang.getCode> will be used to determine the appropriate
122 * dictionary. Dictionaries are stored in <OpenLayers.Lang>.
125 * key - {String} The key for an i18n string value in the dictionary.
126 * context - {Object} Optional context to be used with
127 * <OpenLayers.String.format>.
130 * {String} A internationalized string.
132 OpenLayers.i18n = OpenLayers.Lang.translate;