* setConfiguration( array ( 'location' => 'tests/translations', 'format' => '[LOCALE].xml' ) ); * * $b = new TranslationManager( $a ); * $b->addFilter( 'TranslationFilterFillin' ); * $b->addFilter( 'TranslationFilterBork' ); * * // Asks the backend for data, runs the attached filter, and creates a * // Translation object * $tln1 = $b->getContext( 'nl_NL', 'admin/login' ); * * // Returns the localized string belonging to key "key", the filter has already * // been applied to it. Possible parameters can be passed as associative array * // as optional second parameter. * $string = $tln1->getString( 'key' ); * ?> * * * @package Translation */ class ezcTranslationManager { /** * @var array An array containing the filters that should be applies * whenever a translation context is requested. */ private $filters; /** * @var ezcTranslationBackendInterface The backend in use which is * responsible for reading in the context and returning it to * the manager. */ private $backend; static private $contextCache; /** * Constructs an ezcTranslationManager object * * This constructor constructs a new ezcTranslationManager object. The only * parameter is a class that implements the ezcTranslationBackendInterface. * * @param ezcTranslationBackend $backend An instance of a translation * backend. */ function __construct( ezcTranslationBackend $backend ) { $this->backend = $backend; $this->filters = array(); } /** * Adds a filter to the filter list. * * This methods adds the passed filter object to the list of $filters that * will be applied on every context before being returned by getContext(). * * @param ezcTranslationFilterInterface $filter */ public function addFilter( ezcTranslationFilter $filter ) { $this->filters[] = $filter; } private function getFromCache( $locale, $context ) { if ( isset( self::$contextCache[$locale][$context] ) ) { return self::$contextCache[$locale][$context]; } return null; } private function putToCache( $locale, $contextName, $contextData ) { self::$contextCache[$locale][$contextName] = $contextData; } /** * Returns a translation context's translation map. * * This methods reads the context data from the backend and applies the * filters before returning the translation map as part of the * ezcTranslation object that it returns. * * @param string $locale The locale to return a translator for. * @param string $context The context to return a translator for. * @return ezcTranslation */ public function getContext( $locale, $context ) { // Try to find the context in the cache if ( ( $translationContext = $this->getFromCache( $locale, $context) ) !== null ) { return $translationContext; } // Retrieve the context through the backend and apply filters $translationContext = $this->backend->getContext( $locale, $context ); foreach ( $this->filters as $filter ) { $filter->runFilter( $translationContext ); } // Create the object, put it in the cache and return it $returnContext = new ezcTranslation( $translationContext ); $this->putToCache( $locale, $context, $returnContext ); return $returnContext; } } ?>