Esempio n. 1
0
 /**
  * Triggers CLDR download
  *
  * @param Event $event
  * @throws \Exception
  * @throws \PrestaShopDatabaseException
  */
 public static function init(Event $event)
 {
     $event->getIO()->write("Init CLDR data download...");
     $root_dir = realpath('');
     $cldr_update = new Update($root_dir . '/translations/');
     $cldr_update->init();
     // If settings file exist
     if (file_exists($root_dir . '/config/settings.inc.php')) {
         //load prestashop config to get locale env
         require $root_dir . '/config/config.inc.php';
         //get each defined languages and fetch cldr datas
         $langs = \DbCore::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang');
         foreach ($langs as $lang) {
             $language_code = explode('-', $lang['language_code']);
             if (empty($language_code[1])) {
                 $language_code[1] = $language_code[0];
             }
             $cldr_update->fetchLocale($language_code['0'] . '-' . strtoupper($language_code[1]));
         }
     }
     $event->getIO()->write("Finished...");
 }
Esempio n. 2
0
 public function loadLocalisationPack($file, $selection, $install_mode = false, $iso_localization_pack = null)
 {
     if (!($xml = @simplexml_load_string($file))) {
         return false;
     }
     libxml_clear_errors();
     $main_attributes = $xml->attributes();
     $this->name = (string) $main_attributes['name'];
     $this->version = (string) $main_attributes['version'];
     if ($iso_localization_pack) {
         $id_country = (int) Country::getByIso($iso_localization_pack);
         if ($id_country) {
             $country = new Country($id_country);
         }
         if (!$id_country || !Validate::isLoadedObject($country)) {
             $this->_errors[] = Tools::displayError(sprintf('Cannot load country : %1d', $id_country));
             return false;
         }
         if (!$country->active) {
             $country->active = 1;
             if (!$country->update()) {
                 $this->_errors[] = Tools::displayError(sprintf('Cannot enable the associated country: %1s', $country->name));
             }
         }
     }
     $res = true;
     if (empty($selection)) {
         $res &= $this->_installStates($xml);
         $res &= $this->_installTaxes($xml);
         $res &= $this->_installCurrencies($xml, $install_mode);
         $res &= $this->installConfiguration($xml);
         $res &= $this->installModules($xml);
         $res &= $this->updateDefaultGroupDisplayMethod($xml);
         if (($res || $install_mode) && isset($this->iso_code_lang)) {
             if (!($id_lang = (int) Language::getIdByIso($this->iso_code_lang, true))) {
                 $id_lang = 1;
             }
             if (!$install_mode) {
                 Configuration::updateValue('PS_LANG_DEFAULT', $id_lang);
             }
         } elseif (!isset($this->iso_code_lang) && $install_mode) {
             $id_lang = 1;
         }
         if (!Language::isInstalled(Language::getIsoById($id_lang))) {
             $res &= $this->_installLanguages($xml, $install_mode);
             $res &= $this->_installUnits($xml);
         }
         if ($install_mode && $res && isset($this->iso_currency)) {
             Cache::clean('Currency::getIdByIsoCode_*');
             $res &= Configuration::updateValue('PS_CURRENCY_DEFAULT', (int) Currency::getIdByIsoCode($this->iso_currency));
             Currency::refreshCurrencies();
         }
     } else {
         foreach ($selection as $selected) {
             // No need to specify the install_mode because if the selection mode is used, then it's not the install
             $res &= Validate::isLocalizationPackSelection($selected) ? $this->{'_install' . $selected}($xml) : 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;
 }
 public function submitAddLang()
 {
     $arr_import_lang = explode('|', Tools::getValue('params_import_language'));
     /* 0 = Language ISO code, 1 = PS version */
     if (Validate::isLangIsoCode($arr_import_lang[0])) {
         $array_stream_context = @stream_context_create(array('http' => array('method' => 'GET', 'timeout' => 10)));
         $content = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/gzip/' . $arr_import_lang[1] . '/' . Tools::strtolower($arr_import_lang[0]) . '.gzip', false, $array_stream_context);
         if ($content) {
             $file = _PS_TRANSLATIONS_DIR_ . $arr_import_lang[0] . '.gzip';
             if ((bool) @file_put_contents($file, $content)) {
                 $gz = new \Archive_Tar($file, true);
                 if (_PS_MODE_DEV_) {
                     $gz->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING);
                 }
                 $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent());
                 if ($error = $gz->extractList(AdminTranslationsController::filesListToPaths($files_list), _PS_TRANSLATIONS_DIR_ . '../')) {
                     if (is_object($error) && !empty($error->message)) {
                         $this->errors[] = $this->trans('The archive cannot be extracted.', array(), 'Admin.International.Notification') . ' ' . $error->message;
                     } else {
                         if (!Language::checkAndAddLanguage($arr_import_lang[0])) {
                             $conf = 20;
                         } else {
                             // Reset cache
                             Language::loadLanguages();
                             // Clear smarty modules cache
                             Tools::clearCache();
                             AdminTranslationsController::checkAndAddMailsFiles($arr_import_lang[0], $files_list);
                             if ($tab_errors = AdminTranslationsController::addNewTabs($arr_import_lang[0], $files_list)) {
                                 $this->errors += $tab_errors;
                             }
                         }
                         if (!unlink($file)) {
                             $this->errors[] = sprintf($this->trans('Cannot delete the archive %s.', array(), 'Admin.International.Notification'), $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] . '-' . Tools::strtoupper($languageCode[1]));
                         $this->redirect(false, isset($conf) ? $conf : '15');
                     }
                 } else {
                     $this->errors[] = sprintf($this->trans('Cannot decompress the translation file for the following language: %s', array(), 'Admin.International.Notification'), $arr_import_lang[0]);
                     $checks = array();
                     foreach ($files_list as $f) {
                         if (isset($f['filename'])) {
                             if (is_file(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename']) && !is_writable(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename'])) {
                                 $checks[] = dirname($f['filename']);
                             } elseif (is_dir(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename']) && !is_writable(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . dirname($f['filename']))) {
                                 $checks[] = dirname($f['filename']);
                             }
                         }
                     }
                     $checks = array_unique($checks);
                     foreach ($checks as $check) {
                         $this->errors[] = sprintf($this->trans('Please check rights for folder and files in %s', array(), 'Admin.Notifications.Error'), $check);
                     }
                     if (!unlink($file)) {
                         $this->errors[] = sprintf($this->trans('Cannot delete the archive %s.', array(), 'Admin.International.Notification'), $file);
                     }
                 }
             } else {
                 $this->errors[] = $this->trans('The server does not have permissions for writing.', array(), 'Admin.Notifications.Error') . ' ' . sprintf($this->trans('Please check rights for %s', array(), 'Admin.Notifications.Error'), dirname($file));
             }
         } else {
             $this->errors[] = $this->trans('Language not found.', array(), 'Admin.International.Notification');
         }
     } else {
         $this->errors[] = $this->trans('Invalid parameter.', array(), 'Admin.Notifications.Error');
     }
 }
Esempio n. 4
0
 /**
  * Install Cldr Datas
  */
 public function installCldrDatas()
 {
     $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
     $cldrUpdate->init();
     //get each defined languages and fetch cldr datas
     $langs = \DbCore::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang');
     foreach ($langs as $lang) {
         $language_code = explode('-', $lang['language_code']);
         if (count($language_code) == 1) {
             $cldrUpdate->fetchLocale($language_code['0']);
         } else {
             $cldrUpdate->fetchLocale($language_code['0'] . '-' . Tools::strtoupper($language_code[1]));
         }
     }
 }