better support for videos
[FlickrHacks.git] / _greasemonkey_tools_ / FlickrLocalisation.js
blob69bba8f856ec5b4a5ffaee63ccd24e405ca1d6d3
1 //FlickrLocalisation, script to help localise user script for Flickr
2 //version 0.4
3 //release 29 Jun 2007
4 //author: Pierre Andrews
5 // For reference: http://6v8.gamboni.org/Localising-Flickr-Greasemonkey.html
6 // and http://6v8.gamboni.org/Help-localise-Flickr-scripts.html
8 // --------------------------------------------------------------------
9 // Copyright (C) 2007 Pierre Andrews
10 // This script can be redistributed under the terms of the GNU LGPL, without
11 // modification of this licence and copyright notice. Attribution to the author should be
12 // kept at least in the source of the scripts.
13 // 
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU Lesser General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 // 
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 // GNU General Public License for more details.
23 // 
24 // The GNU General Public License is available by visiting
25 //   http://www.gnu.org/copyleft/lgpl.html
26 // or by writing to
27 //   Free Software Foundation, Inc.
28 //   51 Franklin Street, Fifth Floor
29 //   Boston, MA  02110-1301
30 //   USA
33 This is a set of function to help localise greasemonkey script (or other user scripts) on Flickr.
34 The FlickrLocaliser will deal with finding the language used by the current user and translate your strings in the right language (as long as you provide the translations).
36 You have to initialise it with an object describing the translations of your static strings:
37 var     localiser = new FlickrLocaliser({
38                                 'en-us' : {
39                                         'loading':'Fetching page:@nbrpage@/@total@.',
40                                                 'showing':'Showing page:@page@/@total@.',
41                                                 'enable':'Enable Auto Page',
42                                                 'nophoto':'No more Photos',
43                                                 'nodiscussion': "No more Discussions",
44                                                 'noreply':'No more Replies',
45                                                 'notopic':'No more Topics',
46                                                 'nocomment':'No more Comments',
47                                                 'nopeople':'No more People',
48                                                 'nocontact':'No more Contacts'
49                                                 },
50                                         'fr-fr':{
51                                         'loading':'Chargement de la page:@nbrpage@/@total@.',
52                                                 'showing':'Page Actuelle:@page@/@total@.',
53                                                 'enable':'Activer l\'Auto Pagination',
54                                                 'nophoto':'Plus de Photos',
55                                                 'nodiscussion': "Plus de Discussions",
56                                                 'noreply':'Pas d\'autre réponse',
57                                                 'notopic':'Pas d\'autre sujet',
58                                                 'nocomment':'Pas d\'autre commentaire',
59                                                 'nopeople':'Pas d\'autre Membre',
60                                                 'nocontact':'Pas d\'autre Contact'
61                                                 },
62                                                 defaultLang:'en-us'
63                 });
65 The object contains fields for each language provided by your application. In this example: 'en-us', 'fr-fr' which point to an object pointing key values to translation strings. The key values are the same in each language, just the right hand side changes. Remember to use HTML entities for special characters.
67 The object also contains a field defaultLang which will be used when the user uses a language without translations.
69 To translate a string, use:
70 var message = localiser.localise('showing',{'page':(self.next_request-1),'total':self.nbr_page})
72 The first parameter is the key for the translation. The second is a list of parameters to replace in the localised string. The part of the localised string between @and@ will be replaced by the corresponding parameter value.
76 /***********************************************************************
77  * Flickr Localisation
78  **********************************************************************/
80 var FlickrLocaliser = function(locals) {
81         this.init(locals);
83 FlickrLocaliser.prototype = {
84         selectedLang: undefined,
85         localisations: undefined,
87         getLanguage: function() {
88                 if(!this.selectedLang) {
89                         var langA = document.evaluate(
90                                                                                   "//p[@class='LanguageSelector']//a[contains(@class,'selected')]",
91                                                                                   document,
92                                                                                   null,
93                                                                                   XPathResult.FIRST_ORDERED_NODE_TYPE, null
94                                                                                   ).singleNodeValue
95                         if(langA) {
96                                 var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
97                                 if(matches && matches[1]) {
98                                         this.selectedLang = matches[1];
99                                         return this.selectedLang;
100                                 }
101                         }
102                         return false;
103                 } else return this.selectedLang;
104         },
106         init: function(locals) {
107                 this.localisations = locals;
108         },
110         localise: function(string, params) {
111                 if(this.localisations && this.getLanguage()) {
112                         var currentLang = this.localisations[this.selectedLang];
113                         if(!currentLang) currentLang = this.localisations[this.localisations.defaultLang];
114                         var local = currentLang[string];
115                         if(!local) {
116                                 local = this.localisations[this.localisations.defaultLang][string];
117                         } 
118                         if(!local) return string;
119                         var toRet = local;
120                         while(matches = /@([^@]+)@/g.exec(local)) {
121                                 if(matches[1]) {
122                                         var arg = matches[1];
123                                         var rep = new RegExp('@'+arg+'@','g');
124                                         if(params[arg])
125                                                 toRet = toRet.replace(rep,params[arg]);
126                                         else
127                                                 toRet = toRet.replace(rep,'');
128                                 }
129                         }
130                         return toRet;
131                 } else return undefined;
132         }
136 /*****************************Flickr Localisation**********************/