From eacfda214810727223f2e89cb5382c4d91ad79b0 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Fri, 4 May 2012 14:33:10 +0200
Subject: [PATCH 1/2] Bug 8044: First draft for Koha localization using
 Locale::Maketext

You have to use the new module Koha::I18N

Code example:
  use Koha::I18N;
  use CGI;

  my $input = new CGI;
  my $lh = Koha::I18N->get_handle_from_context($input, 'intranet');

  print $lh->maketext("Localized string!");

PO files are in locale/LANG/messages.po.
To create PO file for a language, just make a directory under locale/
and launch locale/update_po.pl.
The directory name must be in lowercase with '_' instead of '-'.
For example: for french, create locale/fr_fr/ directory.
Missing PO files are created, and existing ones are updated.
You can then translate the PO with your favorite editor. Strings will be
localized at runtime.
---
 Koha/I18N.pm          |   29 +++++++++++++++++++++++
 locale/en/messages.po |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
 locale/update_po.pl   |   61 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+)
 create mode 100644 Koha/I18N.pm
 create mode 100644 locale/en/messages.po
 create mode 100755 locale/update_po.pl

diff --git a/Koha/I18N.pm b/Koha/I18N.pm
new file mode 100644
index 0000000..a81a392
--- /dev/null
+++ b/Koha/I18N.pm
@@ -0,0 +1,29 @@
+package Koha::I18N;
+
+use base qw(Locale::Maketext);
+
+use C4::Templates;
+use C4::Context;
+
+use Locale::Maketext::Lexicon {
+    '*' => [Gettext => C4::Context->config('intranetdir') . '/locale/*/messages.po'],
+    '_AUTO' => 1,
+};
+
+sub get_handle_from_context {
+    my ($class, $cgi, $interface) = @_;
+
+    my $lh;
+    my $lang = C4::Templates::getlanguage($cgi, $interface);
+    if ($lang) {
+        $lh = $class->get_handle($lang)
+            or die "No language handle for '$lang'";
+    } else {
+        $lh = $class->get_handle()
+            or die "Can't get a language handle";
+    }
+
+    return $lh;
+}
+
+1;
diff --git a/locale/en/messages.po b/locale/en/messages.po
new file mode 100644
index 0000000..4423ae4
--- /dev/null
+++ b/locale/en/messages.po
@@ -0,0 +1,62 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: Koha\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-05-04 14:23+0200\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: \n"
+"Language-Team: English <koha-translate@lists.koha-community.org>\n"
+"Language: en\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: C4/Acquisition.pm:241
+msgid "Contract name"
+msgstr ""
+
+#: C4/Acquisition.pm:242
+msgid "Order number"
+msgstr ""
+
+#: C4/Acquisition.pm:243
+msgid "Entry date"
+msgstr ""
+
+#: C4/Acquisition.pm:244
+msgid "ISBN"
+msgstr ""
+
+#: C4/Acquisition.pm:245
+msgid "Author"
+msgstr ""
+
+#: C4/Acquisition.pm:246
+msgid "Title"
+msgstr ""
+
+#: C4/Acquisition.pm:247
+msgid "Publisher code"
+msgstr ""
+
+#: C4/Acquisition.pm:248
+msgid "Collection title"
+msgstr ""
+
+#: C4/Acquisition.pm:249
+msgid "Notes"
+msgstr ""
+
+#: C4/Acquisition.pm:250
+msgid "Quantity"
+msgstr ""
+
+#: C4/Acquisition.pm:251
+msgid "RRP"
+msgstr ""
diff --git a/locale/update_po.pl b/locale/update_po.pl
new file mode 100755
index 0000000..4987c6d
--- /dev/null
+++ b/locale/update_po.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+use Data::Dumper;
+use FindBin qw($Bin);
+
+my $localedir = $Bin;
+my $installdir = "$localedir/..";
+
+# Directories are not scanned recursively
+my @directories_to_scan = qw(
+    . C4 acqui admin authorities basket catalogue cataloguing circ errors
+    Koha labels members misc offline_circ opac patroncards reports
+    reserve reviews rotating_collections serials services sms suggestion tags
+    tools virtualshelves
+);
+
+# Build files list
+my @files_to_scan;
+foreach my $dir (@directories_to_scan) {
+    opendir DIR, "$installdir/$dir" or die "Unable to open $dir: $!";
+    while (readdir DIR) {
+        next unless ( -f "$installdir/$dir/$_" );
+        next unless ( $_ =~ /(\.pl|\.pm)$/ );
+        push @files_to_scan, "$dir/$_";
+    }
+    closedir DIR;
+}
+
+# Build languages list
+my @langs;
+opendir DIR, $localedir;
+while (readdir DIR) {
+    next unless ( -d "$localedir/$_" );
+    next if ($_ eq '.' or $_ eq '..');
+    push @langs, $_;
+}
+close DIR;
+
+my $xgettext = `which xgettext`;
+chomp $xgettext;
+my $xgettext_cmd = "$xgettext -L Perl --from-code=UTF-8 -kmaketext -o messages.pot";
+$xgettext_cmd .= " -D $installdir/$_" foreach (@directories_to_scan);
+$xgettext_cmd .= " $_" foreach (@files_to_scan);
+
+say "Extracting strings from source...";
+if (system($xgettext_cmd) != 0) {
+    die "system call failed : $xgettext_cmd";
+}
+
+foreach my $lang (@langs) {
+    if ( -f "$localedir/$lang/messages.po" ) {
+        say "Updating $lang...";
+        system("msgmerge -U $localedir/$lang/messages.po messages.pot");
+    } else {
+        say "No PO file for $lang. Creating one...";
+        system("cp messages.pot $localedir/$lang/messages.po");
+    }
+}
+
+unlink "messages.pot";
-- 
1.7.10