## 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. */
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. class ezcTranslationManager
57. {
58. /**
59. * An array containing the filters that should be applies
60. * whenever a translation context is requested.
61. *
62. * @var array(string)
63. */
64. private $filters;
66. /**
67. * The backend in use which is responsible for reading in
68. * the context and returning it to the manager.
69. *
70. * @var ezcTranslationBackendInterface
71. */
72. private $backend;
74. /**
75. * Context cache.
76. *
77. * Two dimension array with the locale and the contextname as indexes.
78. *
79. * @var array(string=>array(string=>ezcTranslation))
80. */
81. private $contextCache;
83. /**
84. * Constructs an ezcTranslationManager object
85. *
86. * This constructor constructs a new ezcTranslationManager object. The only
87. * parameter is a class that implements the ezcTranslationBackendInterface.
88. *
89. * @param ezcTranslationBackend $backend An instance of a translation
90. * backend.
91. */
92. function __construct ( ezcTranslationBackend $backend )
93. {
94. $this->backend = $backend;
95. $this->filters = array();
96. }
98. /**
99. * Adds a filter to the filter list.
100. *
101. * This methods adds the passed filter object to the list of $filters that
102. * will be applied on every context before being returned by getContext().
103. *
104. * @param ezcTranslationFilter $filter
105. */
106. public function addFilter ( ezcTranslationFilter $filter )
107. {
108. $this->filters[] = $filter;
109. }
111. /**
112. * Returns the translation for the context $context with the locale $locale.
113. *
114. * Null is returned if no such context exists.
115. *
116. * @param string $locale
117. * @param string $context
118. * @return ezcTranslation
119. */
120. private function getFromCache( $locale, $context )
121. {
122. if ( isset( $this->contextCache[$locale][$context] ) )
123. {
124. return $this->contextCache[$locale][$context];
125. }
126. return null;
127. }
129. /**
130. * Stores $contextData with the name $contextName and locale $locale
131. * to the context cache
132. *
133. * @param string $locale
134. * @param string $contextName
135. * @param ezcTranslation $contextData
136. * @return void
137. */
138. private function putToCache( $locale, $contextName, ezcTranslation $contextData )
139. {
140. $this->contextCache[$locale][$contextName] = $contextData;
141. }
143. /**
144. * Returns the translations for the $context context and the locale $locale.
145. *
146. * This methods reads the context data from the backend and applies the
147. * filters before returning the translation map as part of the
148. * ezcTranslation object that it returns.
149. *
150. * @param string $locale
151. * @param string $context
152. * @return ezcTranslation
153. */
154. public function getContext ( $locale, $context )
155. {
156. // Try to find the context in the cache
157. if ( ( $translationContext = $this->getFromCache( $locale, $context) ) !== null )
158. {
159. return $translationContext;
160. }
161. // Retrieve the context through the backend and apply filters
162. $translationContext = $this->backend->getContext( $locale, $context );
163. foreach ( $this->filters as $filter )
164. {
165. $filter->runFilter( $translationContext );
166. }
167. // Create the object, put it in the cache and return it
168. $returnContext = new ezcTranslation ( $translationContext );
169. $this->putToCache( $locale, $context, $returnContext );
170. return $returnContext;
171. }
172. }
174. ?>

Documentation generated by phpDocumentor 1.4.3