Add skeleton support for translations

This commit is contained in:
Neil Bird 2021-02-28 14:20:11 +00:00
parent 2a509a57db
commit f98535b06a
5 changed files with 44 additions and 15 deletions

View file

@ -1,4 +1,6 @@
// common
const Config = imports.misc.config;
const Gettext = imports.gettext;
const Gio = imports.gi.Gio;
// settings labels
@ -38,3 +40,19 @@ function getSettings(extension)
return new Gio.Settings({ settings_schema: schemaObj });
}
// Initialize Gettext to load translations from extensionsdir/locale.
function initTranslations(extension)
{
let domain = extension.metadata['gettext-domain'];
// check if this extension was built with "make zip-file", and thus
// has the locale files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell
let localeDir = extension.dir.get_child('locale');
if (localeDir.query_exists(null))
Gettext.bindtextdomain(domain, localeDir.get_path());
else
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
}

View file

@ -30,6 +30,8 @@ let PiHoleExt = {
// Common
const Common = PiHoleExt.Metadata.imports.common;
const Gettext = imports.gettext.domain( PiHoleExt.Metadata.metadata['gettext-domain'] );
const _ = Gettext.gettext;
// Implement MythTV class
@ -76,6 +78,7 @@ const PiHole = new Lang.Class(
{
// Core setup
this.parent(null, IndicatorName);
Common.initTranslations( PiHoleExt.Metadata );
// Settings
let settings = Common.getSettings( PiHoleExt.Metadata );
@ -120,7 +123,7 @@ const PiHole = new Lang.Class(
// .. status
let box = new St.BoxLayout({style_class:'pihole-heading-row'});
let label = new St.Label({style_class:'pihole-label', text:"Pi-Hole Status: "});
let label = new St.Label({style_class:'pihole-label', text:_("Pi-Hole Status") + ": "});
box.add_actor(label);
this.StatusField = new St.Label({text:this.Status});
box.add_actor(this.StatusField);
@ -130,14 +133,14 @@ const PiHole = new Lang.Class(
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// .. control buttons
this.PauseButton = new PopupMenu.PopupMenuItem("Pause temporarily");
this.PauseButton = new PopupMenu.PopupMenuItem(_("Pause temporarily"), {style_class:"pihole-indent"});
this.PauseButton.connect('activate', Lang.bind(this, function() {
this.onPauseButton();
return 0;
}));
this.menu.addMenuItem(this.PauseButton);
//
this.EnableDisableButton = new PopupMenu.PopupMenuItem("Disable");
this.EnableDisableButton = new PopupMenu.PopupMenuItem(_("Disable"), {style_class:"pihole-indent"});
this.EnableDisableButton.connect('activate', Lang.bind(this, function() {
this.onEnableDisableButton();
return 0;
@ -148,7 +151,7 @@ const PiHole = new Lang.Class(
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// .. settings
this.SettingsButton = new PopupMenu.PopupMenuItem("Settings");
this.SettingsButton = new PopupMenu.PopupMenuItem(_("Settings"), {style_class:"pihole-indent"});
this.SettingsButton.connect('activate', Lang.bind(this, function() {
this.onSettingsButton();
return 0;

View file

@ -5,5 +5,6 @@
"url" : "https://github.com/fnxweb/gnome-shell-pi-hole",
"description": "Status and basic controls of local Pi-Hole",
"settings-schema": "org.gnome.shell.extensions.fnxweb-pi-hole",
"gettext-domain": "gnome-shell-extension-fnxweb-pi-hole",
"version": 0
}

View file

@ -1,11 +1,12 @@
// Prefs widget
//const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const ExtensionUtils = imports.misc.extensionUtils;
const Gtk = imports.gi.Gtk;
const Metadata = ExtensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain( Metadata.metadata['gettext-domain'] );
const _ = Gettext.gettext;
const Common = Metadata.imports.common;
@ -17,6 +18,7 @@ let settings;
function init()
{
settings = Common.getSettings(Metadata);
Common.initTranslations(Metadata);
}
@ -29,43 +31,43 @@ function buildPrefsWidget()
// Settings
{
let widget = new Gtk.Entry({ width_chars: 50, tooltip_text: "URL of pi-hole admin page for API access" });
let widget = new Gtk.Entry({ width_chars: 50, tooltip_text: _("URL of pi-hole admin page for API access") });
widget.set_text( settings.get_string( Common.URL_SETTING ) );
widget.connect( 'changed', function() {
settings.set_string( Common.URL_SETTING, widget.get_text() );
});
_addSetting( prefs, "Pi-Hole URL", widget );
_addSetting( prefs, _("Pi-Hole URL"), widget );
}
{
let widget = new Gtk.Entry({ width_chars: 50, tooltip_text: "API key of pi-hole from settings/api page" });
let widget = new Gtk.Entry({ width_chars: 50, tooltip_text: _("API key of pi-hole from settings/api page") });
widget.set_text( settings.get_string( Common.API_KEY_SETTING ) );
widget.connect( 'changed', function() {
settings.set_string( Common.API_KEY_SETTING, widget.get_text() );
});
_addSetting( prefs, "API key", widget );
_addSetting( prefs, _("API key"), widget );
}
{
let widget = new Gtk.SpinButton({ tooltip_text: "Rate at which Pi-Hole is normally polled for its status" });
let widget = new Gtk.SpinButton({ tooltip_text: _("Rate at which Pi-Hole is normally polled for its status") });
widget.set_range( 1, 900 );
widget.set_increments( 1, 5 );
widget.set_value( settings.get_uint( Common.UPDATE_RATE_SETTING ) );
widget.connect( 'value-changed', function() {
settings.set_uint( Common.UPDATE_RATE_SETTING, widget.get_value() );
});
_addSetting( prefs, "Update rate (seconds)", widget );
_addSetting( prefs, _("Update rate (seconds)"), widget );
}
{
let widget = new Gtk.SpinButton({ tooltip_text: "How long to pause Pi-Hole for when it is paused" });
let widget = new Gtk.SpinButton({ tooltip_text: _("How long to pause Pi-Hole for when it is paused") });
widget.set_range( 1, 900 );
widget.set_increments( 1, 5 );
widget.set_value( settings.get_uint( Common.DISABLE_TIME_SETTING ) );
widget.connect( 'value-changed', function() {
settings.set_uint( Common.DISABLE_TIME_SETTING, widget.get_value() );
});
_addSetting( prefs, "Pause time (seconds)", widget );
_addSetting( prefs, _("Pause time (seconds)"), widget );
}
// Done

View file

@ -7,3 +7,8 @@ StLabel
{
color: #999999;
}
.pihole-indent
{
padding-left: 2ex;
}