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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var gActiveLanguages;
var gLanguages;
var gLanguageNames = [];
var gLanguageTitles = {};
function Startup()
{
gActiveLanguages = document.getElementById("activeLanguages");
// gLanguages stores the ordered list of languages, due to the nature
// of childNodes it is live and updates automatically.
gLanguages = gActiveLanguages.childNodes;
ReadAvailableLanguages();
}
function AddLanguage()
{
document.documentElement.openSubDialog("chrome://communicator/content/pref/pref-languages-add.xul", "addlangwindow", gLanguageNames);
}
function ReadAvailableLanguages()
{
var i = 0;
var languagesBundle = document.getElementById("languageNamesBundle");
var prefLangBundle = document.getElementById("prefLangBundle");
var regionsBundle = document.getElementById("regionNamesBundle");
var langStrings = document.getElementById("acceptedBundle").strings;
while (langStrings.hasMoreElements())
{
// Progress through the bundle.
var curItem = langStrings.getNext();
if (!(curItem instanceof Ci.nsIPropertyElement))
break;
var stringNameProperty = curItem.key.split('.');
var str = stringNameProperty[0];
if (str && stringNameProperty[1] == 'accept')
{
var stringLangRegion = str.split('-');
if (stringLangRegion[0])
{
var language = "";
var region = null;
try
{
language = languagesBundle.getString(stringLangRegion[0]);
}
catch (ex) {}
if (stringLangRegion.length > 1)
{
try
{
region = regionsBundle.getString(stringLangRegion[1]);
}
catch (ex) {}
}
var title;
if (region)
title = prefLangBundle.getFormattedString("languageRegionCodeFormat",
[language, region, str]);
else
title = prefLangBundle.getFormattedString("languageCodeFormat",
[language, str]);
gLanguageTitles[str] = title;
if (curItem.value == "true")
gLanguageNames.push([title, str]);
}
}
}
// Sort on first element.
gLanguageNames.sort(
function compareFn(a, b)
{
return a[0].localeCompare(b[0]);
}
);
}
function ReadActiveLanguages()
{
var arrayOfPrefs = document.getElementById("intl.accept_languages").value
.toLowerCase().split(/\s*,\s*/);
// No need to rebuild listitems if languages in prefs and listitems match.
if (InSync(arrayOfPrefs))
return;
while (gActiveLanguages.hasChildNodes())
gActiveLanguages.lastChild.remove();
arrayOfPrefs.forEach(
function(aKey)
{
if (aKey)
{
let langTitle = gLanguageTitles.hasOwnProperty(aKey) ?
gLanguageTitles[aKey] : "[" + aKey + "]";
gActiveLanguages.appendItem(langTitle, aKey);
}
}
);
SelectLanguage();
return;
}
// Checks whether listitems and pref values matches, returns false if not.
function InSync(aPrefArray)
{
// Can't match if they don't have the same length.
if (aPrefArray.length != gLanguages.length)
return false;
return aPrefArray.every(
function(aElement, aIndex)
{
return aElement == gLanguages[aIndex].value;
}
);
}
// Called on onsynctopreference.
function WriteActiveLanguages()
{
return Array.from(gLanguages, e => e.value).join(",");
}
function MoveUp()
{
var selected = gActiveLanguages.selectedItem;
var before = selected.previousSibling;
if (before)
{
before.parentNode.insertBefore(selected, before);
gActiveLanguages.selectItem(selected);
gActiveLanguages.ensureElementIsVisible(selected);
}
SelectLanguage();
gActiveLanguages.doCommand();
}
function MoveDown()
{
var selected = gActiveLanguages.selectedItem;
if (selected.nextSibling)
{
var before = selected.nextSibling.nextSibling;
gActiveLanguages.insertBefore(selected, before);
gActiveLanguages.selectItem(selected);
}
SelectLanguage();
gActiveLanguages.doCommand();
}
function RemoveActiveLanguage(aEvent)
{
if (aEvent && aEvent.keyCode != aEvent.DOM_VK_DELETE &&
aEvent.keyCode != aEvent.DOM_VK_BACK_SPACE)
return;
var nextNode = null;
while (gActiveLanguages.selectedItem)
{
var selectedNode = gActiveLanguages.selectedItem;
nextNode = selectedNode.nextSibling || selectedNode.previousSibling;
selectedNode.remove();
}
if (nextNode)
gActiveLanguages.selectItem(nextNode);
SelectLanguage();
gActiveLanguages.doCommand();
}
function SelectLanguage()
{
var len = gActiveLanguages.selectedItems.length;
EnableElementById("langRemove", len, false);
var selected = gActiveLanguages.selectedItem;
EnableElementById("langDown", (len == 1) && selected.nextSibling, false);
EnableElementById("langUp", (len == 1) && selected.previousSibling, false);
}
|