From 8e60fa48a8535da9eaa34ef35b5040c2eb68fc3b Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Thu, 28 Jun 2018 21:35:23 +0200 Subject: all: I18N support This adds I18N support using standard gettext system together with an initial German translation. This can be used to search locales in some directory: Intl.bindtextdomain("shopsystem", "/home/sre/src/serial-barcode-scanner"); And this can be used to switch language at runtime: Intl.setlocale(LocaleCategory.ALL, ""); --- src/pdf-invoice/Makefile | 4 ++-- src/pdf-invoice/main.vala | 17 ++++++++++------- src/pdf-invoice/pdf-invoice.vala | 20 ++++++++++---------- 3 files changed, 22 insertions(+), 19 deletions(-) (limited to 'src/pdf-invoice') diff --git a/src/pdf-invoice/Makefile b/src/pdf-invoice/Makefile index 9805f6e..91b8522 100644 --- a/src/pdf-invoice/Makefile +++ b/src/pdf-invoice/Makefile @@ -2,10 +2,10 @@ all: pdf-invoice @echo > /dev/null pdf-invoice: main.vala pdf-invoice.vala pdf-invoice-interface.vala ../config/config-interface.vala ../database/db-interface.vala ../price.vapi - valac -X -w -g -o $@ --pkg pangocairo --pkg librsvg-2.0 --pkg posix --pkg gdk-2.0 --pkg gio-2.0 $^ + valac -X -D'GETTEXT_PACKAGE="shopsystem"' -X -w -g -o $@ --pkg pangocairo --pkg librsvg-2.0 --pkg posix --pkg gdk-2.0 --pkg gio-2.0 $^ test: pdf-invoice-interface.vala ../database/db-interface.vala test.vala ../price.vapi - valac -X -w -o $@ --pkg gio-2.0 $^ + valac -X -D'GETTEXT_PACKAGE="shopsystem"' -X -w -o $@ --pkg gio-2.0 $^ clean: rm -rf pdf-invoice test diff --git a/src/pdf-invoice/main.vala b/src/pdf-invoice/main.vala index dc95bde..1848852 100644 --- a/src/pdf-invoice/main.vala +++ b/src/pdf-invoice/main.vala @@ -16,34 +16,37 @@ private string datadir; public static int main(string[] args) { + Intl.setlocale(LocaleCategory.ALL, ""); + Intl.textdomain("shopsystem"); + try { Config cfg = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Config", "/io/mainframe/shopsystem/config"); datadir = cfg.get_string("INVOICE", "datadir"); } catch(DBusError e) { - error("DBusError: %s\n", e.message); + error(_("DBus Error: %s\n"), e.message); } catch(IOError e) { - error("IOError: %s\n", e.message); + error(_("IO Error: %s\n"), e.message); } catch(KeyFileError e) { - error("Config Error: %s\n", e.message); + error(_("Config Error: %s\n"), e.message); } Bus.own_name( BusType.SYSTEM, "io.mainframe.shopsystem.InvoicePDF", BusNameOwnerFlags.NONE, - on_bus_aquired, + on_bus_acquired, () => {}, - () => stderr.printf("Could not aquire name\n")); + () => stderr.printf(_("Could not acquire name\n"))); new MainLoop().run(); return 0; } -void on_bus_aquired(DBusConnection conn) { +void on_bus_acquired(DBusConnection conn) { try { conn.register_object("/io/mainframe/shopsystem/invoicepdf", new InvoicePDF(datadir)); } catch(Error e) { - stderr.printf("Could not register service: %s\n", e.message); + stderr.printf(_("Could not register service: %s\n"), e.message); } } diff --git a/src/pdf-invoice/pdf-invoice.vala b/src/pdf-invoice/pdf-invoice.vala index d92c93f..b41817d 100644 --- a/src/pdf-invoice/pdf-invoice.vala +++ b/src/pdf-invoice/pdf-invoice.vala @@ -78,7 +78,7 @@ public class InvoicePDF { var svg = new Rsvg.Handle.from_file(file); svg.render_cairo(ctx); } catch(Error e) { - error("Could not load SVG: %s\n", e.message); + error(_("Could not load SVG: %s\n"), e.message); } } @@ -376,7 +376,7 @@ public class InvoicePDF { try { FileUtils.get_contents(datadir + "/" + "vat.txt", out vattext); } catch(GLib.FileError e) { - throw new IOError.FAILED("Could not open VAT template: %s", e.message); + throw new IOError.FAILED(_("Could not open VAT template: %s"), e.message); } text = text.replace("{{{VAT}}}", vattext); @@ -384,7 +384,7 @@ public class InvoicePDF { layout.set_markup(text, text.length); } catch(GLib.FileError e) { - error("File Error: %s\n", e.message); + error(_("File Error: %s\n"), e.message); } /* render text */ @@ -468,11 +468,11 @@ public class InvoicePDF { var price = @"$(e.price)€".replace(".", ","); if(e.price > 999999) { - throw new InvoicePDFError.PRICE_TOO_HIGH("Prices > 9999.99€ are not supported!"); + throw new InvoicePDFError.PRICE_TOO_HIGH(_("Prices > 9999.99€ are not supported!")); } if(tm.get_year() > 9999) { - throw new InvoicePDFError.TOO_FAR_IN_THE_FUTURE("Years after 9999 are not supported!"); + throw new InvoicePDFError.TOO_FAR_IN_THE_FUTURE(_("Years after 9999 are not supported!")); } /* if date remains the same do not add it again */ @@ -588,7 +588,7 @@ public class InvoicePDF { /* retry adding the entry */ if(!draw_invoice_table_entry(ctx, y, entry, out y)) { - throw new InvoicePDFError.ARTICLE_NAME_TOO_LONG("Article name \"%s\" does not fit on a single page!", entry.product.name); + throw new InvoicePDFError.ARTICLE_NAME_TOO_LONG(_("Article name \"%s\" does not fit on a single page!"), entry.product.name); } } } @@ -625,16 +625,16 @@ public class InvoicePDF { var ctx = new Cairo.Context(document); if(invoice_id == "") - throw new InvoicePDFError.NO_INVOICE_ID("No invoice ID given!"); + throw new InvoicePDFError.NO_INVOICE_ID(_("No invoice ID given!")); if(invoice_entries == null) - throw new InvoicePDFError.NO_INVOICE_DATA("No invoice data given!"); + throw new InvoicePDFError.NO_INVOICE_DATA(_("No invoice data given!")); if(invoice_date == 0) - throw new InvoicePDFError.NO_INVOICE_DATE("No invoice date given!"); + throw new InvoicePDFError.NO_INVOICE_DATE(_("No invoice date given!")); if(invoice_recipient.firstname == "" && invoice_recipient.lastname == "") - throw new InvoicePDFError.NO_INVOICE_RECIPIENT("No invoice recipient given!"); + throw new InvoicePDFError.NO_INVOICE_RECIPIENT(_("No invoice recipient given!")); /* first page */ draw_logo(ctx); -- cgit v1.2.3