|
From: <gem...@li...> - 2012-12-14 16:27:04
|
Revision: 1064
http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1064&view=rev
Author: matijsdejong
Date: 2012-12-14 16:26:58 +0000 (Fri, 14 Dec 2012)
Log Message:
-----------
TranslateableAbstract.php object for easy (and correct) addition of translation to objects
Added Paths:
-----------
trunk/library/classes/MUtil/Translate/TranslateableAbstract.php
Added: trunk/library/classes/MUtil/Translate/TranslateableAbstract.php
===================================================================
--- trunk/library/classes/MUtil/Translate/TranslateableAbstract.php (rev 0)
+++ trunk/library/classes/MUtil/Translate/TranslateableAbstract.php 2012-12-14 16:26:58 UTC (rev 1064)
@@ -0,0 +1,144 @@
+<?php
+
+/**
+ * Copyright (c) 2012, Erasmus MC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Erasmus MC nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * @package MUtil
+ * @subpackage Translate
+ * @author Matijs de Jong <mj...@ma...>
+ * @copyright Copyright (c) 2012 Erasmus MC
+ * @license New BSD License
+ * @version $id: TranslateableAbstract.php 203 2012-01-01t 12:51:32Z matijs $
+ */
+
+/**
+ * Add auto translate functions to a class
+ *
+ * Can be implemented as Traight in PHP 5.4 or copied into source
+ *
+ * @package MUtil
+ * @subpackage Translate
+ * @copyright Copyright (c) 2012 Erasmus MC
+ * @license New BSD License
+ * @since Class available since version 1.1.35
+ */
+class MUtil_Translate_TranslateableAbstract extends MUtil_Registry_TargetAbstract
+{
+ /**
+ *
+ * @var Zend_Translate
+ */
+ protected $translate;
+
+ /**
+ *
+ * @var Zend_Translate_Adapter
+ */
+ protected $translateAdapter;
+
+ /**
+ * Copy from Zend_Translate_Adapter
+ *
+ * Translates the given string
+ * returns the translation
+ *
+ * @param string $text Translation string
+ * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale
+ * identifier, @see Zend_Locale for more information
+ * @return string
+ */
+ public function _($text, $locale = null)
+ {
+ return $this->translateAdapter->_($text, $locale);
+ }
+
+ /**
+ * Called after the check that all required registry values
+ * have been set correctly has run.
+ *
+ * This function is no needed if the classes are setup correctly
+ *
+ * @return void
+ */
+ public function afterRegistry()
+ {
+ parent::afterRegistry();
+
+ $this->initTranslateable();
+ }
+
+ /**
+ * Function that checks the setup of this class/traight
+ *
+ * This function is not needed if the variables have been defined correctly in the
+ * source for this object and theose variables have been applied.
+ *
+ * return @void
+ */
+ protected function initTranslateable()
+ {
+ if ($this->translateAdapter instanceof Zend_Translate_Adapter) {
+ // OK
+ return;
+ }
+
+ if ($this->translate instanceof Zend_Translate) {
+ // Just one step
+ $this->translateAdapter = $this->translate->getAdapter();
+ return;
+ }
+
+ if ($this->translate instanceof Zend_Translate_Adapter) {
+ // It does happen and if it is all we have
+ $this->translateAdapter = $this->translate;
+ return;
+ }
+
+ // Make sure there always is an adapter, even if it is fake.
+ $this->translateAdapter = new MUtil_Translate_Adapter_Potemkin();
+ }
+
+ /**
+ * Copy from Zend_Translate_Adapter
+ *
+ * Translates the given string using plural notations
+ * Returns the translated string
+ *
+ * @see Zend_Locale
+ * @param string $singular Singular translation string
+ * @param string $plural Plural translation string
+ * @param integer $number Number for detecting the correct plural
+ * @param string|Zend_Locale $locale (Optional) Locale/Language to use, identical with
+ * locale identifier, @see Zend_Locale for more information
+ * @return string
+ */
+ public function plural($singular, $plural, $number, $locale = null)
+ {
+ $args = func_get_args();
+ return call_user_func_array(array($this->translateAdapter, 'plural'), $args);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|