Add Translation package.
authorJan Schneider <jan@horde.org>
Tue, 5 Oct 2010 11:14:56 +0000 (13:14 +0200)
committerJan Schneider <jan@horde.org>
Wed, 13 Oct 2010 00:30:02 +0000 (02:30 +0200)
framework/Translation/lib/Horde/Translation.php [new file with mode: 0644]
framework/Translation/lib/Horde/Translation/Gettext.php [new file with mode: 0644]
framework/Translation/package.xml [new file with mode: 0644]

diff --git a/framework/Translation/lib/Horde/Translation.php b/framework/Translation/lib/Horde/Translation.php
new file mode 100644 (file)
index 0000000..ce76538
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * @package Translation
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ */
+
+/**
+ * The Horde_Translation interface defines the interface for any classes
+ * providing translations.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @package Translation
+ */
+interface Horde_Translation
+{
+    /**
+     * Returns the translation of a message.
+     *
+     * @var string $message  The string to translate.
+     *
+     * @return string  The string translation, or the original string if no
+     *                 translation exists.
+     */
+    public function t($message);
+}
diff --git a/framework/Translation/lib/Horde/Translation/Gettext.php b/framework/Translation/lib/Horde/Translation/Gettext.php
new file mode 100644 (file)
index 0000000..04637b5
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * @package Translation
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ */
+
+/**
+ * The Horde_Translation_Gettext provides translations through the gettext
+ * extension, but fails gracefully if gettext is not installed.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @package Translation
+ */
+class Horde_Translation_Gettext implements Horde_Translation
+{
+    /**
+     * The translation domain, e.g. package name.
+     *
+     * @var string
+     */
+    protected $_domain;
+
+    /**
+     * Whether the gettext extension is installed.
+     *
+     * @var boolean
+     */
+    protected $_gettext;
+
+    /**
+     * Constructor.
+     *
+     * @param string $domain  The translation domain, e.g. package name.
+     * @param string $path    The path to the gettext catalog.
+     */
+    public function __construct($domain, $path)
+    {
+        $this->_gettext = function_exists('_');
+        if (!$this->_gettext) {
+            return;
+        }
+        $this->_domain = $domain;
+        bindtextdomain($this->_domain, $path);
+    }
+
+    /**
+     * Returns the translation of a message.
+     *
+     * @var string $message  The string to translate.
+     *
+     * @return string  The string translation, or the original string if no
+     *                 translation exists.
+     */
+    public function t($message)
+    {
+        return $this->_gettext ? dgettext($this->_domain, $message) : $message;
+    }
+}
diff --git a/framework/Translation/package.xml b/framework/Translation/package.xml
new file mode 100644 (file)
index 0000000..6ab343e
--- /dev/null
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.9.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Translation</name>
+ <channel>pear.horde.org</channel>
+ <summary>Horde translation library</summary>
+ <description>.</description>
+ <lead>
+  <name>Jan Schneider</name>
+  <user>jan</user>
+  <email>jan@horde.org</email>
+  <active>yes</active>
+ </lead>
+ <date>2010-10-05</date>
+ <time>13:12:56</time>
+ <version>
+  <release>0.1.0</release>
+  <api>0.1.0</api>
+ </version>
+ <stability>
+  <release>beta</release>
+  <api>beta</api>
+ </stability>
+ <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
+ <notes>
+* Initial release.
+ </notes>
+ <contents>
+  <dir baseinstalldir="/" name="/">
+   <dir name="lib">
+    <dir name="Horde">
+     <dir name="Translation">
+      <file name="Gettext.php" role="php" />
+     </dir> <!-- //lib/Horde/Translation -->
+     <file name="Translation.php" role="php" />
+    </dir> <!-- //lib/Horde -->
+   </dir> <!-- //lib -->
+  </dir> <!-- / -->
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.2.0</min>
+   </php>
+   <pearinstaller>
+    <min>1.5.0</min>
+   </pearinstaller>
+  </required>
+  <optional>
+   <extension>
+    <name>gettext</name>
+   </extension>
+  </optional>
+ </dependencies>
+ <phprelease>
+  <filelist>
+   <install as="Horde/Translation.php" name="lib/Horde/Translation.php" />
+   <install as="Horde/Translation/Gettext.php" name="lib/Horde/Translation/Gettext.php" />
+  </filelist>
+ </phprelease>
+ <changelog>
+  <release>
+   <version>
+    <release>0.1.0</release>
+    <api>0.1.0</api>
+   </version>
+   <stability>
+    <release>beta</release>
+    <api>beta</api>
+   </stability>
+   <date>2010-10-05</date>
+   <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
+   <notes>
+* Initial release.
+   </notes>
+  </release>
+ </changelog>
+</package>