~joebordes/chive/chive_lib_upgrade

« back to all changes in this revision

Viewing changes to yii/cli/commands/MessageCommand.php

  • Committer: Joe Bordes
  • Date: 2011-08-22 18:24:26 UTC
  • Revision ID: joe@tsolucio.com-20110822182426-zrvpiuyvm20ybjki
Update yii framework 1.1.8 and associated libraries.
Update About page to reflect changes.
Basic testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * MessageCommand class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
/**
 
12
 * MessageCommand extracts messages to be translated from source files.
 
13
 * The extracted messages are saved as PHP message source files
 
14
 * under the specified directory.
 
15
 *
 
16
 * @author Qiang Xue <qiang.xue@gmail.com>
 
17
 * @version $Id: MessageCommand.php 3212 2011-05-12 23:00:19Z alexander.makarow $
 
18
 * @package system.cli.commands
 
19
 * @since 1.0
 
20
 */
 
21
class MessageCommand extends CConsoleCommand
 
22
{
 
23
        public function getHelp()
 
24
        {
 
25
                return <<<EOD
 
26
USAGE
 
27
  yiic message <config-file>
 
28
 
 
29
DESCRIPTION
 
30
  This command searches for messages to be translated in the specified
 
31
  source files and compiles them into PHP arrays as message source.
 
32
 
 
33
PARAMETERS
 
34
 * config-file: required, the path of the configuration file. You can find
 
35
   an example in framework/messages/config.php.
 
36
 
 
37
   The file can be placed anywhere and must be a valid PHP script which
 
38
   returns an array of name-value pairs. Each name-value pair represents
 
39
   a configuration option.
 
40
 
 
41
   The following options are available:
 
42
 
 
43
   - sourcePath: string, root directory of all source files.
 
44
   - messagePath: string, root directory containing message translations.
 
45
   - languages: array, list of language codes that the extracted messages
 
46
     should be translated to. For example, array('zh_cn','en_au').
 
47
   - fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
 
48
     Only the files whose extension name can be found in this list
 
49
     will be processed. If empty, all files will be processed.
 
50
   - exclude: array, a list of directory and file exclusions. Each
 
51
     exclusion can be either a name or a path. If a file or directory name
 
52
     or path matches the exclusion, it will not be copied. For example,
 
53
     an exclusion of '.svn' will exclude all files and directories whose
 
54
     name is '.svn'. And an exclusion of '/a/b' will exclude file or
 
55
     directory 'sourcePath/a/b'.
 
56
   - translator: the name of the function for translating messages.
 
57
     Defaults to 'Yii::t'. This is used as a mark to find messages to be
 
58
     translated.
 
59
   - overwrite: if message file must be overwritten with the merged messages.
 
60
 
 
61
 
 
62
EOD;
 
63
        }
 
64
 
 
65
        /**
 
66
         * Execute the action.
 
67
         * @param array command line parameters specific for this command
 
68
         */
 
69
        public function run($args)
 
70
        {
 
71
                if(!isset($args[0]))
 
72
                        $this->usageError('the configuration file is not specified.');
 
73
                if(!is_file($args[0]))
 
74
                        $this->usageError("the configuration file {$args[0]} does not exist.");
 
75
 
 
76
                $config=require_once($args[0]);
 
77
                $translator='Yii::t';
 
78
                extract($config);
 
79
 
 
80
                if(!isset($sourcePath,$messagePath,$languages))
 
81
                        $this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
 
82
                if(!is_dir($sourcePath))
 
83
                        $this->usageError("The source path $sourcePath is not a valid directory.");
 
84
                if(!is_dir($messagePath))
 
85
                        $this->usageError("The message path $messagePath is not a valid directory.");
 
86
                if(empty($languages))
 
87
                        $this->usageError("Languages cannot be empty.");
 
88
 
 
89
                if(!isset($overwrite))
 
90
                        $overwrite = false;
 
91
 
 
92
                $options=array();
 
93
                if(isset($fileTypes))
 
94
                        $options['fileTypes']=$fileTypes;
 
95
                if(isset($exclude))
 
96
                        $options['exclude']=$exclude;
 
97
                $files=CFileHelper::findFiles(realpath($sourcePath),$options);
 
98
 
 
99
                $messages=array();
 
100
                foreach($files as $file)
 
101
                        $messages=array_merge_recursive($messages,$this->extractMessages($file,$translator));
 
102
 
 
103
                foreach($languages as $language)
 
104
                {
 
105
                        $dir=$messagePath.DIRECTORY_SEPARATOR.$language;
 
106
                        if(!is_dir($dir))
 
107
                                @mkdir($dir);
 
108
                        foreach($messages as $category=>$msgs)
 
109
                        {
 
110
                                $msgs=array_values(array_unique($msgs));
 
111
                                $this->generateMessageFile($msgs,$dir.DIRECTORY_SEPARATOR.$category.'.php',$overwrite);
 
112
                        }
 
113
                }
 
114
        }
 
115
 
 
116
        protected function extractMessages($fileName,$translator)
 
117
        {
 
118
                echo "Extracting messages from $fileName...\n";
 
119
                $subject=file_get_contents($fileName);
 
120
                $n=preg_match_all('/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',$subject,$matches,PREG_SET_ORDER);
 
121
                $messages=array();
 
122
                for($i=0;$i<$n;++$i)
 
123
                {
 
124
                        if(($pos=strpos($matches[$i][1],'.'))!==false)
 
125
                                $category=substr($matches[$i][1],$pos+1,-1);
 
126
                        else
 
127
                                $category=substr($matches[$i][1],1,-1);
 
128
                        $message=$matches[$i][2];
 
129
                        $messages[$category][]=eval("return $message;");  // use eval to eliminate quote escape
 
130
                }
 
131
                return $messages;
 
132
        }
 
133
 
 
134
        protected function generateMessageFile($messages,$fileName,$overwrite)
 
135
        {
 
136
                echo "Saving messages to $fileName...";
 
137
                if(is_file($fileName))
 
138
                {
 
139
                        $translated=require($fileName);
 
140
                        sort($messages);
 
141
                        ksort($translated);
 
142
                        if(array_keys($translated)==$messages)
 
143
                        {
 
144
                                echo "nothing new...skipped.\n";
 
145
                                return;
 
146
                        }
 
147
                        $merged=array();
 
148
                        $untranslated=array();
 
149
                        foreach($messages as $message)
 
150
                        {
 
151
                                if(!empty($translated[$message]))
 
152
                                        $merged[$message]=$translated[$message];
 
153
                                else
 
154
                                        $untranslated[]=$message;
 
155
                        }
 
156
                        ksort($merged);
 
157
                        sort($untranslated);
 
158
                        $todo=array();
 
159
                        foreach($untranslated as $message)
 
160
                                $todo[$message]='';
 
161
                        ksort($translated);
 
162
                        foreach($translated as $message=>$translation)
 
163
                        {
 
164
                                if(!isset($merged[$message]) && !isset($todo[$message]))
 
165
                                        $todo[$message]='@@'.$translation.'@@';
 
166
                        }
 
167
                        $merged=array_merge($todo,$merged);
 
168
                        if($overwrite === false)
 
169
                                $fileName.='.merged';
 
170
                        echo "translation merged.\n";
 
171
                }
 
172
                else
 
173
                {
 
174
                        $merged=array();
 
175
                        foreach($messages as $message)
 
176
                                $merged[$message]='';
 
177
                        ksort($merged);
 
178
                        echo "saved.\n";
 
179
                }
 
180
                $array=str_replace("\r",'',var_export($merged,true));
 
181
                $content=<<<EOD
 
182
<?php
 
183
/**
 
184
 * Message translations.
 
185
 *
 
186
 * This file is automatically generated by 'yiic message' command.
 
187
 * It contains the localizable messages extracted from source code.
 
188
 * You may modify this file by translating the extracted messages.
 
189
 *
 
190
 * Each array element represents the translation (value) of a message (key).
 
191
 * If the value is empty, the message is considered as not translated.
 
192
 * Messages that no longer need translation will have their translations
 
193
 * enclosed between a pair of '@@' marks.
 
194
 *
 
195
 * Message string can be used with plural forms format. Check i18n section
 
196
 * of the guide for details.
 
197
 *
 
198
 * NOTE, this file must be saved in UTF-8 encoding.
 
199
 *
 
200
 * @version \$Id: \$
 
201
 */
 
202
return $array;
 
203
 
 
204
EOD;
 
205
                file_put_contents($fileName, $content);
 
206
        }
 
207
}
 
 
b'\\ No newline at end of file'