From abe032ef32c32d6ad9c734550ab3c3b268351494 Mon Sep 17 00:00:00 2001 From: CarnezEnzo Date: Mon, 17 Feb 2020 15:49:48 +0100 Subject: [PATCH 1/2] Allow to import .po/.xlf files without zip --- Controller/AdminController.php | 3 +- Controller/ImportController.php | 43 ++++++++++++++----- .../default/Translation/translation.html | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Controller/AdminController.php b/Controller/AdminController.php index 2c72c84..74a25c4 100644 --- a/Controller/AdminController.php +++ b/Controller/AdminController.php @@ -22,8 +22,9 @@ public function showPage() { // Is the module active ? $translationInUse = is_dir($this->getTranslationDir()); + $ext = Translation::getConfigValue('extension'); - return $this->render('Translation/translation', ['in_use' => $translationInUse]); + return $this->render('Translation/translation', ['in_use' => $translationInUse, 'ext' => $ext]); } public function setExtensionAction() diff --git a/Controller/ImportController.php b/Controller/ImportController.php index 8504d69..7e392c3 100644 --- a/Controller/ImportController.php +++ b/Controller/ImportController.php @@ -13,9 +13,21 @@ use Thelia\Controller\Admin\BaseAdminController; use Thelia\Tools\URL; use Translation\Translation; +use \RecursiveDirectoryIterator; +use \RecursiveIteratorIterator; class ImportController extends BaseAdminController { + public function getFilePath($dirPath, $fileName) { + $oDirectory = new RecursiveDirectoryIterator($dirPath); + $oIterator = new RecursiveIteratorIterator($oDirectory); + foreach($oIterator as $oFile) { + if ($oFile->getFilename() == $fileName) { + return $oFile->getPath(); + } + } + } + public function importAction() { $path = Translation::TRANSLATIONS_DIR . 'tmp'; @@ -32,20 +44,30 @@ public function importAction() /** @var UploadedFile $importFile */ $importFile = $this->getRequest()->files->get('file'); + if ($importFile) { + $originalExt = $importFile->getClientOriginalExtension(); + $originalName = $importFile->getClientOriginalName(); - $today = new \DateTime(); - $fileName = 'translation-import-' . $today->format('Y-m-d_H-i-s') . '.zip'; + if ($originalExt == 'zip') { - copy($importFile , $path . DS . $fileName); + $today = new \DateTime(); + $fileName = 'translation-import-' . $today->format('Y-m-d_H-i-s') . '.zip'; - $zip = new \ZipArchive(); - $zip->open($path . DS . $fileName); - $zip->extractTo($path); - $zip->close(); + copy($importFile , $path . DS . $fileName); - unlink($path . DS . $fileName); - $this->moveDirectory($path , Translation::TRANSLATIONS_DIR . $ext , $ext); - Translation ::deleteTmp(); + $zip = new \ZipArchive(); + $zip->open($path . DS . $fileName); + $zip->extractTo($path); + $zip->close(); + + unlink($path . DS . $fileName); + $this->moveDirectory($path , Translation::TRANSLATIONS_DIR . $ext , $ext); + Translation ::deleteTmp(); + } else { + $filePath = $this->getFilePath(Translation::TRANSLATIONS_DIR . $ext, $originalName); + copy($importFile, $filePath . DS . $originalName); + } + } return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/translation')); } @@ -87,4 +109,3 @@ protected function mergeDirectory($oldDir , $newDir) } } } - diff --git a/templates/backOffice/default/Translation/translation.html b/templates/backOffice/default/Translation/translation.html index caf8245..2d11f56 100644 --- a/templates/backOffice/default/Translation/translation.html +++ b/templates/backOffice/default/Translation/translation.html @@ -166,7 +166,7 @@
- +
From e84906a75cd415a87175e17ffdaf16687b4d64d7 Mon Sep 17 00:00:00 2001 From: CarnezEnzo Date: Mon, 17 Feb 2020 16:17:12 +0100 Subject: [PATCH 2/2] Handle possible errors --- Controller/AdminController.php | 10 ++++++++-- Controller/ImportController.php | 6 ++++++ I18n/backOffice/default/en_US.php | 2 ++ I18n/backOffice/default/fr_FR.php | 2 ++ .../backOffice/default/Translation/translation.html | 12 ++++++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Controller/AdminController.php b/Controller/AdminController.php index 74a25c4..6da2bca 100644 --- a/Controller/AdminController.php +++ b/Controller/AdminController.php @@ -23,8 +23,14 @@ public function showPage() // Is the module active ? $translationInUse = is_dir($this->getTranslationDir()); $ext = Translation::getConfigValue('extension'); - - return $this->render('Translation/translation', ['in_use' => $translationInUse, 'ext' => $ext]); + $error = $this->getRequest()->get('error'); + + return $this->render('Translation/translation', + [ + 'in_use' => $translationInUse, + 'ext' => $ext, + 'error' => $error + ]); } public function setExtensionAction() diff --git a/Controller/ImportController.php b/Controller/ImportController.php index 7e392c3..ac67198 100644 --- a/Controller/ImportController.php +++ b/Controller/ImportController.php @@ -26,6 +26,7 @@ public function getFilePath($dirPath, $fileName) { return $oFile->getPath(); } } + return false; } public function importAction() @@ -65,9 +66,14 @@ public function importAction() Translation ::deleteTmp(); } else { $filePath = $this->getFilePath(Translation::TRANSLATIONS_DIR . $ext, $originalName); + if (!$filePath) + return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/translation', ['error' => '1'])); copy($importFile, $filePath . DS . $originalName); + } } + else + return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/translation', ['error' => '2'])); return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/translation')); } diff --git a/I18n/backOffice/default/en_US.php b/I18n/backOffice/default/en_US.php index ac39a46..07853b2 100644 --- a/I18n/backOffice/default/en_US.php +++ b/I18n/backOffice/default/en_US.php @@ -19,6 +19,8 @@ instead of Thelia\'s integrated translation system. You can export all the texts to be translated, use an external translation tool to translate them, and import the result that will be used instead of standard Thelia translations.' => 'This module implements a translation system which uses standard format files (PO, Xlif) instead of Thelia\'s integrated translation system. You can export all the texts to be translated, use an external translation tool to translate them, and import the result that will be used instead of standard Thelia translations.', 'Translations' => 'External translations', + 'Please import a valid file.' => 'Please import a valid file.', + 'Failed to update this file, this file doesn\'t seem to exist in this translation system.' => 'Failed to update this file, this file doesn\'t seem to exist in this translation system.', 'You\'re using the external translations. Click the revert button to go back to standard Thelia translation system.' => 'You\'re using the external translations. Click the revert button to go back to standard Thelia translation system.', 'You\'re using the standard Thelia translation system. Please import translations to use the external translations' => 'You\'re using the standard Thelia translation system. Please import translations to use the external translations', 'Zip the folder you have exported before with exactly the same structure and with your translation modification. And upload it here.' => 'Zip the folder you have exported before with exactly the same structure and with your translation modification. And upload it here.', diff --git a/I18n/backOffice/default/fr_FR.php b/I18n/backOffice/default/fr_FR.php index c8193ff..a341381 100644 --- a/I18n/backOffice/default/fr_FR.php +++ b/I18n/backOffice/default/fr_FR.php @@ -19,6 +19,8 @@ instead of Thelia\'s integrated translation system. You can export all the texts to be translated, use an external translation tool to translate them, and import the result that will be used instead of standard Thelia translations.' => 'Ce module permet d\'utiliser un système de traduction qui s\'appuie sur des fichiers respectant un format standard (PO, Xlif) au lieu du système de traduction intégré de Thelia. Vous pouvez ainsi exporter tous les textes à traduire, utiliser des outils de traduction externes pour les traduire, et importer les résultats qui seront pris en compte immédiatement.', 'Translations' => 'Traductions externes', + 'Please import a valid file.' => 'Merci d\'importer un fichier valide.', + 'Failed to update this file, this file doesn\'t seem to exist in this translation system.' => 'Echec de la mise à jour de ce fichier, ce fichier ne semble pas exister dans ce système de traduction.', 'You\'re using the external translations. Click the revert button to go back to standard Thelia translation system.' => 'Vous utilisez le système de traduction externe. Cliquez le bouton "Retour au standard" pour revenir au système de traduction standard de Thelia.', 'You\'re using the standard Thelia translation system. Please import translations to use the external translations' => 'Vous utilisez le système de traduction standard de Thelia. Importez des traductions pour utiliser le système de traduction externe.', 'Zip the folder you have exported before with exactly the same structure and with your translation modification. And upload it here.' => 'Compressez au format Zip le dossier qui contient les fichiers traduits avec exactement la même structure que le fichier exporté, et téléversez-le ici.', diff --git a/templates/backOffice/default/Translation/translation.html b/templates/backOffice/default/Translation/translation.html index 2d11f56..62e34f8 100644 --- a/templates/backOffice/default/Translation/translation.html +++ b/templates/backOffice/default/Translation/translation.html @@ -15,6 +15,18 @@ + {if $error} +
+
+ {if $error == 1} + {intl l="Failed to update this file, this file doesn't seem to exist in this translation system." d="translation.bo.default"} + {else} + {intl l="Please import a valid file." d="translation.bo.default"} + {/if} +
+
+ {/if} +