2121 */
2222class ExcelMessageController extends Controller
2323{
24- public $ defaultAction = 'export ' ;
25-
2624 /**
2725 * @var string Comma separated list of languages to process.
2826 * Default are all languages listed in the messages config file.
@@ -67,6 +65,15 @@ public function options($actionID)
6765 return $ options ;
6866 }
6967
68+ /**
69+ * Show this help message
70+ */
71+ public function actionIndex ()
72+ {
73+ $ helpController = Yii::$ app ->createController ('help ' );
74+ return $ helpController [0 ]->runAction ('index ' , ['excel-message ' ]);
75+ }
76+
7077 /**
7178 * Creates Excel files with translations from PHP message files.
7279 *
@@ -79,48 +86,49 @@ public function options($actionID)
7986 * file.
8087 * @param string $excelDir The path or alias to the output directory for
8188 * the Excel files.
82- * @param string $type The type of messages to include . Either 'new'
89+ * @param string $type The type of messages to export . Either 'new'
8390 * (default) or 'all'.
8491 * @throws Exception on failure.
8592 */
8693 public function actionExport ($ configFile , $ excelDir , $ type = 'new ' )
8794 {
8895 $ config = $ this ->checkArgs ($ configFile , $ excelDir );
89- $ messages = [];
96+ $ export = [];
9097 $ sourceLanguage = Yii::$ app ->language ;
9198 foreach ($ config ['languages ' ] as $ language ) {
9299 if (!$ this ->languageIncluded ($ language )) {
93100 $ this ->stdout ("Skipping language $ language. \n" , Console::FG_YELLOW );
94101 continue ;
95102 }
96103 $ dir = $ config ['messagePath ' ] . DIRECTORY_SEPARATOR . $ language ;
97- foreach (glob ("$ dir/*.php " ) as $ file ) {
104+ $ messageFiles = glob ("$ dir/*.php " );
105+ foreach ($ messageFiles as $ file ) {
98106 $ category = pathinfo ($ file , PATHINFO_FILENAME );
99107 if (!$ this ->categoryIncluded ($ category )) {
100108 $ this ->stdout ("Skipping category $ category. \n" , Console::FG_YELLOW );
101109 continue ;
102110 }
103111 $ this ->stdout ("Reading $ file ... " , Console::FG_GREEN );
104- $ existing = require ($ file );
112+ $ messages = require ($ file );
105113 if ($ type === 'new ' ) {
106- $ existing = array_filter ($ existing , function ($ v ) {
114+ $ messages = array_filter ($ messages , function ($ v ) {
107115 return $ v === '' ;
108116 });
109117 }
110- foreach ($ existing as $ source => $ translation ) {
111- if (!isset ($ messages [$ language ])) {
112- $ messages [$ language ] = [];
118+ foreach ($ messages as $ source => $ translation ) {
119+ if (!isset ($ export [$ language ])) {
120+ $ export [$ language ] = [];
113121 }
114- if (!isset ($ messages [$ language ][$ category ])) {
115- $ messages [$ language ][$ category ] = [];
122+ if (!isset ($ export [$ language ][$ category ])) {
123+ $ export [$ language ][$ category ] = [];
116124 }
117- $ messages [$ language ][$ category ][$ source ] = $ translation ;
125+ $ export [$ language ][$ category ][$ source ] = $ translation ;
118126 }
119127 $ this ->stdout ("Done. \n" , Console::FG_GREEN );
120128 }
121129 }
122- if (count ($ messages ) !== 0 ) {
123- $ this ->writeToExcelFiles ($ messages , $ excelDir );
130+ if (count ($ export ) !== 0 ) {
131+ $ this ->writeToExcelFiles ($ export , $ excelDir );
124132 } else {
125133 $ this ->stdout ("No new translations found \n" , Console::FG_GREEN );
126134 }
@@ -142,19 +150,21 @@ public function actionExport($configFile, $excelDir, $type = 'new')
142150 * @param string $excelDir The path or alias to the input directory for the
143151 * Excel files.
144152 * @param string $extension The Excel file extension. Default is 'xlsx'.
145- * @return void
153+ * @param string $type The type of import. Either 'new' to only add missing
154+ * messages or 'all' (default) to add/update everything.
146155 */
147- public function actionImport ($ configFile , $ excelDir , $ extension = 'xlsx ' )
156+ public function actionImport ($ configFile , $ excelDir , $ extension = 'xlsx ' , $ type = ' all ' )
148157 {
149158 $ config = $ this ->checkArgs ($ configFile , $ excelDir );
150159 $ messages = [];
151- foreach (glob (rtrim ($ excelDir , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*. ' . $ extension ) as $ file ) {
152- $ language = pathinfo ($ file , PATHINFO_FILENAME );
160+ $ excelFiles = glob (rtrim ($ excelDir , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '*. ' . $ extension );
161+ foreach ($ excelFiles as $ excelFile ) {
162+ $ language = pathinfo ($ excelFile , PATHINFO_FILENAME );
153163 if (!$ this ->languageIncluded ($ language )) {
154164 $ this ->stdout ("Skipping language $ language. \n" , Console::FG_YELLOW );
155165 continue ;
156166 }
157- $ excel = PhpspreadsheetIOFactory::load ($ file );
167+ $ excel = PhpspreadsheetIOFactory::load ($ excelFile );
158168 foreach ($ excel ->getSheetNames () as $ category ) {
159169 if (!$ this ->categoryIncluded ($ category )) {
160170 $ this ->stdout ("Skipping category $ category. \n" , Console::FG_YELLOW );
@@ -177,7 +187,8 @@ public function actionImport($configFile, $excelDir, $extension = 'xlsx')
177187 }
178188 }
179189 }
180- $ this ->updateMessageFiles ($ messages , $ config );
190+ $ this ->updateMessageFiles ($ messages , $ config , $ type === 'new ' );
191+ return self ::EXIT_CODE_NORMAL ;
181192 }
182193
183194 /**
@@ -269,8 +280,10 @@ protected function writeToExcelFiles($messages, $excelDir)
269280 *
270281 * @param array $messages
271282 * @param array $config
283+ * @param bool $skipExisting whether to skip messages that already have a
284+ * translation
272285 */
273- protected function updateMessageFiles ($ messages , $ config )
286+ protected function updateMessageFiles ($ messages , $ config, $ skipExisting )
274287 {
275288 foreach ($ messages as $ language => $ categories ) {
276289 $ this ->stdout ("Updating translations for $ language \n" , Console::FG_GREEN );
@@ -286,7 +299,7 @@ protected function updateMessageFiles($messages, $config)
286299 if (!array_key_exists ($ message , $ existingMessages )) {
287300 $ this ->stdout ('Skipping (removed): ' , Console::FG_YELLOW );
288301 $ this ->stdout ($ message . "\n" );
289- } elseif ($ existingMessages [$ message ] !== '' ) {
302+ } elseif ($ existingMessages [$ message ] !== '' && $ skipExisting ) {
290303 $ this ->stdout ('Skipping (exists): ' , Console::FG_YELLOW );
291304 $ this ->stdout ($ message . "\n" );
292305 } else {
0 commit comments