]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Lang.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Lang.js
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. */
4
5 /**
6  * @requires OpenLayers/Console.js
7  */
8
9 /**
10  * Namespace: OpenLayers.Lang
11  * Internationalization namespace.  Contains dictionaries in various languages
12  *     and methods to set and get the current language.
13  */
14 OpenLayers.Lang = {
15     
16     /** 
17      * Property: code
18      * {String}  Current language code to use in OpenLayers.  Use the
19      *     <setCode> method to set this value and the <getCode> method to
20      *     retrieve it.
21      */
22     code: null,
23
24     /** 
25      * APIProperty: defaultCode
26      * {String} Default language to use when a specific language can't be
27      *     found.  Default is "en".
28      */
29     defaultCode: "en",
30         
31     /**
32      * APIFunction: getCode
33      * Get the current language code.
34      *
35      * Returns:
36      * The current language code.
37      */
38     getCode: function() {
39         if(!OpenLayers.Lang.code) {
40             OpenLayers.Lang.setCode();
41         }
42         return OpenLayers.Lang.code;
43     },
44     
45     /**
46      * APIFunction: setCode
47      * Set the language code for string translation.  This code is used by
48      *     the <OpenLayers.Lang.translate> method.
49      *
50      * Parameters-
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>
55      *     will be used.
56      */
57     setCode: function(code) {
58         var lang;
59         if(!code) {
60             code = (OpenLayers.Util.getBrowserName() == "msie") ?
61                 navigator.userLanguage : navigator.language;
62         }
63         var parts = code.split('-');
64         parts[0] = parts[0].toLowerCase();
65         if(typeof OpenLayers.Lang[parts[0]] == "object") {
66             lang = parts[0];
67         }
68
69         // check for regional extensions
70         if(parts[1]) {
71             var testLang = parts[0] + '-' + parts[1].toUpperCase();
72             if(typeof OpenLayers.Lang[testLang] == "object") {
73                 lang = testLang;
74             }
75         }
76         if(!lang) {
77             OpenLayers.Console.warn(
78                 'Failed to find OpenLayers.Lang.' + parts.join("-") +
79                 ' dictionary, falling back to default language'
80             );
81             lang = OpenLayers.Lang.defaultCode;
82         }
83         
84         OpenLayers.Lang.code = lang;
85     },
86
87     /**
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>.
92      *
93      * Parameters:
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>.
97      * 
98      * Returns:
99      * {String} A internationalized string.
100      */
101     translate: function(key, context) {
102         var dictionary = OpenLayers.Lang[OpenLayers.Lang.getCode()];
103         var message = dictionary[key];
104         if(!message) {
105             // Message not found, fall back to message key
106             message = key;
107         }
108         if(context) {
109             message = OpenLayers.String.format(message, context);
110         }
111         return message;
112     }
113     
114 };
115
116
117 /**
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>.
123  *
124  * Parameters:
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>.
128  * 
129  * Returns:
130  * {String} A internationalized string.
131  */
132 OpenLayers.i18n = OpenLayers.Lang.translate;