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

605 lines
15 KiB
JavaScript
Raw Normal View History

/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Adrian Havill <havill@redhat.com>
*/
2006-05-17 02:25:34 +00:00
//GLOBALS
//locale bundles
2006-05-17 02:22:36 +00:00
var regionsBundle;
var languagesBundle;
var acceptedBundle;
var prefLangBundle;
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 (!prefLangBundle) prefLangBundle = srGetStrBundle("chrome://communicator/locale/pref/pref-languages.properties");
if (!acceptedBundle) acceptedBundle = srGetStrBundle("resource:/res/language.properties");
2006-05-17 02:22:36 +00:00
}
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
try {
GetBundles();
2006-05-17 02:25:34 +00:00
}
2006-05-17 02:22:36 +00:00
2006-05-17 02:25:34 +00:00
catch(ex) {
}
2006-05-17 02:22:36 +00:00
ReadAvailableLanguages();
if (!("arguments" in window)) {
//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');
2006-05-17 02:25:34 +00:00
} //try
catch(ex) {
} //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) {
//pref service backup
} //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");
} //try
2006-05-17 02:25:40 +00:00
catch(ex) {
2006-05-17 02:25:34 +00:00
} //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');
2006-05-17 02:25:34 +00:00
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');
pref_string = window.opener.document.getElementById('intlAcceptLanguages').label;
2006-05-17 02:25:34 +00:00
} //try
catch(ex) {
2006-05-17 02:25:40 +00:00
} //catch
LoadAvailableLanguages();
2006-05-17 02:25:34 +00:00
}
SelectLanguage();
2006-05-17 02:22:36 +00:00
}
function AddLanguage()
{
2006-05-17 02:25:40 +00:00
//cludge: make pref string available from the popup
document.getElementById('intlAcceptLanguages').label = pref_string;
window.openDialog("chrome://communicator/content/pref/pref-languages-add.xul","","modal=yes,chrome,resizable=yes", "addlangwindow");
2006-05-17 02:25:40 +00:00
UpdateSavePrefString();
SelectLanguage();
}
2006-05-17 02:22:36 +00:00
function ReadAvailableLanguages()
{
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;
var acceptedBundleEnum = acceptedBundle.getSimpleEnumeration();
var curItem;
var stringName;
var stringNameProperty;
2006-05-17 02:22:36 +00:00
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);
2006-05-17 02:22:36 +00:00
//dump string name (key)
stringName = curItem.getKey();
2006-05-17 02:22:36 +00:00
stringNameProperty = stringName.split('.');
if (stringNameProperty[1] == 'accept') {
//dump the UI string (value)
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];
var stringLangRegion = stringNameProperty[0].split('-');
2006-05-17 02:22:36 +00:00
if (stringLangRegion[0]) {
var tit;
try {
tit = languagesBundle.GetStringFromName(stringLangRegion[0]);
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:22:36 +00:00
catch (ex) {
tit = "";
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
if (stringLangRegion.length > 1) {
try {
tit += "/" + regionsBundle.GetStringFromName(stringLangRegion[1]);
2006-05-17 02:25:34 +00:00
}
2006-05-17 02:25:34 +00:00
catch (ex) {
tit += "["+str+"]";
2006-05-17 02:25:34 +00:00
}
} //if region
tit = tit + " [" + str + "]";
2006-05-17 02:22:36 +00:00
} //if language
if (str && tit) {
availLanguageDict[i] = new Array(3);
availLanguageDict[i][0] = tit;
availLanguageDict[i][1] = str;
availLanguageDict[i][2] = visible;
2006-05-17 02:22:36 +00:00
i++;
2006-05-17 02:22:36 +00:00
} // if str&tit
2006-05-17 02:25:34 +00:00
//} //if visible
} //if accepted
} //while
availLanguageDict.sort();
}
function LoadAvailableLanguages()
{
if (availLanguageDict)
for (var i = 0; i < availLanguageDict.length; i++) {
if (availLanguageDict[i][2] == 'true') {
2006-05-17 02:25:34 +00:00
AddTreeItem(document, available_languages_treeroot, availLanguageDict[i][1], availLanguageDict[i][0]);
2006-05-17 02:25:34 +00:00
} //if
} //for
}
function LoadActiveLanguages()
{
if (pref_string) {
var arrayOfPrefs = pref_string.split(', ');
for (var i = 0; i < arrayOfPrefs.length; i++) {
var str = arrayOfPrefs[i];
var tit = GetLanguageTitle(str);
if (str) {
if (!tit)
tit = '[' + str + ']';
2006-05-17 02:25:34 +00:00
AddTreeItem(document, active_languages_treeroot, str, tit);
} //if
} //for
}
2006-05-17 02:22:36 +00:00
}
2006-05-17 02:25:34 +00:00
function LangAlreadyActive(langId)
2006-05-17 02:22:36 +00:00
{
var found = false;
2006-05-17 02:22:36 +00:00
try {
var arrayOfPrefs = pref_string.split(', ');
if (arrayOfPrefs)
for (var 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 isAlpha(mixedCase) {
var allCaps = mixedCase.toUpperCase();
for (var i = allCaps.length - 1; i >= 0; i--) {
var c = allCaps.charAt(i);
if (c < 'A' || c > 'Z') return false;
}
return true;
}
function isAlphaNum(mixedCase) {
var allCaps = mixedCase.toUpperCase();
for (var i = allCaps.length - 1; i >= 0; i--) {
var c = allCaps.charAt(i);
if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) return false;
}
return true;
}
function IsRFC1766LangTag(candidate) {
/* Valid language codes examples:
i.e. ja-JP-kansai (Kansai dialect of Japanese)
en-US-texas (Texas dialect)
i-klingon-tng (did TOS Klingons speak in non-English?)
sgn-US-MA (Martha Vineyard's Sign Language)
*/
var tags = candidate.split('-');
/* if not IANA "i" or a private "x" extension, the primary
tag should be a ISO 639 country code, two or three letters long.
we don't check if the country code is bogus or not.
*/
var checkedTags = 0;
if (tags[0].toLowerCase() != "x" && tags[0].toLowerCase() != "i") {
if (tags[0].length != 2 && tags[0].length != 3) return false;
if (!isAlpha(tags[0])) return false;
checkedTags++;
/* the first subtag can be either a 2 letter ISO 3166 country code,
or an IANA registered tag from 3 to 8 characters.
*/
if (tags.length > 1) {
if (tags[1].length < 2 || tags[1].length > 3) return false;
if (!isAlphaNum(tags[1])) return false;
/* do not allow user-assigned ISO 3166 country codes */
if (tags[1].length == 2 && isAlpha(tags[1])) {
var countryCode = tags[1].toUpperCase();
if (countryCode == "AA" || countryCode == "ZZ") return false;
if (countryCode[0] == 'X') return false;
if (countryCode[0] == 'Q' && countryCode[1] > 'L') return false;
}
checkedTags++;
}
}
else if (tags.length < 2) return false;
else {
if ((tags[1].length < 1) || (tags[1].length > 8)) return false;
if (!isAlphaNum(tags[1])) return false;
checkedTags++;
}
/* any remaining subtags must be one to eight alphabetic characters */
for (var i = checkedTags; i < tags.length; i++) {
if ((tags[1].length < 1) || (tags[i].length > 8)) return false;
if (!isAlphaNum(tags[i])) return false;
checkedTags++;
}
return true;
}
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;
Languagename = selCell.getAttribute('label');
Languageid = selCell.getAttribute('id');
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);
}//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) {
/* reject bogus lang strings, INCLUDING those with HTTP "q"
values kludged on the end of them
*/
if (!IsRFC1766LangTag(otherField.value)) {
window.alert(prefLangBundle.GetStringFromName("illegalOtherLanguage"));
return false;
}
Languageid = otherField.value;
2006-05-17 02:25:34 +00:00
Languageid = Languageid.toLowerCase();
Languagename = GetLanguageTitle(Languageid);
if (!Languagename)
Languagename = '[' + Languageid + ']';
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);
}//if
2006-05-17 02:25:34 +00:00
}
available_languages.clearItemSelection();
return true;
} //AddAvailableLanguage
function HandleDoubleClick(node)
{
var languageId = node.id;
var languageName = node.getAttribute('label');
if (languageName && languageName.length > 0)
{
if (!LangAlreadyActive(languageId)) {
AddTreeItem(window.opener.document, active_languages_treeroot, languageId, languageName);
}
window.close();
}//if
} //HandleDoubleClick
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) {
var selectedNode = active_languages.selectedItems[0];
nextNode = selectedNode.nextSibling;
if (!nextNode)
if (selectedNode.previousSibling)
nextNode = selectedNode.previousSibling;
var row = selectedNode.firstChild;
var cell = row.firstChild;
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
function GetLanguageTitle(id)
2006-05-17 02:25:34 +00:00
{
if (availLanguageDict)
for (var j = 0; j < availLanguageDict.length; j++) {
if ( availLanguageDict[j][1] == id) {
//title =
return availLanguageDict[j][0];
}
}
return '';
2006-05-17 02:25:34 +00:00
}
function AddTreeItem(doc, treeRoot, langID, langTitle)
{
try { //let's beef up our error handling for languages without label / title
2006-05-17 02:25:34 +00:00
// Create a treerow for the new Language
var item = doc.createElement('treeitem');
var row = doc.createElement('treerow');
var cell = doc.createElement('treecell');
2006-05-17 02:25:34 +00:00
// Copy over the attributes
cell.setAttribute('label', langTitle);
cell.setAttribute('id', langID);
2006-05-17 02:25:34 +00:00
// Add it to the active languages tree
item.appendChild(row);
row.appendChild(cell);
2006-05-17 02:25:34 +00:00
treeRoot.appendChild(item);
2006-05-17 02:25:34 +00:00
} //try
2006-05-17 02:25:34 +00:00
catch (ex) {
} //catch
2006-05-17 02:25:34 +00:00
}
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
for (var item = active_languages_treeroot.firstChild; item != null; item = item.nextSibling) {
var row = item.firstChild;
var cell = row.firstChild;
var languageid = cell.getAttribute('id');
2006-05-17 02:25:34 +00:00
if (languageid.length > 1) {
2006-05-17 02:25:34 +00:00
num_languages++;
//separate >1 languages by commas
if (num_languages > 1) {
pref_string = pref_string + "," + " " + languageid;
} else {
pref_string = languageid;
} //if
} //if
2006-05-17 02:25:34 +00:00
}//for
2006-05-17 02:25:40 +00:00
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
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"];
if (prefInt) {
prefInt = prefInt.getService();
prefInt = prefInt.QueryInterface(Components.interfaces.nsIPref);
}
2006-05-17 02:25:34 +00:00
}
if (prefInt)
{
prefInt.SetCharPref("intl.accept_languages", pref_string);
}
}
catch(ex)
{
}
} //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
function SelectLanguage() {
if (active_languages.selectedItems.length) {
document.getElementById("remove").disabled = false;
var selected = active_languages.selectedItems[0];
document.getElementById("down").disabled = !selected.nextSibling;
document.getElementById("up").disabled = !selected.previousSibling;
}
else {
document.getElementById("remove").disabled = true;
document.getElementById("down").disabled = true;
document.getElementById("up").disabled = true;
}
if (parent.hPrefWindow.getPrefIsLocked(document.getElementById("up").getAttribute("prefstring")))
document.getElementById("up").disabled = true;
if (parent.hPrefWindow.getPrefIsLocked(document.getElementById("down").getAttribute("prefstring")))
document.getElementById("down").disabled = true;
if (parent.hPrefWindow.getPrefIsLocked(document.getElementById("add").getAttribute("prefstring")))
document.getElementById("add").disabled = true;
if (parent.hPrefWindow.getPrefIsLocked(document.getElementById("remove").getAttribute("prefstring")))
document.getElementById("remove").disabled = true;
} // SelectLanguage