Development

HowToGenerateI18NFiles: sfPakeI18n.php

You must first sign up to be able to contribute.

HowToGenerateI18NFiles: sfPakeI18n.php

File sfPakeI18n.php, 6.3 kB (added by benoit, 12 years ago)
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>"');
20   }
21
22   $app    = $args[0];
23
24   define('SF_ROOT_DIR',    sfConfig::get('sf_root_dir'));
25   define('SF_APP',         $app);
26   define('SF_ENVIRONMENT', 'dev');
27   define('SF_DEBUG',       true);
28
29
30   $appDir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP;
31   if ( !is_dir($appDir) )
32   {
33     throw new Exception("unknown application : $appDir");
34   }
35   require_once($appDir.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
36   $context = sfContext::getInstance();
37
38   if ( !$context->getI18n() )
39   {
40     throw new Exception("i18n not set in config/settings.yml");
41   }
42
43   $src_language = $culture = sfConfig::get('sf_i18n_default_culture');
44   if ($src_language == null)
45   {
46     throw new Exception('please set i18n default culture in "config/i18n.yml"');
47   }
48
49   $dest_language = $args[1];
50   $encoding = isset($args[2]) ? $args[2] : 'utf-8'; //TODO: find default encoding ?
51
52   $lang = $context->getI18N()->setCulture($dest_language);
53   $messageSource = $context->getI18N()->getGlobalMessageSource();
54   if ($messageSource != null)
55   {
56     if ($verbose) echo '>> app    '.pakeApp::excerpt('starting i18n scan for "'.$app.'"'.' application')."\n";
57     if ($verbose) echo '>>   default '.pakeApp::excerpt(' language : "'.$src_language).'"'."\n";
58
59     //create dest file if not found
60     if( $messageSource->init($dest_language, $src_language, $encoding) && $verbose)
61     {
62       echo '>>    file created'."\n";
63     }
64
65     //the empty transUnit array
66     $transUnits = array();
67
68     //Scan all the php file for __()
69     $finder = sfFinder::type('file');
70     $finder = $finder->name('*.php');
71     $files = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules');
72     foreach ($files as $aFile) {
73       $transUnits = array_merge($transUnits, _updatePHPMessage($aFile) );
74     }
75
76     //scan for generator files params
77     $finder = sfFinder::type('file');
78     $finder = $finder->name('generator.yml');
79     $files = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules');
80     foreach ($files as $aFile) {
81       $transUnits = array_merge($transUnits,_updateGeneratorMessage($aFile) );
82     }
83
84     //add standard known admin generated syntax
85     //TODO : maybe we can simply scan the sfAdminGenerator classes for __(...)
86     $transUnits[] = 'no results';
87     $transUnits[] = 'actions';
88     $transUnits[] = 'yes or no';
89     $transUnits[] = 'yes';
90     $transUnits[] = 'no';
91     $transUnits[] = 'remove file';
92
93
94     //scan for validator files params
95     $finder = sfFinder::type('dir');
96     $finder = $finder->name('validate');
97     $dirs = $finder->in(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules');
98     foreach ($dirs as $aDir) {
99       $files = sfFinder::type('file')->name('*.yml')->in($aDir);
100       foreach ($files as $aFile)
101       {
102         if ($verbose) echo '>>    val: '.pakeApp::excerpt($aFile)."\n";
103         $transUnits = array_merge ($transUnits, _updateValidatorMessage($aFile) );
104       }
105     }
106
107     //add untranslated messages
108     foreach ($transUnits as $transUnit) {
109       //check if the message exist
110       $messageFormat = $context->getI18N()->getGlobalMessageFormat()->formatExists($transUnit);
111       if (!$messageFormat)
112       {
113         //append if not found
114         $messageSource->append($transUnit);
115         if ($verbose) echo '>>     added '.pakeApp::excerpt(' : '.$transUnit)."\n";
116       }
117     }
118
119     //save all untranslated messages
120     $messageSource->save();
121     } else {
122       throw new Exception("no message source found create at least one 'message.$native_cult.xml' in 'apps/$app/i18n'");
123     }
124
125   }
126
127   /**
128   * @param string the file to scan
129   * @return array transunits found
130   */
131   function _updatePHPMessage($aFile)
132   {
133
134     $verbose = pakeApp::get_instance()->get_verbose();
135
136     //scan file content
137     $fileContent = file_get_contents($aFile);
138     if ($verbose) echo '>>   scan: '.pakeApp::excerpt($aFile)."\n";
139     preg_match_all("/__\([\"''](.*?)[\"'']\)/",$fileContent, $out);
140     return $out[1];
141   }
142
143   /**
144   * @param string the file to scan
145   * @return array transunits found
146   */
147   function _updateGeneratorMessage($aFile)
148   {
149
150     $verbose = pakeApp::get_instance()->get_verbose();
151     $transUnits = array();
152
153     //load and process the YAML file
154     $genConfig = sfYaml::load($aFile);
155     if (isset($genConfig['generator']['param']))
156     {
157       $param = $genConfig['generator']['param'];
158       $labels = array("name", "title");
159       get_array_elems($param, $labels, $transUnits);
160     }
161     return $transUnits;
162   }
163
164   /**
165   * @param string the file to scan
166   * @return array transunits found
167   */
168   function _updateValidatorMessage($aFile)
169   {
170     $verbose = pakeApp::get_instance()->get_verbose();
171     $transUnits = array();
172     //load and process the YAML file
173     $valConfig = sfYaml::load($aFile);
174     $labels = array("msg", "error");
175     get_array_elems($valConfig, $labels, $transUnits);
176     return $transUnits;
177   }
178
179   /**
180    * @param string string to trim
181    * @return string strimed string
182    */
183   function _trimString($string)
184   {
185     return preg_replace("/\\\\'/", '\'', $string);
186   }
187
188
189   ######################################
190   #
191   # Author: Lez Lytollis
192   # (with help from Chris Boget and Andreas Pour)
193   #
194   # A recursive function to traverse a multi-dimensional array
195   # where the dimensions are not known
196   #
197   #######################################
198   function get_array_elems($arrResult, $labels, &$transUnitsFound){
199     while(list($key,$value)=each($arrResult)){
200       if (is_array($value)){
201         get_array_elems($value, $labels, $transUnitsFound);
202       }
203       else {
204         for ($i=0; $i<count($value);$i++){
205           foreach ($labels as $aLabel)
206           {
207             if (strpos($key,$aLabel) !== false)
208             {
209               $transUnitsFound[] = _trimString($value);
210             }
211           }
212         }
213       }
214     }
215   }
216
217   ?>