Skip to content
Browse files

Started working on the titon\libs\translators\gettext classes

  • Loading branch information...
1 parent a1f1dc6 commit eb6afad5fc5fcb9af71fedfc228c1eabb3ff6b15 @milesj milesj committed Apr 1, 2012
View
2 vendors/titon/core/G11n.php
@@ -222,7 +222,7 @@ public function initialize() {
* @return boolean
*/
public function is($key) {
- $locale = $this->current()->locale();
+ $locale = $this->current()->getLocale();
return ($locale['key'] == $key || $locale['id'] == $key);
}
View
85 vendors/titon/libs/translators/gettext/DomainTranslator.php
@@ -0,0 +1,85 @@
+<?php
+/**
+ * Titon: A PHP 5.4 Modular Framework
+ *
+ * @copyright Copyright 2010, Titon
+ * @link http://github.com/titon
+ * @license http://opensource.org/licenses/bsd-license.php (BSD License)
+ */
+
+namespace titon\libs\translators\gettext;
+
+use titon\Titon;
+use titon\libs\translators\TranslatorAbstract;
+use \Locale;
+
+/**
+ * Translator used for hooking into the GNU gettext library and fetching messages from locale domain files.
+ *
+ * @package titon.libs.translators.gettext
+ */
+class DomainTranslator extends TranslatorAbstract {
+
+ /**
+ * Cached domain lookups.
+ *
+ * @access protected
+ * @var array
+ */
+ protected $_domains = array();
+
+ /**
+ * Bind domain locations if they have not been setup.
+ *
+ * @access public
+ * @param string $module
+ * @param string $catalog
+ * @return void
+ */
+ public function bindDomains($module, $catalog) {
+ $domainKey = $module . '.' . $catalog;
+
+ bind_textdomain_codeset($catalog, Titon::config()->encoding());
+
+ if (isset($this->_domains[$domainKey])) {
+ return;
+ }
+
+ if ($module) {
+ bindtextdomain($catalog, APP_MODULES . $module . '/resources/messages');
+ }
+
+ bindtextdomain($catalog, APP_RESOURCES . 'messages');
+
+ $this->_domains[$domainKey] = true;
+ }
+
+ /**
+ * Get the message from the bound domain.
+ *
+ * @access public
+ * @param string $key
+ * @return string
+ * @throws titon\libs\translators\TranslatorException
+ */
+ public function getMessage($key) {
+ if (isset($this->_cache[$key])) {
+ return $this->_cache[$key];
+ }
+
+ list($module, $catalog, $id) = $this->parseKey($key);
+
+ $this->bindDomains($module, $catalog);
+
+ $message = dgettext($catalog, $id);
+
+ if ($message != $id) {
+ $this->_cache[$key] = $message;
+
+ return $message;
+ }
+
+ throw new TranslatorException(sprintf('Message key %s does not exist in: %s.', $key, Locale::DEFAULT_LOCALE));
+ }
+
+}
View
23 vendors/titon/libs/translators/gettext/GettextTranslator.php
@@ -1,23 +0,0 @@
-<?php
-/**
- * Titon: A PHP 5.4 Modular Framework
- *
- * @copyright Copyright 2010, Titon
- * @link http://github.com/titon
- * @license http://opensource.org/licenses/bsd-license.php (BSD License)
- */
-
-namespace titon\libs\translators\gettext;
-
-use titon\Titon;
-use titon\libs\translators\TranslatorAbstract;
-use titon\libs\translators\TranslatorException;
-
-/**
- * @todo
- *
- * @package titon.libs.translators.gettext
- */
-class GettextTranslator extends TranslatorAbstract {
-
-}
View
37 vendors/titon/libs/translators/gettext/MoTranslator.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * Titon: A PHP 5.4 Modular Framework
+ *
+ * @copyright Copyright 2010, Titon
+ * @link http://github.com/titon
+ * @license http://opensource.org/licenses/bsd-license.php (BSD License)
+ */
+
+namespace titon\libs\translators\gettext;
+
+use titon\libs\bundles\messages\gettext\MoMessageBundle;
+use titon\libs\translators\TranslatorAbstract;
+
+/**
+ * Translator used for parsing GNU gettext MO binary files into an array of translated messages.
+ *
+ * @package titon.libs.translators.gettext
+ */
+class MoTranslator extends TranslatorAbstract {
+
+ /**
+ * Load the correct resource bundle for the associated file type.
+ *
+ * @access public
+ * @param string $module
+ * @param string $locale
+ * @return titon\libs\bundles\Bundle
+ */
+ public function loadBundle($module, $locale) {
+ return new MoMessageBundle(array(
+ 'module' => $module,
+ 'bundle' => $locale
+ ));
+ }
+
+}
View
37 vendors/titon/libs/translators/gettext/PoTranslator.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * Titon: A PHP 5.4 Modular Framework
+ *
+ * @copyright Copyright 2010, Titon
+ * @link http://github.com/titon
+ * @license http://opensource.org/licenses/bsd-license.php (BSD License)
+ */
+
+namespace titon\libs\translators\gettext;
+
+use titon\libs\bundles\messages\gettext\PoMessageBundle;
+use titon\libs\translators\TranslatorAbstract;
+
+/**
+ * Translator used for parsing GNU gettext PO files into an array of translated messages.
+ *
+ * @package titon.libs.translators.gettext
+ */
+class PoTranslator extends TranslatorAbstract {
+
+ /**
+ * Load the correct resource bundle for the associated file type.
+ *
+ * @access public
+ * @param string $module
+ * @param string $locale
+ * @return titon\libs\bundles\Bundle
+ */
+ public function loadBundle($module, $locale) {
+ return new PoMessageBundle(array(
+ 'module' => $module,
+ 'bundle' => $locale
+ ));
+ }
+
+}

0 comments on commit eb6afad

Please sign in to comment.
Something went wrong with that request. Please try again.