I provide here, under LGPL, a little javascript tool that you can include in your Greasemonkey script to easily localise them.
The script is embedded in an object that will figure out the language of the Flickr interface currently used by the user and return the right translation to display with your Greasemonkey script.
1The script
Just get this code and insert it at the beginning of your greasemonkey script [2]. Be sure to have the scope protected properly so that other scripts using this tool wont interfere with this one.
2Setting up an instance
The first thing you have to do is set up an instance of the FlickrLocaliser by providing it the “dictionary” of localisation strings. This is done like this:
var localiser = new FlickrLocaliser({
'en-us' : {
'loading':'Fetching page:@nbrpage@/@total@.',
'showing':'Showing page:@page@/@total@.',
'enable':'Enable Auto Page',
'nophoto':'No more Photos'
},
'fr-fr':{
'loading':'Chargement de la page:@nbrpage@/@total@.',
'showing':'Page Actuelle:@page@/@total@.',
'enable':'Activer l\'Auto Pagination',
'nophoto':'Plus de Photos'
},
defaultLang:'en-us'
});
The constructor takes an object describing the availlable localisations as well as the default language of the script (used when no localisation is availlable for the current interface language).
Each localisation is provided by a variable with the language code:
- en-us for English (US)
- fr-fr for French (en)
- zh-hk for Chinese (HK)
- de-de for German
- es-us for Spanish (US)
- ko-kr for Korean
- it-it for Italian
- pt-br for Portuguese (Brazil)
That variable must contain an object with key pointing to localised strings in that language. Be sure, in the localised strings, to use HTML entities for non ASCII characters.
When used, the script will figure out which language is currently used and extract the right localised string, or the default one.
3Getting a localised string
In you script, when you have to display a text to the user, you shouldn’t put it in static in the script but use the localisation method and putting a new key in the localisation dictionary.
The simple call would look like:
var message = localiser.localise('enable');
This will return the localised string for the key enable
It can happen that some variable must be inserted in the text to display to the user. In that case, you can specify variable names in the localised string in the dictionary by putting them between @. For example:
'loading':'Fetching page:@nbrpage@/@total@.' uses two variables:nbrpage and total that will be replaced later when you are querying for the localised string.
You can use the extended call to localise to pass extra parameters, for example:
var nbr_page = 10;
var next_request=9;
var message = localiser.localise('showing',{'page':(next_request-1),'total':nbr_page})
The localise method will take the additional parameters and replace them in the localised string by their value. If no value is specified, the variable name is just removed from the string.



