Apache Zeta Components Manual :: File Source for translation_manager.php

Source for file translation_manager.php

Documentation is available at translation_manager.php

  1. <?php
  2. /**
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements.  See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership.  The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License.  You may obtain a copy of the License at
  11.  * 
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  * 
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  *
  21.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22.  * @version //autogentag//
  23.  * @filesource
  24.  * @package Translation
  25.  */
  26.  
  27. /**
  28.  * ezcTranslationManager handles a specific translation file and provides
  29.  * functionality to apply filters and retrieve contexts and translations.
  30.  *
  31.  * The following example shows typical usage:
  32.  * <code>
  33.  * <?php
  34.  * $a = new ezcTranslationTsBackend( 'tests/translations' );
  35.  * $a->setOptions( array ( 'format' => '[LOCALE].xml' ) );
  36.  *
  37.  * $b = new ezcTranslationManager( $a );
  38.  * $b->addFilter( ezcTranslationComplementEmptyFilter::getInstance() );
  39.  * $b->addFilter( ezcTranslationBorkFilter::getInstance() );
  40.  *
  41.  * // Asks the backend for data, runs the attached filter, and creates a
  42.  * // Translation object
  43.  * $tln1 = $b->getContext( 'nl_NL', 'design/admin/content/browse_copy_node' );
  44.  *
  45.  * // Returns the localized string belonging to key "key", the filter has already
  46.  * // been applied to it. Possible parameters can be passed as associative array
  47.  * // as optional second parameter.
  48.  * $string = $tln1->getTranslation( 'Choose location for copy of <%object_name>', array( 'object_name' => 'Foo' ) );
  49.  * ?>
  50.  * </code>
  51.  *
  52.  * @package Translation
  53.  * @version //autogentag//
  54.  * @mainclass
  55.  */
  56. {
  57.     /**
  58.      * An array containing the filters that should be applies
  59.      * whenever a translation context is requested.
  60.      *
  61.      * @var array(string) 
  62.      */
  63.     private $filters;
  64.  
  65.     /**
  66.      * The backend in use which is responsible for reading in
  67.      * the context and returning it to the manager.
  68.      *
  69.      * @var ezcTranslationBackendInterface 
  70.      */
  71.     private $backend;
  72.  
  73.     /**
  74.      * Context cache.
  75.      *
  76.      * Two dimension array with the locale and the contextname as indexes.
  77.      *
  78.      * @var array(string=>array(string=>ezcTranslation)) 
  79.      */
  80.     private $contextCache;
  81.  
  82.     /**
  83.      * Constructs an ezcTranslationManager object
  84.      *
  85.      * This constructor constructs a new ezcTranslationManager object. The only
  86.      * parameter is a class that implements the ezcTranslationBackendInterface.
  87.      *
  88.      * @param ezcTranslationBackend $backend An instance of a translation
  89.      *                                        backend.
  90.      */
  91.     function __constructezcTranslationBackend $backend )
  92.     {
  93.         $this->backend $backend;
  94.         $this->filters array();
  95.     }
  96.  
  97.     /**
  98.      * Adds a filter to the filter list.
  99.      *
  100.      * This methods adds the passed filter object to the list of $filters that
  101.      * will be applied on every context before being returned by getContext().
  102.      *
  103.      * @param ezcTranslationFilter $filter 
  104.      */
  105.     public function addFilterezcTranslationFilter $filter )
  106.     {
  107.         $this->filters[$filter;
  108.     }
  109.  
  110.     /**
  111.      * Returns the translation for the context $context with the locale $locale.
  112.      *
  113.      * Null is returned if no such context exists.
  114.      *
  115.      * @param string $locale 
  116.      * @param string $context 
  117.      * @return ezcTranslation 
  118.      */
  119.     private function getFromCache$locale$context )
  120.     {
  121.         if isset$this->contextCache[$locale][$context) )
  122.         {
  123.             return $this->contextCache[$locale][$context];
  124.         }
  125.         return null;
  126.     }
  127.  
  128.     /**
  129.      * Stores $contextData with the name $contextName and locale $locale
  130.      * to the context cache
  131.      *
  132.      * @param string $locale 
  133.      * @param string $contextName 
  134.      * @param ezcTranslation $contextData 
  135.      * @return void 
  136.      */
  137.     private function putToCache$locale$contextNameezcTranslation $contextData )
  138.     {
  139.         $this->contextCache[$locale][$contextName$contextData;
  140.     }
  141.  
  142.     /**
  143.      * Returns the translations for the $context context and the locale $locale.
  144.      *
  145.      * This methods reads the context data from the backend and applies the
  146.      * filters before returning the translation map as part of the
  147.      * ezcTranslation object that it returns.
  148.      *
  149.      * @param string $locale 
  150.      * @param string $context 
  151.      * @return ezcTranslation 
  152.      */
  153.     public function getContext$locale$context )
  154.     {
  155.         // Try to find the context in the cache
  156.         if ( ( $translationContext $this->getFromCache$locale$context) ) !== null )
  157.         {
  158.             return $translationContext;
  159.         }
  160.         // Retrieve the context through the backend and apply filters
  161.         $translationContext $this->backend->getContext$locale$context );
  162.         foreach $this->filters as $filter )
  163.         {
  164.             $filter->runFilter$translationContext );
  165.         }
  166.         // Create the object, put it in the cache and return it
  167.         $returnContext new ezcTranslation$translationContext );
  168.         $this->putToCache$locale$context$returnContext );
  169.         return $returnContext;
  170.     }
  171. }
  172.  
  173. ?>
Documentation generated by phpDocumentor 1.4.3