6V8 - Production of my Mind

Home page > For your Computer > Greasemonkey > Localising Flickr Greasemonkey scripts

Localising Flickr Greasemonkey scripts

 
Flickr introduced localisation [1] of their interface in multiple language. However, most of the Greasemonkey scripts that have been developed up to now were in English.
13 June 2007, by Mortimer

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.

greasemonkey - 4.7 kb
FlickrLocaliser source
389 download(s) since 25 July 2007

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.

Date of online publication: 13 June 2007
last-update: 27 June 2007
Keywords : Flickr , Greasemonkey
Forum messages 1
visits:
1689

Creative Commons Attribution NonCommercial ShareAlike 2.5  License

notes

[1] translations

[2] Note that the license requires you to keep the license notice and attribution

 

P.S.

Wanna help translate Flickr GM scripts? you can use the interface provided here.

Reply to this article

1 Message

 

The most read articles

 
©
Pierre Andrews
York, uk
| Site Map | Site created with SPIP 1.9.2c [10268] | RSS | template by IZO, Mortimer. | clicky stats
In very up Backing Tina
In very up Backing Tina
In very up Backing Tina
In very up Backing Tina
In very up Backing Tina