gecko-dev/suite/common/pref/pref-languages.js

522 lines
12 KiB
JavaScript
Raw Normal View History

2006-05-17 02:25:34 +00:00
//GLOBALS
//pref service and pref string
2006-05-17 02:25:40 +00:00
var prefInt = parent.hPrefWindow.pref;
2006-05-17 02:25:34 +00:00
//locale bundles
2006-05-17 02:22:36 +00:00
var regionsBundle;
var languagesBundle;
var acceptedBundle;
2006-05-17 02:25:34 +00:00
//dictionary of all supported locales
2006-05-17 02:22:36 +00:00
var availLanguageDict;
2006-05-17 02:25:34 +00:00
//XUL tree handles
var available_languages;
var available_languages_treeroot;
//XUL tree handles
var active_languages;
var active_languages_treeroot;
//XUL window pref window interface object
2006-05-17 02:25:40 +00:00
var pref_string = new String();
2006-05-17 02:22:36 +00:00
function GetBundles()
{
if (!regionsBundle) regionsBundle = srGetStrBundle("chrome://global/locale/regionNames.properties");
if (!languagesBundle) languagesBundle = srGetStrBundle("chrome://global/locale/languageNames.properties");
if (!acceptedBundle) acceptedBundle = srGetStrBundle("resource:/res/language.properties");
}
2006-05-17 02:25:34 +00:00
function Init()
{
2006-05-17 02:25:42 +00:00
doSetOKCancel(AddAvailableLanguage);
2006-05-17 02:25:34 +00:00
dump("********** Init()\n");
2006-05-17 02:25:34 +00:00
try {
GetBundles()
ReadAvailableLanguages();
}
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
catch(ex) {
dump("*** Couldn't get string bundles\n");
}
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
if (!window.arguments) {
2006-05-17 02:25:40 +00:00
//no caller arguments - load base window
2006-05-17 02:25:34 +00:00
try {
active_languages = document.getElementById('active_languages');
active_languages_treeroot = document.getElementById('active_languages_root');
prefwindow_proxy_object = document.getElementById('intlAcceptLanguages');
} //try
catch(ex) {
dump("*** Couldn't get XUL element handles\n");
} //catch
try {
parent.initPanel('chrome://communicator/content/pref/pref-languages.xul');
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
catch(ex) {
2006-05-17 02:25:40 +00:00
dump("*** Couldn't initialize page switcher and pref loader.\n");
2006-05-17 02:25:34 +00:00
//pref service backup
2006-05-17 02:25:34 +00:00
} //catch
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:40 +00:00
try {
pref_string = parent.hPrefWindow.getPref( "string", "intl.accept_languages");
//fall-back (hard-coded for Beta2, need to move to XUL/DTD)
if (!pref_string) pref_string = "en";
} //try
2006-05-17 02:25:40 +00:00
catch(ex) {
dump("*** Couldn't read pref string\n");
} //catch
2006-05-17 02:25:34 +00:00
try {
2006-05-17 02:25:40 +00:00
dump("*** Language PrefString: " + pref_string + "\n");
2006-05-17 02:25:34 +00:00
} //try
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
catch(ex) {
dump("*** Pref object doesn't exist\n");
} //catch
2006-05-17 02:25:40 +00:00
LoadActiveLanguages();
2006-05-17 02:25:34 +00:00
} else {
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:40 +00:00
//load available languages popup
2006-05-17 02:25:34 +00:00
try {
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
//add language popup
available_languages = document.getElementById('available_languages');
available_languages_treeroot = document.getElementById('available_languages_root');
active_languages = window.opener.document.getElementById('active_languages');
active_languages_treeroot = window.opener.document.getElementById('active_languages_root');
2006-05-17 02:25:40 +00:00
pref_string = window.opener.document.getElementById('intlAcceptLanguages').value;
2006-05-17 02:25:34 +00:00
} //try
catch(ex) {
dump("*** Couldn't get XUL element handles\n");
} //catch
2006-05-17 02:25:40 +00:00
try {
dump("*** AddAvailable Language PrefString: " + pref_string + "\n");
} //try
catch(ex) {
dump("*** Pref object doesn't exist\n");
} //catch
2006-05-17 02:25:34 +00:00
LoadAvailableLanguages();
2006-05-17 02:25:34 +00:00
}
2006-05-17 02:22:36 +00:00
}
function AddLanguage()
{
dump("********** AddLanguage()\n");
2006-05-17 02:25:40 +00:00
//cludge: make pref string available from the popup
document.getElementById('intlAcceptLanguages').value = pref_string;
2006-05-17 02:25:34 +00:00
window.openDialog("chrome://communicator/content/pref/pref-languages-add.xul","","modal=yes,chrome,resizable=no", "addlangwindow");
2006-05-17 02:25:40 +00:00
UpdateSavePrefString();
2006-05-17 02:25:34 +00:00
}
2006-05-17 02:22:36 +00:00
function ReadAvailableLanguages()
{
2006-05-17 02:22:36 +00:00
availLanguageDict = new Array();
2006-05-17 02:25:34 +00:00
var visible = new String();
2006-05-17 02:22:36 +00:00
var str = new String();
var i =0;
acceptedBundleEnum = acceptedBundle.getSimpleEnumeration();
while (acceptedBundleEnum.hasMoreElements()) {
2006-05-17 02:22:36 +00:00
//progress through the bundle
curItem = acceptedBundleEnum.getNext();
2006-05-17 02:22:36 +00:00
//"unpack" the item, nsIPropertyElement is now partially scriptable
curItem = curItem.QueryInterface(Components.interfaces.nsIPropertyElement);
//dump string name (key)
var stringName = curItem.getKey();
stringNameProperty = stringName.split('.');
if (stringNameProperty[1] == 'accept') {
//dump the UI string (value)
2006-05-17 02:25:34 +00:00
visible = curItem.getValue();
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
//if (visible == 'true') {
2006-05-17 02:22:36 +00:00
str = stringNameProperty[0];
stringLangRegion = stringNameProperty[0].split('-');
if (stringLangRegion[0]) {
2006-05-17 02:25:34 +00:00
2006-05-17 02:22:36 +00:00
try {
2006-05-17 02:25:34 +00:00
var tit = languagesBundle.GetStringFromName(stringLangRegion[0]);
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
2006-05-17 02:22:36 +00:00
catch (ex) {
2006-05-17 02:25:34 +00:00
dump("No language string for:" + stringLangRegion[1] + "\n");
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
if (stringLangRegion[1]) {
try {
var tit = tit + "/" + regionsBundle.GetStringFromName(stringLangRegion[1]);
}
catch (ex) {
dump("No region string for:" + stringLangRegion[1] + "\n");
}
} //if region
tit = tit + " [" + str + "]";
2006-05-17 02:22:36 +00:00
} //if language
if (str) if (tit) {
availLanguageDict[i] = new Array(3);
2006-05-17 02:22:36 +00:00
availLanguageDict[i][0] = tit;
availLanguageDict[i][1] = str;
2006-05-17 02:25:34 +00:00
availLanguageDict[i][2] = visible;
2006-05-17 02:22:36 +00:00
i++;
if (tit) {}
else dump('Not label for :' + str + ', ' + tit+'\n');
} // if str&tit
2006-05-17 02:25:34 +00:00
//} //if visible
2006-05-17 02:22:36 +00:00
} //if accepted
} //while
availLanguageDict.sort();
}
function LoadAvailableLanguages()
{
2006-05-17 02:22:36 +00:00
dump("Loading available languages!\n");
if (availLanguageDict) for (i = 0; i < availLanguageDict.length; i++) {
2006-05-17 02:25:34 +00:00
if (availLanguageDict[i][2] == 'true') {
2006-05-17 02:25:34 +00:00
AddTreeItem(document, available_languages_treeroot, availLanguageDict[i][1], availLanguageDict[i][0]);
} //if
} //for
}
function LoadActiveLanguages()
{
2006-05-17 02:25:40 +00:00
dump("LoadActiveLanguages, PrefString: " + pref_string + "\n");
try {
2006-05-17 02:25:40 +00:00
arrayOfPrefs = pref_string.split(', ');
}
catch (ex) {
2006-05-17 02:22:36 +00:00
dump("failed to split the preference string!\n");
}
if (arrayOfPrefs) for (i = 0; i < arrayOfPrefs.length; i++) {
str = arrayOfPrefs[i];
2006-05-17 02:22:36 +00:00
tit = GetLanguageTitle(str);
if (str) {
if (!tit)
tit = '[' + str + ']';
2006-05-17 02:25:34 +00:00
AddTreeItem(document, active_languages_treeroot, str, tit);
2006-05-17 02:22:36 +00:00
} //if
} //for
}
2006-05-17 02:25:34 +00:00
function LangAlreadyActive(langId)
2006-05-17 02:22:36 +00:00
{
2006-05-17 02:25:40 +00:00
dump("*** LangAlreadyActive, PrefString: " + pref_string + "\n");
var found = false;
2006-05-17 02:22:36 +00:00
try {
arrayOfPrefs = pref_string.split(', ');
if (arrayOfPrefs) for (i = 0; i < arrayOfPrefs.length; i++) {
if (arrayOfPrefs[i] == langId) {
found = true;
break;
}
}
return found;
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
catch(ex){
return false;
2006-05-17 02:22:36 +00:00
}
}
function AddAvailableLanguage()
{
2006-05-17 02:25:34 +00:00
var Languagename = new String();
var Languageid = new String();
2006-05-17 02:25:34 +00:00
//selected languages
for (var nodeIndex=0; nodeIndex < available_languages.selectedItems.length; nodeIndex++) {
var selItem = available_languages.selectedItems[nodeIndex];
var selRow = selItem.firstChild;
var selCell = selRow.firstChild;
2006-05-17 02:25:34 +00:00
Languagename = selCell.getAttribute('value');
Languageid = selCell.getAttribute('id');
already_active = false;
if (!LangAlreadyActive(Languageid)) {
2006-05-17 02:25:34 +00:00
AddTreeItem(window.opener.document, active_languages_treeroot, Languageid, Languagename);
2006-05-17 02:25:34 +00:00
}//if
} //loop selected languages
2006-05-17 02:25:34 +00:00
//user-defined languages
var otherField = document.getElementById( "languages.other" );
2006-05-17 02:25:34 +00:00
if (otherField.value) {
dump("Other field: " + otherField.value + "\n");
Languageid = otherField.value;
Languageid = Languageid.toLowerCase();
Languagename = GetLanguageTitle(Languageid);
if (!Languagename)
Languagename = '[' + Languageid + ']';
2006-05-17 02:25:34 +00:00
already_active = false;
2006-05-17 02:25:34 +00:00
if (!LangAlreadyActive(Languageid)) {
2006-05-17 02:25:34 +00:00
AddTreeItem(window.opener.document, active_languages_treeroot, Languageid, Languagename);
2006-05-17 02:25:34 +00:00
}//if
}
available_languages.clearItemSelection();
window.close();
} //AddAvailableLanguage
function RemoveActiveLanguage()
{
2006-05-17 02:25:34 +00:00
var nextNode = null;
2006-05-17 02:25:34 +00:00
var numSelected = active_languages.selectedItems.length;
var deleted_all = false;
2006-05-17 02:25:34 +00:00
while (active_languages.selectedItems.length > 0) {
2006-05-17 02:25:34 +00:00
var selectedNode = active_languages.selectedItems[0];
nextNode = selectedNode.nextSibling;
if (!nextNode)
2006-05-17 02:25:34 +00:00
if (selectedNode.previousSibling)
nextNode = selectedNode.previousSibling;
var row = selectedNode.firstChild;
var cell = row.firstChild;
2006-05-17 02:25:34 +00:00
row.removeChild(cell);
selectedNode.removeChild(row);
active_languages_treeroot.removeChild(selectedNode);
} //while
if (nextNode) {
2006-05-17 02:25:34 +00:00
active_languages.selectItem(nextNode)
} else {
2006-05-17 02:25:34 +00:00
//active_languages.clearItemSelection();
}
2006-05-17 02:25:34 +00:00
2006-05-17 02:25:40 +00:00
UpdateSavePrefString();
2006-05-17 02:25:34 +00:00
} //RemoveActiveLanguage
2006-05-17 02:25:34 +00:00
function GetLanguageTitle(id)
{
if (availLanguageDict) for (j = 0; j < availLanguageDict.length; j++) {
if ( availLanguageDict[j][1] == id) {
//title =
dump("found title for:" + id + " ==> " + availLanguageDict[j][0] + "\n");
return availLanguageDict[j][0];
}
}
return '';
}
function AddTreeItem(doc, treeRoot, langID, langTitle)
{
try { //let's beef up our error handling for languages without label / title
// Create a treerow for the new Language
var item = doc.createElement('treeitem');
var row = doc.createElement('treerow');
var cell = doc.createElement('treecell');
// Copy over the attributes
cell.setAttribute('value', langTitle);
cell.setAttribute('id', langID);
// Add it to the active languages tree
item.appendChild(row);
row.appendChild(cell);
treeRoot.appendChild(item);
dump("*** Added tree item: " + langTitle + "\n");
} //try
catch (ex) {
dump("*** Failed to add item: " + langTitle + "\n");
} //catch
}
2006-05-17 02:25:40 +00:00
function UpdateSavePrefString()
2006-05-17 02:25:34 +00:00
{
var num_languages = 0;
2006-05-17 02:25:40 +00:00
pref_string = null;
2006-05-17 02:25:34 +00:00
2006-05-17 02:25:40 +00:00
dump("*** UpdateSavePrefString()\n");
2006-05-17 02:25:34 +00:00
for (var item = active_languages_treeroot.firstChild; item != null; item = item.nextSibling) {
row = item.firstChild;
cell = row.firstChild;
languageid = cell.getAttribute('id');
if (languageid.length > 1) {
num_languages++;
//separate >1 languages by commas
if (num_languages > 1) {
2006-05-17 02:25:40 +00:00
pref_string = pref_string + "," + " " + languageid;
2006-05-17 02:25:34 +00:00
} else {
2006-05-17 02:25:40 +00:00
pref_string = languageid;
2006-05-17 02:25:34 +00:00
} //if
} //if
}//for
2006-05-17 02:25:40 +00:00
dump("*** Pref string set to: " + pref_string + "\n");
parent.hPrefWindow.setPref( "string", "intl.accept_languages", pref_string );
//Save();
2006-05-17 02:25:34 +00:00
}
function Save()
{
2006-05-17 02:25:34 +00:00
dump('*** Save()\n');
2006-05-17 02:25:34 +00:00
try
{
2006-05-17 02:25:40 +00:00
var prefInt = null;
2006-05-17 02:25:34 +00:00
if (!prefInt) {
prefInt = Components.classes["@mozilla.org/preferences;1"];
2006-05-17 02:25:34 +00:00
if (prefInt) {
prefInt = prefInt.getService();
prefInt = prefInt.QueryInterface(Components.interfaces.nsIPref);
}
}
if (prefInt)
{
2006-05-17 02:25:40 +00:00
prefInt.SetCharPref("intl.accept_languages", pref_string);
dump('*** saved pref: ' + pref_string + '.\n');
}
2006-05-17 02:25:34 +00:00
}
2006-05-17 02:25:34 +00:00
catch(ex)
{
dump("*** Couldn't save!\n");
}
} //Save
function MoveUp() {
2006-05-17 02:25:34 +00:00
if (active_languages.selectedItems.length == 1) {
var selected = active_languages.selectedItems[0];
var before = selected.previousSibling
if (before) {
before.parentNode.insertBefore(selected, before);
2006-05-17 02:25:34 +00:00
active_languages.selectItem(selected);
active_languages.ensureElementIsVisible(selected);
}
}
2006-05-17 02:25:40 +00:00
UpdateSavePrefString();
2006-05-17 02:25:34 +00:00
} //MoveUp
function MoveDown() {
2006-05-17 02:25:34 +00:00
if (active_languages.selectedItems.length == 1) {
var selected = active_languages.selectedItems[0];
if (selected.nextSibling) {
if (selected.nextSibling.nextSibling) {
selected.parentNode.insertBefore(selected, selected.nextSibling.nextSibling);
}
else {
selected.parentNode.appendChild(selected);
}
2006-05-17 02:25:34 +00:00
active_languages.selectItem(selected);
}
}
2006-05-17 02:25:40 +00:00
UpdateSavePrefString();
2006-05-17 02:25:34 +00:00
} //MoveDown