Creating extensions

Translate an extension

Modified from the pygtk FAQ.

First, any strings you want to translate have to use double quotes and be enclosed in _(), e.g. _("translatable string") as opposed to 'not translatable string'.

In your extension.js, include the following:

const Gettext = imports.gettext;

Gettext.bindtextdomain("my-extension", ExtensionSystem.extensionMeta["my-extension"].path + "/locale");

const _ = Gettext.gettext;

Where my-extension is the uuid in metadata.json.

Next, make sure you have gettext installed and extract the translatable string using the following command:

xgettext -k_ -kN_ -o messages.pot extension.js

Note: there is no Javascript syntax parser in gettext. You will get a warning that it will fallback to C. In certain circumstances, the default C parser is not able to properly detect the strings of your file. In theses cases, you can try to use the Perl parser which is sometimes more adapted to the Javascript syntax:

xgettext -L Perl --from-code=UTF-8 -k_ -kN_ -o messages.pot extension.js

In order to start a new translation, invoke msginit. This will create a new translation in the language of your current locale, or you can use:

LANG=sv_SE msginit

to create a new translation in e.g. swedish.

This will create a .po file. Edit it to add your translation.

Now add a directory for the translation in your extensions directory, e.g. my-extension/. In our example with a swedish translation:

mkdir -p locale/sv/LC_MESSAGES

And finally create a machine readable copy of the translation using:

msgfmt sv.po -o locale/sv/LC_MESSAGES/

If you want to update an existing translation, use:

msgmerge -U sv.po messages.pot

Multi-file extensions

If your extension is not trivial, you'll want to split it in multiple .js files. To import them from your extension's source code, do this:

/* This identifier string comes from your installation directory */
const Extension = imports.misc.extensionUtils.getCurrentExtension();
const MyModule = Extension.imports.myModule;

This will import myModule.js and you can use it as MyModule.

Projects/GnomeShell/Extensions/FAQ/CreatingExtensions (last edited 2013-11-22 17:00:10 by WilliamJonMcCann)