From f94dc147cbc7d26d7cd2be2ad8ee16b0d05ff3b9 Mon Sep 17 00:00:00 2001 From: Luc Vandesype Date: Wed, 8 Jul 2015 16:27:22 +0200 Subject: [PATCH] cldr : fetch cldr datas when import new language, fix persmissions error --- Core/Business/Cldr/Repository.php | 10 ++++++- Core/Business/Cldr/Update.php | 30 +++++++++++++------ classes/Currency.php | 13 ++++---- classes/LocalizationPack.php | 14 +++++++++ .../admin/AdminTranslationsController.php | 15 ++++++++++ 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/Core/Business/Cldr/Repository.php b/Core/Business/Cldr/Repository.php index 6e65f25a027b6..ffc6ad2643637 100644 --- a/Core/Business/Cldr/Repository.php +++ b/Core/Business/Cldr/Repository.php @@ -43,16 +43,19 @@ class Repository protected $region; protected $locale; protected $contextLanguage; + protected $oldUmask; public function __construct($contextLanguage = null) { $this->contextLanguage = $contextLanguage; - $this->cldrCacheFolder = _PS_TRANSLATIONS_DIR_.'cldr'; + $this->oldUmask = umask(0000); + if (!is_dir($this->cldrCacheFolder)) { try { mkdir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas', 0777, true); + } catch (\Exception $e) { throw new \Exception('Cldr cache folder can\'t be created'); } @@ -77,6 +80,11 @@ public function __construct($contextLanguage = null) $this->localeRepository = $this->repository->locales[$this->getCulture()]; } + public function __destruct() + { + umask($this->oldUmask); + } + /* * get the current culture */ diff --git a/Core/Business/Cldr/Update.php b/Core/Business/Cldr/Update.php index ba7c31b9a127a..6dbbac2ba5415 100644 --- a/Core/Business/Cldr/Update.php +++ b/Core/Business/Cldr/Update.php @@ -33,8 +33,11 @@ class Update extends Repository { const ZIP_CORE_URL = 'http://www.unicode.org/Public/cldr/26/json-full.zip'; + protected $oldUmask; + public function __construct($psCacheDir) { + $this->oldUmask = umask(0000); $this->cldrCacheFolder = $psCacheDir.'cldr'; if (!is_dir($this->cldrCacheFolder)) { @@ -46,6 +49,11 @@ public function __construct($psCacheDir) } } + public function __destruct() + { + umask($this->oldUmask); + } + /* * Init CLDR datas and download default language */ @@ -71,7 +79,6 @@ public function init() //extract ONLY supplemental json files $archive = new \ZipArchive(); - if ($archive->open($file) === true) { for ($i = 0; $i < $archive->numFiles; $i++) { $filename = $archive->getNameIndex($i); @@ -112,6 +119,7 @@ public function fetchLocale($locale) for ($i = 0; $i < $archive->numFiles; $i++) { $filename = $archive->getNameIndex($i); + if (preg_match('%^main\/'.$locale.'\/(.*).json$%', $filename)) { if (!is_dir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas'.DIRECTORY_SEPARATOR.dirname($filename))) { mkdir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas'.DIRECTORY_SEPARATOR.dirname($filename), 0777, true); @@ -138,10 +146,12 @@ private function generateSupplementalDatas() foreach ($files as $file) { if ($file != '.' && $file != '..') { $newFileName = 'supplemental--'.pathinfo($file)['filename']; - copy( - $rootPath.'supplemental'.DIRECTORY_SEPARATOR.$file, - $this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName - ); + if (!file_exists($this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName)) { + copy( + $rootPath . 'supplemental' . DIRECTORY_SEPARATOR . $file, + $this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName + ); + } } } } @@ -159,10 +169,12 @@ private function generateMainDatas($locale) foreach ($files as $file) { if ($file != '.' && $file != '..') { $newFileName = 'main--'.$locale.'--'.pathinfo($file)['filename']; - copy( - $rootPath.'main'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$file, - $this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName - ); + if (!file_exists($this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName)) { + copy( + $rootPath . 'main' . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $file, + $this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName + ); + } } } } diff --git a/classes/Currency.php b/classes/Currency.php index 32d2bfbbc9b35..c187272657f2b 100644 --- a/classes/Currency.php +++ b/classes/Currency.php @@ -194,16 +194,15 @@ public static function getCurrencies($object = false, $active = true, $group_by $cldr = new Repository(Context::getContext()->language); foreach($tab as $k => $c){ - if($object){ $tab[$k] = Currency::getCurrencyInstance($c['id_currency']); - } + }else{ + $currency = $cldr->getCurrency($c['iso_code']); - $currency = $cldr->getCurrency($c['iso_code']); - - $tab[$k]['name'] = ucfirst($currency['name']); - $tab[$k]['iso_code_num'] = $currency['iso_code']; - $tab[$k]['sign'] = $currency['symbol']; + $tab[$k]['name'] = ucfirst($currency['name']); + $tab[$k]['iso_code_num'] = $currency['iso_code']; + $tab[$k]['sign'] = $currency['symbol']; + } } return $tab; diff --git a/classes/LocalizationPack.php b/classes/LocalizationPack.php index 93d1c644cfc7d..3ada80eda5ff9 100644 --- a/classes/LocalizationPack.php +++ b/classes/LocalizationPack.php @@ -24,6 +24,8 @@ * International Registered Trademark & Property of PrestaShop SA */ +use PrestaShop\PrestaShop\Core\Business\Cldr\Update; + class LocalizationPackCore { public $name; @@ -98,6 +100,18 @@ public function loadLocalisationPack($file, $selection, $install_mode = false, $ } } + //get/update cldr datas for each language + if ($iso_localization_pack) { + foreach ($xml->languages->language as $lang) { + //use this to get correct language code ex : qc become fr + $languageCode = explode('-', Language::getLanguageCodeByIso($lang['iso_code'])); + $isoCode = $languageCode[0].'-'.strtoupper($iso_localization_pack); + + $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_); + $cldrUpdate->fetchLocale($isoCode); + } + } + return $res; } diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index f7694a6838589..7fd9f9aaea951 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -24,6 +24,8 @@ * International Registered Trademark & Property of PrestaShop SA */ +use PrestaShop\PrestaShop\Core\Business\Cldr\Update; + class AdminTranslationsControllerCore extends AdminController { /** Name of theme by default */ @@ -721,6 +723,7 @@ public function submitImportLang() $gz = new Archive_Tar($_FILES['file']['tmp_name'], true); $filename = $_FILES['file']['name']; $iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename); + if (Validate::isLangIsoCode($iso_code)) { $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME)); $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent()); @@ -790,6 +793,13 @@ public function submitImportLang() } } } + + //fetch cldr datas for the new imported locale + $languageCode = explode('-', Language::getLanguageCodeByIso($iso_code)); + $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_); + $cldrUpdate->fetchLocale($languageCode[0].'-'.strtoupper($languageCode[1])); + + $this->redirect(false, (isset($conf) ? $conf : '15')); } } @@ -886,6 +896,11 @@ public function submitAddLang() $this->errors[] = sprintf(Tools::displayError('Cannot delete the archive %s.'), $file); } + //fetch cldr datas for the new imported locale + $languageCode = explode('-', Language::getLanguageCodeByIso($arr_import_lang[0])); + $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_); + $cldrUpdate->fetchLocale($languageCode[0].'-'.strtoupper($languageCode[1])); + $this->redirect(false, (isset($conf) ? $conf : '15')); } } else {