OSDN Git Service

import source-tree based svn r84.
[bluegriffon/BlueGriffon.git] / base / content / bluegriffon / js / languages.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the Firefox Preferences System.
15  *
16  * The Initial Developer of the Original Code is
17  * Ben Goodger.
18  * Portions created by the Initial Developer are Copyright (C) 2005
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *  Ben Goodger <ben@mozilla.org>
23  *  Adrian Havill <havill@redhat.com>
24  *  Steffen Wilberg <steffen.wilberg@web.de>
25  *  Daniel Glazman <daniel.glazman@disruptive-innovations.com>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40  
41 /* code chunk needed in dialog or window is
42     <stringbundleset id="languageSet">
43       <stringbundle id="bundleRegions"      src="chrome://global/locale/regionNames.properties"/>
44       <stringbundle id="bundleLanguages"    src="chrome://global/locale/languageNames.properties"/>
45       <stringbundle id="bundlePreferences"  src="chrome://bluegriffon/locale/language.properties"/>
46       <stringbundle id="bundleAccepted"     src="resource://gre/res/language.properties"/>
47     </stringbundleset>
48  */
49
50
51 var BGLanguagesHelper = {
52
53   _availableLanguagesList : [],
54   _acceptLanguages        : { },
55   
56   _selectedItemID         : null,
57   
58   init: function ()
59   {
60     var languageListBox = document.getElementById("languageListBox");
61     if (languageListBox)
62     {
63       this._loadAvailableLanguages();
64     }
65   },
66   
67   get _activeLanguages()
68   {
69     return document.getElementById("activeLanguages");
70   },
71   
72   get _availableLanguages()
73   {
74     return document.getElementById("availableLanguages");
75   },
76   
77   _loadAvailableLanguages: function ()
78   {
79     // This is a parser for: resource://gre/res/language.properties
80     // The file is formatted like so:
81     // ab[-cd].accept=true|false
82     //  ab = language
83     //  cd = region
84     var bundleAccepted    = document.getElementById("bundleAccepted");
85     var bundleRegions     = document.getElementById("bundleRegions");
86     var bundleLanguages   = document.getElementById("bundleLanguages");
87     var bundlePreferences = document.getElementById("bundlePreferences");
88
89     function LanguageInfo(aName, aABCD, aIsVisible)
90     {
91       this.name = aName;
92       this.abcd = aABCD;
93       this.isVisible = aIsVisible;
94     }
95
96     // 1) Read the available languages out of language.properties
97     var strings = bundleAccepted.strings;
98     while (strings.hasMoreElements()) {
99       var currString = strings.getNext();
100       if (!(currString instanceof Components.interfaces.nsIPropertyElement))
101         break;
102       
103       var property = currString.key.split("."); // ab[-cd].accept
104       if (property[1] == "accept") {
105         var abCD = property[0];
106         var abCDPairs = abCD.split("-");      // ab[-cd]
107         var useABCDFormat = abCDPairs.length > 1;
108         var ab = useABCDFormat ? abCDPairs[0] : abCD;
109         var cd = useABCDFormat ? abCDPairs[1] : "";
110         if (ab) {
111           var language = "";
112           try {
113             language = bundleLanguages.getString(ab);
114           } 
115           catch (e) { continue; };
116           
117           var region = "";
118           if (useABCDFormat) {
119             try {
120               region = bundleRegions.getString(cd);
121             }
122             catch (e) { continue; }
123           }
124           
125           var name = "";
126           if (useABCDFormat)
127             name = bundlePreferences.getFormattedString("languageRegionCodeFormat", 
128                                                         [language, region, abCD]);
129           else
130             name = bundlePreferences.getFormattedString("languageCodeFormat", 
131                                                         [language, abCD]);
132           
133           if (name && abCD) {
134             var isVisible = currString.value == "true" && 
135                             (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD]);
136             var li = new LanguageInfo(name, abCD, isVisible);
137             this._availableLanguagesList.push(li);
138           }
139         }
140       }
141     }
142     this._buildAvailableLanguageList();
143   },
144   
145   _buildAvailableLanguageList: function ()
146   {
147     var languageListBox = document.getElementById("languageListBox");
148     if (!languageListBox)
149       return;
150     while (languageListBox.hasChildNodes())
151       languageListBox.removeChild(languageListBox.firstChild);
152       
153     // Sort the list of languages by name
154     this._availableLanguagesList.sort(function (a, b) {
155                                         return a.name.localeCompare(b.name);
156                                       });
157                                   
158     // Load the UI with the data
159     for (var i = 0; i < this._availableLanguagesList.length; ++i) {
160       var abCD = this._availableLanguagesList[i].abcd;
161       if (this._availableLanguagesList[i].isVisible && 
162           (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD])) {
163         var menuitem = document.createElement("listitem");
164         menuitem.id = this._availableLanguagesList[i].abcd;
165         languageListBox.appendChild(menuitem);
166         menuitem.setAttribute("label", this._availableLanguagesList[i].name);
167         menuitem.setAttribute("value", this._availableLanguagesList[i].abcd);
168       }
169     }
170   },
171
172   showChoice: function(aElt)
173   {
174     var a = 1;
175     alert(aElt.value);
176   }
177 };
178