Development

HowToGenerateI18NFiles: sfPakeI18n.php_module

You must first sign up to be able to contribute.

HowToGenerateI18NFiles: sfPakeI18n.php_module

File sfPakeI18n.php_module, 8.2 kB (added by ETS_DIDACTIC_MEDIA(MUNICH), 10 years ago)

Modified sfPakeI18n.php; MessageSources? need to be modified in Order to use with this one (works for already modified XLIFF-MessageSource?).

Line 
1 <?php
2
3 pake_desc('initialize a new language file');
4 pake_task('init-i18n');
5 pake_alias('new', 'init-i18n');
6
7 /**
8 * create a language file scanning __() or i18n flags occurence
9 * @param string task name
10 * @param array task arguments
11 */
12 function run_init_i18n($task, $args)
13 {
14
15   $verbose = pakeApp::get_instance()->get_verbose();
16
17   if (count($args) < 3)
18   {
19     throw new Exception('Syntax is: "init-i18n <application> <dest-language> <encoding> <optional modul(\'all\' for all Modules)>"');
20   }
21
22   $app    = $args[0];
23   $module = isset($args[3]) ? $args[3] : null;
24  
25   // in case module is 'all' do procedure for each module
26   if ($module == 'all'){
27       $allModulesPerModule = true;
28   }
29   else {
30       $allModulesPerModule = false;
31   }
32
33   define('SF_ROOT_DIR',    sfConfig::get('sf_root_dir'));
34   define('SF_APP',         $app);
35   define('SF_ENVIRONMENT', 'dev');
36   define('SF_DEBUG',       true);
37
38
39   $appDir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP;
40   if ( !is_dir($appDir) )
41   {
42     throw new Exception("unknown application : $appDir");
43   }
44   require_once($appDir.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
45   $context = sfContext::getInstance();
46
47   if ( !$context->getI18n() )
48   {
49     throw new Exception("i18n not set in config/settings.yml");
50   }
51
52   $src_language = $culture = sfConfig::get('sf_i18n_default_culture');
53   if ($src_language == null)
54   {
55     throw new Exception('please set i18n default culture in "config/i18n.yml"');
56   }
57
58   $dest_language = $args[1];
59   $encoding = isset($args[2]) ? $args[2] : 'utf-8'; //TODO: find default encoding ?
60
61   $lang = $context->getI18N()->setCulture($dest_language);
62   $messageSource = $context->getI18N()->getGlobalMessageSource();
63   if ($messageSource != null){
64     if ($verbose) echo '>> app    '.pakeApp::excerpt('starting i18n scan for "'.$app.'"'.' application')."\n";
65     if ($verbose) echo '>>   default '.pakeApp::excerpt(' language : "'.$src_language).'"'."\n";
66
67
68     echo "MODULE:".$module."\n";
69
70     $modules = array();
71    
72     //Scan the module file for modules
73     if ($allModulesPerModule) {
74         $finder = sfFinder::type('directory');
75         $finder = $finder->maxdepth('0');
76         $dirs = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules');
77         foreach ($dirs as $dir) {
78             //
79             $splittedDir = explode('/',$dir);
80             $dir = end($splittedDir);
81             $modules[] = $dir;
82         }     
83     }
84     else {
85         $modules[] = $module;
86     }
87
88     foreach ($modules as $module){
89
90         //create dest file if not found
91         if( $messageSource->initModified($dest_language, $src_language, $encoding,
92                                             'messages', $module) && $verbose){
93             echo '>>    file created'."\n";
94         }
95        
96         //the empty transUnit array
97         $transUnits = array();
98    
99         //Scan all the php file for __()
100         $finder = sfFinder::type('file');
101         $finder = $finder->name('*.php');
102         $files = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'
103             .DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules'
104             .DIRECTORY_SEPARATOR.$module);
105         foreach ($files as $aFile) {
106             $transUnits = array_merge($transUnits, _updatePHPMessage($aFile));
107         }
108    
109         //scan for generator files params
110         $finder = sfFinder::type('file');
111         $finder = $finder->name('generator.yml');
112         $files = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'
113             .DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules'
114             .DIRECTORY_SEPARATOR.$module);
115         foreach ($files as $aFile) {
116           $transUnits = array_merge($transUnits,_updateGeneratorMessage($aFile));
117         }
118
119         //only if there is at least one generator file add standard syntax
120         if (count($files) > 0){
121        
122             //add standard known admin generated syntax
123             //TODO : maybe we can simply scan the sfAdminGenerator classes for __(...)
124             $transUnits[] = 'no results';
125             $transUnits[] = 'actions';
126             $transUnits[] = 'yes or no';
127             $transUnits[] = 'yes';
128             $transUnits[] = 'no';
129             $transUnits[] = 'remove file';
130         }
131
132         //scan for validator files params
133         $finder = sfFinder::type('dir');
134         $finder = $finder->name('validate');
135         $dirs = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'
136             .DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules'
137             .DIRECTORY_SEPARATOR.$module);
138         foreach ($dirs as $aDir) {
139             $files = sfFinder::type('file')->name('*.yml')->in($aDir);
140             foreach ($files as $aFile){
141                 if ($verbose) echo '>>    val: '.pakeApp::excerpt($aFile)."\n";
142                 $transUnits = array_merge ($transUnits, _updateValidatorMessage($aFile));
143           }
144         }
145
146         //add untranslated messages
147         foreach ($transUnits as $transUnit) {
148          
149           //check if the message exist
150           $messageFormat = $context->getI18N()->getGlobalMessageFormat()
151               ->formatExists($transUnit);
152           if (!$messageFormat){
153             //append if not found
154             $messageSource->append($transUnit);
155             if ($verbose) {
156                 echo '>>     added '.pakeApp::excerpt(' : '.$transUnit)."\n";
157             }
158           }
159         }
160  
161         if (isset($module)){
162             $modifier = '../modules/'.$module.'/i18n/';
163         $fileName = $messageSource->getSource($modifier.'messages'.
164             '.'.$dest_language.'.xml');
165             echo "FILE TO MODIFY:".$fileName."\n";
166         }
167         else {
168             $fileName = null;
169         }
170        
171         //save all untranslated messages
172         $messageSource->saveModified('messages', $fileName);
173
174     }// end foreach
175          
176   } else {
177       throw new Exception("no message source found create at least one 'message.$native_cult.xml' in 'apps/$app/i18n'");
178   }
179 }
180
181   /**
182   * @param string the file to scan
183   * @return array transunits found
184   */
185   function _updatePHPMessage($aFile)
186   {
187
188     $verbose = pakeApp::get_instance()->get_verbose();
189
190     //scan file content
191     $fileContent = file_get_contents($aFile);
192     if ($verbose) echo '>>   scan: '.pakeApp::excerpt($aFile)."\n";
193     preg_match_all("/__\\(\\s*(['\"])(.+?)\\1(?:\\s*[,\\)])/",$fileContent, $out);
194     return $out[2];
195   }
196
197   /**
198   * @param string the file to scan
199   * @return array transunits found
200   */
201   function _updateGeneratorMessage($aFile)
202   {
203
204     $verbose = pakeApp::get_instance()->get_verbose();
205     $transUnits = array();
206
207     //load and process the YAML file
208     $genConfig = sfYaml::load($aFile);
209     if (isset($genConfig['generator']['param']))
210     {
211       $param = $genConfig['generator']['param'];
212       $labels = array("name", "title");
213       get_array_elems($param, $labels, $transUnits);
214     }
215     return $transUnits;
216   }
217
218   /**
219   * @param string the file to scan
220   * @return array transunits found
221   */
222   function _updateValidatorMessage($aFile)
223   {
224     $verbose = pakeApp::get_instance()->get_verbose();
225     $transUnits = array();
226     //load and process the YAML file
227     $valConfig = sfYaml::load($aFile);
228     $labels = array("msg", "error");
229     get_array_elems($valConfig, $labels, $transUnits);
230     return $transUnits;
231   }
232
233   /**
234    * @param string string to trim
235    * @return string strimed string
236    */
237   function _trimString($string)
238   {
239     return preg_replace("/\\\\'/", '\'', $string);
240   }
241
242
243   ######################################
244   #
245   # Author: Lez Lytollis
246   # (with help from Chris Boget and Andreas Pour)
247   #
248   # A recursive function to traverse a multi-dimensional array
249   # where the dimensions are not known
250   #
251   #######################################
252   function get_array_elems($arrResult, $labels, &$transUnitsFound){
253     while(list($key,$value)=each($arrResult)){
254       if (is_array($value)){
255         get_array_elems($value, $labels, $transUnitsFound);
256       }
257       else {
258         for ($i=0; $i<count($value);$i++){
259           foreach ($labels as $aLabel)
260           {
261             if (strpos($key,$aLabel) !== false)
262             {
263               $transUnitsFound[] = _trimString($value);
264             }
265           }
266         }
267       }
268     }
269   }
270
271   ?>