Simple guide to improve your own extension on gnome-shell


This guide does not will include all aspects of the creation of the extension for gnome shell, but will be specific to the management of the localization and the management of storing the preferences of the extension itself, of these topics did not find much on the net so i summarize what I found for reminders. If you want to investigate other aspects of development i recommend you visit these articles:

well assuming you have already created your extension, and we want to implement additional language will need to use the tools gettext; the first thing is to include the support files convenience.js, this will facilitate the translation and persistence of preference; then edit the file in which you must get a translation by adding the function gettext ( _ for macro) before each string to be translated, like this example:

...
const Gettext = imports.gettext.domain('...'); //UUID extension
const _ = Gettext.gettext;
...

// Put your extension initialization code here
function init(metadata) {
    ...
    Convenience.initTranslations();
    ...
}

function showHello() {
    ...
    let text =  _("Hello, world!");
    ...
}

Now all you need to create the actual translation of the strings, open a terminal and move into the folder containing the file extension, and create the file .pot that will hold all the strings to translate with this command:

xgettext -L Python --from-code=UTF-8 -k_ -kN_ -o messages.pot *.js

then we will use a part of the gettext tools to automate the translation with this command:

msginit --locale=$lang --input=messages.pot

replaces $lang with the language code of the translation, visit this page for help; if the previous command is successful will have created a file $lang.po while containing the translated strings, after checking and possibly correct the automatic translations with the program poedit compile this file to create the binary file .mo, so as to be used in the installation of user, with this command:

mkdir -p locale/$lang/LC_MESSAGES
msgfmt $lang.po -o locale/$lang/LC_MESSAGES/$UUID.mo

replaces $lang with the language code of the translation and $UUID with the ID of the extension.
The part for the localization is over now let’s see how to handle the preferences of the extension; it and its GUI are created inside a file that must be named prefs.js, here you can find an example; to facilitate the work of designing the GUI you can use the program glade
Schermata del 2013-03-10
this allows you to visually create the interface and then use it inside the extension with the library GtkBuilder, this is a sample code:

// creates the ui builder and add the main resource file
let uiFilePath = Me.path + "/test.gtkbuilder";
let builder = new Gtk.Builder();

if (builder.add_from_file(uiFilePath) == 0) {
    global.log("JS LOG: could not load the ui file: %s".format(uiFilePath));

    let label = new Gtk.Label({
        label: _("Could not load the preferences UI file"),
        vexpand: true
    });

    this.pack_start(label, true, true, 0);
} else {
    global.log('JS LOG:_UI file receive and load: '+uiFilePath);

    ...
}

now to complete the extension must create a binary file from our schema of preferences previously created, this is the command:

glib-compile-schemas $path

replaces $path with the path of your exstension, to see any errors and do basic debug you can investigate into the log file of gdm:

cat  ~/.cache/gdm/session.log | grep JS\ LOG

here the complete project containing a test extension that i created by following the steps listed above: Download PRJ
that’s all.8y3

Un pensiero su “Simple guide to improve your own extension on gnome-shell

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...