1 //FlickrLocalisation, script to help localise user script for Flickr
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.
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.
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.
24 // The GNU General Public License is available by visiting
25 // http://www.gnu.org/copyleft/lgpl.html
27 // Free Software Foundation, Inc.
28 // 51 Franklin Street, Fifth Floor
29 // Boston, MA 02110-1301
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({
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'
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'
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 /***********************************************************************
78 **********************************************************************/
80 var FlickrLocaliser = function(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')]",
93 XPathResult.FIRST_ORDERED_NODE_TYPE, null
96 var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
97 if(matches && matches[1]) {
98 this.selectedLang = matches[1];
99 return this.selectedLang;
103 } else return this.selectedLang;
106 init: function(locals) {
107 this.localisations = locals;
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];
116 local = this.localisations[this.localisations.defaultLang][string];
118 if(!local) return string;
120 while(matches = /@([^@]+)@/g.exec(local)) {
122 var arg = matches[1];
123 var rep = new RegExp('@'+arg+'@','g');
125 toRet = toRet.replace(rep,params[arg]);
127 toRet = toRet.replace(rep,'');
131 } else return undefined;
136 /*****************************Flickr Localisation**********************/