1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Console.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Namespace: OpenLayers.Lang |
---|
12 | * Internationalization namespace. Contains dictionaries in various languages |
---|
13 | * and methods to set and get the current language. |
---|
14 | */ |
---|
15 | OpenLayers.Lang = { |
---|
16 | |
---|
17 | /** |
---|
18 | * Property: code |
---|
19 | * {String} Current language code to use in OpenLayers. Use the |
---|
20 | * <setCode> method to set this value and the <getCode> method to |
---|
21 | * retrieve it. |
---|
22 | */ |
---|
23 | code: null, |
---|
24 | |
---|
25 | /** |
---|
26 | * APIProperty: defaultCode |
---|
27 | * {String} Default language to use when a specific language can't be |
---|
28 | * found. Default is "en". |
---|
29 | */ |
---|
30 | defaultCode: "en", |
---|
31 | |
---|
32 | /** |
---|
33 | * APIFunction: getCode |
---|
34 | * Get the current language code. |
---|
35 | * |
---|
36 | * Returns: |
---|
37 | * The current language code. |
---|
38 | */ |
---|
39 | getCode: function() { |
---|
40 | if(!OpenLayers.Lang.code) { |
---|
41 | OpenLayers.Lang.setCode(); |
---|
42 | } |
---|
43 | return OpenLayers.Lang.code; |
---|
44 | }, |
---|
45 | |
---|
46 | /** |
---|
47 | * APIFunction: setCode |
---|
48 | * Set the language code for string translation. This code is used by |
---|
49 | * the <OpenLayers.Lang.translate> method. |
---|
50 | * |
---|
51 | * Parameters- |
---|
52 | * code - {String} These codes follow the IETF recommendations at |
---|
53 | * http://www.ietf.org/rfc/rfc3066.txt. If no value is set, the |
---|
54 | * browser's language setting will be tested. If no <OpenLayers.Lang> |
---|
55 | * dictionary exists for the code, the <OpenLayers.String.defaultLang> |
---|
56 | * will be used. |
---|
57 | */ |
---|
58 | setCode: function(code) { |
---|
59 | var lang; |
---|
60 | if(!code) { |
---|
61 | code = (OpenLayers.Util.getBrowserName() == "msie") ? |
---|
62 | navigator.userLanguage : navigator.language; |
---|
63 | } |
---|
64 | var parts = code.split('-'); |
---|
65 | parts[0] = parts[0].toLowerCase(); |
---|
66 | if(typeof OpenLayers.Lang[parts[0]] == "object") { |
---|
67 | lang = parts[0]; |
---|
68 | } |
---|
69 | |
---|
70 | // check for regional extensions |
---|
71 | if(parts[1]) { |
---|
72 | var testLang = parts[0] + '-' + parts[1].toUpperCase(); |
---|
73 | if(typeof OpenLayers.Lang[testLang] == "object") { |
---|
74 | lang = testLang; |
---|
75 | } |
---|
76 | } |
---|
77 | if(!lang) { |
---|
78 | OpenLayers.Console.warn( |
---|
79 | 'Failed to find OpenLayers.Lang.' + parts.join("-") + |
---|
80 | ' dictionary, falling back to default language' |
---|
81 | ); |
---|
82 | lang = OpenLayers.Lang.defaultCode; |
---|
83 | } |
---|
84 | |
---|
85 | OpenLayers.Lang.code = lang; |
---|
86 | }, |
---|
87 | |
---|
88 | /** |
---|
89 | * APIMethod: translate |
---|
90 | * Looks up a key from a dictionary based on the current language string. |
---|
91 | * The value of <getCode> will be used to determine the appropriate |
---|
92 | * dictionary. Dictionaries are stored in <OpenLayers.Lang>. |
---|
93 | * |
---|
94 | * Parameters: |
---|
95 | * key - {String} The key for an i18n string value in the dictionary. |
---|
96 | * context - {Object} Optional context to be used with |
---|
97 | * <OpenLayers.String.format>. |
---|
98 | * |
---|
99 | * Returns: |
---|
100 | * {String} A internationalized string. |
---|
101 | */ |
---|
102 | translate: function(key, context) { |
---|
103 | var dictionary = OpenLayers.Lang[OpenLayers.Lang.getCode()]; |
---|
104 | var message = dictionary[key]; |
---|
105 | if(!message) { |
---|
106 | // Message not found, fall back to message key |
---|
107 | message = key; |
---|
108 | } |
---|
109 | if(context) { |
---|
110 | message = OpenLayers.String.format(message, context); |
---|
111 | } |
---|
112 | return message; |
---|
113 | } |
---|
114 | |
---|
115 | }; |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | * APIMethod: OpenLayers.i18n |
---|
120 | * Alias for <OpenLayers.Lang.translate>. Looks up a key from a dictionary |
---|
121 | * based on the current language string. The value of |
---|
122 | * <OpenLayers.Lang.getCode> will be used to determine the appropriate |
---|
123 | * dictionary. Dictionaries are stored in <OpenLayers.Lang>. |
---|
124 | * |
---|
125 | * Parameters: |
---|
126 | * key - {String} The key for an i18n string value in the dictionary. |
---|
127 | * context - {Object} Optional context to be used with |
---|
128 | * <OpenLayers.String.format>. |
---|
129 | * |
---|
130 | * Returns: |
---|
131 | * {String} A internationalized string. |
---|
132 | */ |
---|
133 | OpenLayers.i18n = OpenLayers.Lang.translate; |
---|