diff --git a/classes/Language.php b/classes/Language.php index a7f8b8ae0..c54d122d0 100644 --- a/classes/Language.php +++ b/classes/Language.php @@ -767,6 +767,48 @@ class LanguageCore extends ObjectModel return self::$countActiveLanguages; } + public static function downloadAndInstallLanguagePack($iso, $version) + { + require_once(_PS_TOOL_DIR_.'tar/Archive_Tar.php'); + + if (!Validate::isLanguageIsoCode($iso)) + return false; + + $lang_pack_ok = false; + $errors = array(); + $file = _PS_TRANSLATIONS_DIR_.$iso.'.gzip'; + if (!$lang_pack_link = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_language_pack.php?version='._PS_VERSION_.'&iso_lang='.$iso)) + $errors[] = Tools::displayError('Archive cannot be downloaded from prestashop.com.'); + elseif (!$lang_pack = Tools::jsonDecode($lang_pack_link)) + $errors[] = Tools::displayError('Error occurred when language was checked according to your Prestashop version.'); + elseif ($content = Tools::file_get_contents('http://translations.prestashop.com/download/lang_packs/gzip/'.$version.'/'.$iso.'.gzip')) + if (!is_writable($file) || !file_put_contents($file, $content)) + $errors[] = Tools::displayError('Server does not have permissions for writing.'); + if (file_exists($file)) + { + $gz = new Archive_Tar($file, true); + $files_list = $gz->listContent(); + if (!$gz->extract(_PS_TRANSLATIONS_DIR_.'../', false)) + { + $errors[] = Tools::displayError('Cannot decompress the translation file for the following language: ').(string)$iso; + return false; + } + else + { + AdminTranslationsController::checkAndAddMailsFiles($iso, $files_list); + AdminTranslationsController::addNewTabs($iso, $files_list); + } + if (!Language::checkAndAddLanguage((string)$iso, $lang_pack)) + { + $errors[] = Tools::displayError('An error occurred while creating the language: ').(string)$iso; + return false; + } + @unlink($file); + } + + return count($errors) ? $errors : true; + } + /** * Check if more on than one language is activated * diff --git a/classes/LocalizationPack.php b/classes/LocalizationPack.php index 02998ed00..442c1f8bd 100644 --- a/classes/LocalizationPack.php +++ b/classes/LocalizationPack.php @@ -296,40 +296,9 @@ class LocalizationPackCore if (Language::getIdByIso($attributes['iso_code']) && !$install_mode) continue; - $errno = 0; - $errstr = ''; - if (!$lang_pack_link = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_language_pack.php?version='._PS_VERSION_.'&iso_lang='.$attributes['iso_code'])) - $this->_errors[] = Tools::displayError('Archive cannot be downloaded from prestashop.com.'); - elseif (!$lang_pack = Tools::jsonDecode($lang_pack_link)) - $this->_errors[] = Tools::displayError('Error occurred when language was checked according to your Prestashop version.'); - elseif ($content = Tools::file_get_contents('http://translations.prestashop.com/download/lang_packs/gzip/'.$lang_pack->version.'/'.$attributes['iso_code'].'.gzip')) - { - $file = _PS_TRANSLATIONS_DIR_.$attributes['iso_code'].'.gzip'; - if (file_put_contents($file, $content)) - { - $gz = new Archive_Tar($file, true); - $files_list = $gz->listContent(); - - if (!$gz->extract(_PS_TRANSLATIONS_DIR_.'../', false)) - { - $this->_errors[] = Tools::displayError('Cannot decompress the translation file for the following language: ').(string)$attributes['iso_code']; - return false; - } - else - { - AdminTranslationsController::checkAndAddMailsFiles($attributes['iso_code'], $files_list); - AdminTranslationsController::addNewTabs($attributes['iso_code'], $files_list); - } - if (!Language::checkAndAddLanguage((string)$attributes['iso_code'], $lang_pack)) - { - $this->_errors[] = Tools::displayError('An error occurred while creating the language: ').(string)$attributes['iso_code']; - return false; - } - @unlink($file); - } - else - $this->_errors[] = Tools::displayError('Server does not have permissions for writing.'); - } + $errors = Language::downloadAndInstallLanguagePack($attributes['iso_code'], $attributes['version']); + if ($errors !== true && is_array($errors)) + $this->_errors = array_merge($this->_errors, $errors); } // change the default language if there is only one language in the localization pack if (!count($this->_errors) && $install_mode && isset($attributes['iso_code']) && count($xml->languages->language) == 1) diff --git a/install-dev/models/install.php b/install-dev/models/install.php index 63d08a8fd..b0d7fa986 100644 --- a/install-dev/models/install.php +++ b/install-dev/models/install.php @@ -307,6 +307,8 @@ class InstallModelInstall extends InstallAbstractModel if (!$xml = @simplexml_load_file(_PS_INSTALL_LANGS_PATH_.$iso.'/language.xml')) throw new PrestashopInstallerException($this->language->l('File "language.xml" not valid for language iso "%s"', $iso)); + Language::downloadAndInstallLanguagePack($iso, _PS_INSTALL_VERSION_); + // Add language in database $language = new Language(); $language->iso_code = $iso;