Adding framework for internationalization support. The new OpenLayers.Lang.translate method takes a key and looks for a value in a dictionary based on the current language setting. Set a new language code with OpenLayers.Lang.setCode. Get the current code with OpenLayers.Lang.getCode. Thanks to Mike Adair for the lead on this one. r=ahocevar,me (closes #109)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@6313 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
|
||||
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
|
||||
* full text of the license. */
|
||||
|
||||
|
||||
/**
|
||||
* Namespace: OpenLayers.Lang
|
||||
* Internationalization namespace. Contains dictionaries in various languages
|
||||
* and methods to set and get the current language.
|
||||
*/
|
||||
OpenLayers.Lang = {
|
||||
|
||||
/**
|
||||
* Property: code
|
||||
* {String} Current language code to use in OpenLayers. Use the
|
||||
* <setCode> method to set this value and the <getCode> method to
|
||||
* retrieve it.
|
||||
*/
|
||||
code: null,
|
||||
|
||||
/**
|
||||
* APIProperty: defaultCode
|
||||
* {String} Default language to use when a specific language can't be
|
||||
* found. Default is "en".
|
||||
*/
|
||||
defaultCode: "en",
|
||||
|
||||
/**
|
||||
* APIFunction: getCode
|
||||
* Get the current language code.
|
||||
*
|
||||
* Returns:
|
||||
* The current language code.
|
||||
*/
|
||||
getCode: function() {
|
||||
if(!OpenLayers.Lang.code) {
|
||||
OpenLayers.Lang.setCode();
|
||||
}
|
||||
return OpenLayers.Lang.code;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIFunction: setCode
|
||||
* Set the language code for string translation. This code is used by
|
||||
* the <OpenLayers.Lang.translate> method.
|
||||
*
|
||||
* Parameters-
|
||||
* code - {String} These codes follow the IETF recommendations at
|
||||
* http://www.ietf.org/rfc/rfc3066.txt. If no value is set, the
|
||||
* browser's language setting will be tested. If no <OpenLayers.Lang>
|
||||
* dictionary exists for the code, the <OpenLayers.String.defaultLang>
|
||||
* will be used.
|
||||
*/
|
||||
setCode: function(code) {
|
||||
var lang;
|
||||
if(!code) {
|
||||
code = (OpenLayers.Util.getBrowserName() == "msie") ?
|
||||
navigator.userLanguage : navigator.language;
|
||||
}
|
||||
var parts = code.split('-');
|
||||
parts[0] = parts[0].toLowerCase();
|
||||
if(typeof OpenLayers.Lang[parts[0]] == "object") {
|
||||
lang = parts[0];
|
||||
}
|
||||
|
||||
// check for regional extensions
|
||||
if(parts[1]) {
|
||||
var testLang = parts[0] + '-' + parts[1].toUpperCase();
|
||||
if(typeof OpenLayers.Lang[testLang] == "object") {
|
||||
lang = testLang;
|
||||
}
|
||||
}
|
||||
if(!lang) {
|
||||
OpenLayers.Console.warn(
|
||||
'Failed to find OpenLayers.Lang.' + parts.join("-") +
|
||||
' dictionary, falling back to default language'
|
||||
);
|
||||
lang = OpenLayers.Lang.defaultCode
|
||||
}
|
||||
|
||||
OpenLayers.Lang.code = lang;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: translate
|
||||
* Looks up a key from a dictionary based on the current language string.
|
||||
* The value of <getCode> will be used to determine the appropriate
|
||||
* dictionary. Dictionaries are stored in <OpenLayers.Lang>.
|
||||
*
|
||||
* Parameters:
|
||||
* key - {String} The key for an i18n string value in the dictionary.
|
||||
* context - {Object} Optional context to be used with
|
||||
* <OpenLayers.String.format>.
|
||||
*
|
||||
* Returns:
|
||||
* {String} A internationalized string.
|
||||
*/
|
||||
translate: function(key, context) {
|
||||
var dictionary = OpenLayers.Lang[OpenLayers.Lang.getCode()];
|
||||
var message = dictionary[key];
|
||||
if(!message) {
|
||||
// Message not found, fall back to message key
|
||||
message = key;
|
||||
}
|
||||
if(context) {
|
||||
message = OpenLayers.String.format(message, context);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* APIMethod: OpenLayers.i18n
|
||||
* Alias for <OpenLayers.Lang.translate>. Looks up a key from a dictionary
|
||||
* based on the current language string. The value of
|
||||
* <OpenLayers.Lang.getCode> will be used to determine the appropriate
|
||||
* dictionary. Dictionaries are stored in <OpenLayers.Lang>.
|
||||
*
|
||||
* Parameters:
|
||||
* key - {String} The key for an i18n string value in the dictionary.
|
||||
* context - {Object} Optional context to be used with
|
||||
* <OpenLayers.String.format>.
|
||||
*
|
||||
* Returns:
|
||||
* {String} A internationalized string.
|
||||
*/
|
||||
OpenLayers.i18n = OpenLayers.Lang.translate;
|
||||
Reference in New Issue
Block a user