Development

/branches/1.3/lib/i18n/sfMessageSource_Aggregate.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/i18n/sfMessageSource_Aggregate.class.php

Revision 13401, 3.9 kB (checked in by fabien, 6 years ago)

[1.1, 1.2] fixed i18n:extract task (closes #4924)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfMessageSource_Aggregate aggregates several message source objects.
13  *
14  * @package    symfony
15  * @subpackage i18n
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfMessageSource_Aggregate extends sfMessageSource
20 {
21   protected
22     $messageSources = array();
23
24   /**
25    * Constructor.
26    *
27    * The order of the messages sources in the array is important.
28    * This class will take the first translation found in the message sources.
29    *
30    * @param array $messageSources An array of message sources.
31    *
32    * @see   MessageSource::factory();
33    */
34   function __construct($messageSources)
35   {
36     $this->messageSources = $messageSources;
37   }
38
39   public function setCulture($culture)
40   {
41     parent::setCulture($culture);
42
43     foreach ($this->messageSources as $messageSource)
44     {
45       $messageSource->setCulture($culture);
46     }
47   }
48
49   protected function getLastModified($sources)
50   {
51     $lastModified = time();
52     foreach ($sources as $source)
53     {
54       $lastModified = min($lastModified, $source[0]->getLastModified($source[1]));
55     }
56
57     return $lastModified;
58   }
59
60   public function isValidSource($sources)
61   {
62     foreach ($sources as $source)
63     {
64       if (false === $source[0]->isValidSource($source[1]))
65       {
66         continue;
67       }
68
69       return true;
70     }
71
72     return false;
73   }
74
75   public function getSource($variant)
76   {
77     $sources = array();
78     foreach ($this->messageSources as $messageSource)
79     {
80       $sources[] = array($messageSource, $messageSource->getSource(str_replace($messageSource->getId(), '', $variant)));
81     }
82
83     return $sources;
84   }
85
86   public function &loadData($sources)
87   {
88     $messages = array();
89     foreach ($sources as $source)
90     {
91       if (false === $source[0]->isValidSource($source[1]))
92       {
93         continue;
94       }
95
96       $data = $source[0]->loadData($source[1]);
97       if (is_array($data))
98       {
99         $messages = array_merge($data, $messages);
100       }
101     }
102
103     return $messages;
104   }
105
106   public function getCatalogueList($catalogue)
107   {
108     $variants = array();
109     foreach ($this->messageSources as $messageSource)
110     {
111       foreach ($messageSource->getCatalogueList($catalogue) as $variant)
112       {
113         $variants[] = $messageSource->getId().$variant;
114       }
115     }
116
117     return $variants;
118   }
119
120   public function append($message)
121   {
122     // Append to the first message source only
123     if (count($this->messageSources))
124     {
125       $this->messageSources[0]->append($message);
126     }
127   }
128
129   public function update($text, $target, $comments, $catalogue = 'messages')
130   {
131     // Only update one message source
132     foreach ($this->messageSources as $messageSource)
133     {
134       if ($messageSource->update($text, $target, $comments, $catalogue))
135       {
136         return true;
137       }
138     }
139
140     return false;
141   }
142
143   public function delete($message, $catalogue = 'messages')
144   {
145     $retval = false;
146     foreach ($this->messageSources as $messageSource)
147     {
148       if ($messageSource->delete($message, $catalogue))
149       {
150         $retval = true;
151       }
152     }
153
154     return $retval;
155   }
156
157   public function save($catalogue = 'messages')
158   {
159     $retval = false;
160     foreach ($this->messageSources as $messageSource)
161     {
162       if ($messageSource->save($catalogue))
163       {
164         $retval = true;
165       }
166     }
167
168     return $retval;
169   }
170
171   public function getId()
172   {
173     $id = '';
174     foreach ($this->messageSources as $messageSource)
175     {
176       $id .= $messageSource->getId();
177     }
178
179     return md5($id);
180   }
181
182   public function catalogues()
183   {
184     throw new sfException('The "catalogues()" method is not implemented for this message source.');
185   }
186 }
187
Note: See TracBrowser for help on using the browser.