-
Notifications
You must be signed in to change notification settings - Fork 106
/
Lang.js
140 lines (131 loc) · 4.34 KB
/
Lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2008-present The Open Source Geospatial Foundation
*
* Published under the BSD license.
* See https://github.com/geoext/geoext2/blob/master/license.txt for the full
* text of the license.
*/
/*
* @requires GeoExt/Version.js
*/
/**
* The GeoExt.Lang singleton is created when the library is loaded.
* Include all relevant language files after this file in your build.
*
* @class GeoExt.Lang
*/
Ext.define('GeoExt.Lang', {
extend: 'Ext.util.Observable',
singleton: true,
requires: [
'GeoExt.Version'
],
/**
* The current language tag. Use `#set` to set the locale. Defaults
* to the browser language where available.
*
* @cfg {String}
*/
locale: navigator.language || navigator.userLanguage,
/**
* Dictionary of string lookups per language.
*
* @property {Object}
* @private
*/
dict: null,
/**
* Fires when localized strings are set. Listeners will receive a
* single `locale` event with the language tag.
*
* @event localize
*/
/**
* Construct the Lang singleton.
*
* @private
*/
constructor: function() {
this.dict = {};
this.callParent();
},
/**
* Add translation strings to the dictionary. This method can be called
* multiple times with the same language tag (locale argument) to extend
* a single dictionary.
*
* @param {String} locale A language tag that follows the "en-CA"
* convention (http://www.ietf.org/rfc/rfc3066.txt).
* @param {Object} lookup An object with properties that are dot
* delimited names of objects with localizable strings (e.g.
* "GeoExt.VectorLegend.prototype"). The values for these properties
* are objects that will be used to extend the target objects with
* localized strings (e.g. {untitledPrefix: "Untitiled "})
*/
add: function(locale, lookup) {
var obj = this.dict[locale];
if (!obj) {
this.dict[locale] = Ext.apply({}, lookup);
} else {
for (var key in lookup) {
obj[key] = Ext.apply(obj[key] || {}, lookup[key]);
}
}
if (!locale || locale === this.locale) {
this.set(locale);
} else if (this.locale.indexOf(locale + "-") === 0) {
// current locale is regional variation of added strings
// call set so newly added strings are used where appropriate
this.set(this.locale);
}
},
/**
* Set the language for all GeoExt components. This will use any localized
* strings in the dictionary (set with the `#add` method) that
* correspond to the complete matching language tag or any "higher order"
* tag (e.g. setting "en-CA" will use strings from the "en" dictionary if
* matching strings are not found in the "en-CA" dictionary).
*
* @param {String} locale Language identifier tag following recommendations
* at http://www.ietf.org/rfc/rfc3066.txt.
*/
set: function(locale) {
// compile lookup based on primary and all subtags
var tags = locale ? locale.split("-") : [];
var id = "";
var lookup = {}, parent, str, i, ii;
for (i=0, ii=tags.length; i<ii; ++i) {
id += (id && "-" || "") + tags[i];
if (id in this.dict) {
parent = this.dict[id];
for (str in parent) {
if (str in lookup) {
Ext.apply(lookup[str], parent[str]);
} else {
lookup[str] = Ext.apply({}, parent[str]);
}
}
}
}
// now extend all objects given by dot delimited names in lookup
for (str in lookup) {
var obj = window;
var parts = str.split(".");
var missing = false;
for (i=0, ii=parts.length; i<ii; ++i) {
var name = parts[i];
if (name in obj) {
obj = obj[name];
} else {
missing = true;
break;
}
}
if (!missing) {
Ext.apply(obj, lookup[str]);
}
}
this.locale = locale;
this.fireEvent("localize", locale);
}
});