суббота, 9 мая 2009 г.

Scripting Lokalize

I started working on scripting support in Lokalize several months ago, but never really announced this because I wanted to write my own scripts and refine/extend API accordingly along the way.

KDE 4.3 will bring us 'officially' scriptable Lokalize. Among recent additions are new project wizard with anonsvn checkout support (screencast; it also checks out templates/ and scripts/ folders), actions to update .po from template and to compile .po into .mo and place it in local .kde folder so it could be picked up automatically by applications on the next start (see here and here for python sources).

Recently I also extended pology with ability to communicate to Lokalize, so now you can run xml checks against your translations and get erroneous files opened in Lokalize and positioned on appropriate entry.

So how it works: on each project open Lokalize scans PROJECTDIR/lokalize-scripts folder for .rc files and adds them to a 'cache' file called PROJECTDIR/lokalize-scripts/scripts.rc (so you shouldn't generally want to add it to version control system). RC files contain descriptions of scripts, particularly their paths. The path is relative to .rc file folder (or to a system scripts folder - they are tried both). So you for example can specify "../../scripts/lokalize/opensrc.py" to load script from global kde-l10n4 scripts folder (i.e. not specific to your language). Each script is represented with action in application menu (and you may assign a shortcut to it).

I wrote a script (in qtscript/javascript) to do a check specific to Russian language: %1 should be mentioned in _all_ plural forms, because, in Russian, the first form is also used for 21, 31 and so on. You can find it here. The script contains no immediate code, but only a function. The name of the function (fileSaved) is essential, because it matches the signal emitted by Editor object, which allows us to communicate with current editor tab in Lokalize. The function is automatically called on each files save, and if file contains errors, a warning is issued and wrong entries are shown. Scripts are actually don't get loaded until user explicitly triggers their action in the menu, so to override this behaviour RC file specifies <property name="autorun" >true</property>.

Below are links to API references. Everything marked as Q_SCRIPTABLE may be used from scripts
Editor object API reference
Lokalize object API reference
Project object API reference

You are welcome to write your own scripts and polish existing ones.

Комментариев нет: