Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of text domain stack prototype

  • Loading branch information...
commit c48c1cfc096a3e9f3323318e5e6c86d2132c8e0c 1 parent f67ec57
@DASPRiD authored
View
35 library/Zend/I18n/Translator/Translator.php
@@ -57,6 +57,13 @@ class Translator
protected $remote = array();
/**
+ * Default text domain to use if none is specified.
+ *
+ * @var string
+ */
+ protected $defaultTextDomain = 'default';
+
+ /**
* Default locale.
*
* @var string
@@ -304,6 +311,28 @@ public function getPluginManager()
}
/**
+ * Set the defualt text domain.
+ *
+ * @param string $textDomain
+ * @return Translator
+ */
+ public function setDefaultTextDomain($textDomain)
+ {
+ $this->defaultTextDomain = $textDomain;
+ return $this;
+ }
+
+ /**
+ * Get the default text domain.
+ *
+ * @return string
+ */
+ public function getDefaultTextDomain()
+ {
+ return $this->defaultTextDomain;
+ }
+
+ /**
* Translate a message.
*
* @param string $message
@@ -311,9 +340,10 @@ public function getPluginManager()
* @param string $locale
* @return string
*/
- public function translate($message, $textDomain = 'default', $locale = null)
+ public function translate($message, $textDomain = null, $locale = null)
{
$locale = ($locale ?: $this->getLocale());
+ $textDomain = ($textDomain ?: $this->getDefaultTextDomain());
$translation = $this->getTranslatedMessage($message, $locale, $textDomain);
if ($translation !== null && $translation !== '') {
@@ -344,10 +374,11 @@ public function translatePlural(
$singular,
$plural,
$number,
- $textDomain = 'default',
+ $textDomain = null,
$locale = null
) {
$locale = $locale ?: $this->getLocale();
+ $textDomain = ($textDomain ?: $this->getDefaultTextDomain());
$translation = $this->getTranslatedMessage($singular, $locale, $textDomain);
if ($translation === null || $translation === '') {
View
225 library/Zend/I18n/View/Helper/TextDomainStack.php
@@ -0,0 +1,225 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_I18n
+ */
+
+namespace Zend\I18n\View\Helper;
+
+use SplStack;
+use Zend\I18n\Exception;
+use Zend\View\Helper\AbstractHelper;
+use Zend\I18n\Translator\Translator;
+use Zend\I18n\Translator\TranslatorAwareInterface;
+
+/**
+ * View helper for keeping trace of used text domains.
+ *
+ * @category Zend
+ * @package Zend_I18n
+ * @subpackage View
+ */
+class TextDomainStack extends AbstractHelper implements TranslatorAwareInterface
+{
+ /**
+ * Stack of text domains.
+ *
+ * @var SplStack
+ */
+ protected $stack;
+
+ /**
+ * Translator instance.
+ *
+ * @var Translator
+ */
+ protected $translator;
+
+ /**
+ * Whether translator should be used.
+ *
+ * @var boolan
+ */
+ protected $translatorEnabled = true;
+
+ /**
+ * Create a new text domain stack.
+ */
+ public function __construct()
+ {
+ $this->stack = new SplStack();
+ }
+
+ /**
+ * setTranslator(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::setTranslator()
+ * @param Translator $translator
+ * @param string $textDomain
+ * @return TextDomainStack
+ */
+ public function setTranslator(Translator $translator = null, $textDomain = null)
+ {
+ $this->translator = $translator;
+
+ if (null !== $textDomain) {
+ $translator->setDefaultTextDomain($textDomain);
+ }
+
+ $this->stack->push($translator->getDefaultTextDomain());
+ return $this;
+ }
+
+ /**
+ * getTranslator(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::getTranslator()
+ * @return Translator|null
+ */
+ public function getTranslator()
+ {
+ if (!$this->isTranslatorEnabled()) {
+ return null;
+ }
+
+ return $this->translator;
+ }
+
+ /**
+ * hasTranslator(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::hasTranslator()
+ * @return boolean
+ */
+ public function hasTranslator()
+ {
+ return (bool) $this->getTranslator();
+ }
+
+ /**
+ * setTranslatorEnabled(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::setTranslatorEnabled()
+ * @param boolean $enabled
+ * @return TextDomainStack
+ */
+ public function setTranslatorEnabled($enabled = true)
+ {
+ $this->translatorEnabled = (bool) $enabled;
+ return $this;
+ }
+
+ /**
+ * isTranslatorEnabled(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::isTranslatorEnabled()
+ * @return bool
+ */
+ public function isTranslatorEnabled()
+ {
+ return $this->translatorEnabled;
+ }
+
+ /**
+ * setTranslatorTextDomain(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::setTranslatorTextDomain()
+ * @param string $textDomain
+ * @return TextDomainStack
+ */
+ public function setTranslatorTextDomain($textDomain = 'default')
+ {
+ $translator = $this->getTranslator();
+
+ if (null === $translator) {
+ throw new Exception\RuntimeException('Translator has not been set');
+ }
+
+ if (count($this->stack)) {
+ $this->stack->pop();
+ }
+
+ $this->stack->push($textDomain);
+ $translator->setDefaultTextDomain($textDomain);
+ return $this;
+ }
+
+ /**
+ * getTranslatorTextDomain(): defined by TranslatorAwareInterface.
+ *
+ * @see TranslatorAwareInterface::getTranslatorTextDomain()
+ *
+ * @return string
+ */
+ public function getTranslatorTextDomain()
+ {
+ $translator = $this->getTranslator();
+
+ if (null === $translator) {
+ throw new Exception\RuntimeException('Translator has not been set');
+ }
+
+ return $translator->getDefaultTextDomain();
+ }
+
+ /**
+ * Push a text domain on the stack.
+ *
+ * @param string|null $textDomain
+ * @return TextDomainStack
+ * @throws Exception\RuntimeException
+ */
+ public function __invoke($textDomain = null)
+ {
+ $translator = $this->getTranslator();
+
+ if (null === $translator) {
+ throw new Exception\RuntimeException('Translator has not been set');
+ } elseif (null !== $textDomain) {
+ $this->pushTextDomain($textDomain);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Push a text domain on the stack.
+ *
+ * @param string $textDomain
+ * @throws Exception\RuntimeException
+ */
+ public function pushTextDomain($textDomain)
+ {
+ $translator = $this->getTranslator();
+
+ if (null === $translator) {
+ throw new Exception\RuntimeException('Translator has not been set');
+ }
+
+ $this->stack->push($textDomain);
+ $translator->setDefaultTextDomain($textDomain);
+ }
+
+ /**
+ * Pop the last text domain.
+ *
+ * @throws Exception\RuntimeException
+ * @return void
+ */
+ public function popTextDomain()
+ {
+ $translator = $this->getTranslator();
+
+ if (null === $translator) {
+ throw new Exception\RuntimeException('Translator has not been set');
+ } elseif (!count($this->stack)) {
+ throw new Exception\RuntimeException('No text domain found in the stack');
+ }
+
+ $translator->setDefaultTextDomain($this->stack->pop());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.