コード例 #1
0
ファイル: Language.php プロジェクト: M03G/PrestaShop
 public static function checkAndAddLanguage($iso_code, $lang_pack = false, $only_add = false, $params_lang = null)
 {
     if (Language::getIdByIso($iso_code)) {
         return true;
     }
     // Initialize the language
     $lang = new Language();
     $lang->iso_code = Tools::strtolower($iso_code);
     $lang->language_code = $iso_code;
     // Rewritten afterwards if the language code is available
     $lang->active = true;
     // If the language pack has not been provided, retrieve it from prestashop.com
     if (!$lang_pack) {
         $lang_pack = self::getLangDetails($iso_code);
     }
     // If a language pack has been found or provided, prefill the language object with the value
     if ($lang_pack) {
         foreach ($lang_pack as $key => $value) {
             if ($key != 'iso_code' && isset(Language::$definition['fields'][$key])) {
                 $lang->{$key} = $value;
             }
         }
     }
     // Use the values given in parameters to override the data retrieved automatically
     if ($params_lang !== null && is_array($params_lang)) {
         foreach ($params_lang as $key => $value) {
             if ($key != 'iso_code' && isset(Language::$definition['fields'][$key])) {
                 $lang->{$key} = $value;
             }
         }
     }
     if (!$lang->name && $lang->iso_code) {
         $lang->name = $lang->iso_code;
     }
     if (!$lang->validateFields() || !$lang->validateFieldsLang() || !$lang->add(true, false, $only_add)) {
         return false;
     }
     if (isset($params_lang['allow_accented_chars_url']) && in_array($params_lang['allow_accented_chars_url'], array('1', 'true'))) {
         Configuration::updateGlobalValue('PS_ALLOW_ACCENTED_CHARS_URL', 1);
     }
     $flag = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/flags/jpeg/' . $iso_code . '.jpg');
     if ($flag != null && !preg_match('/<body>/', $flag)) {
         $file = fopen(_PS_ROOT_DIR_ . '/img/l/' . (int) $lang->id . '.jpg', 'w');
         if ($file) {
             fwrite($file, $flag);
             fclose($file);
         } else {
             Language::_copyNoneFlag((int) $lang->id);
         }
     } else {
         Language::_copyNoneFlag((int) $lang->id);
     }
     $files_copy = array('/en.jpg');
     foreach (ImageType::getAll() as $alias => $config) {
         $files_copy[] = '/en-default-' . ImageType::getFormattedName($alias) . '.jpg';
     }
     foreach (array(_PS_CAT_IMG_DIR_, _PS_MANU_IMG_DIR_, _PS_PROD_IMG_DIR_, _PS_SUPP_IMG_DIR_) as $to) {
         foreach ($files_copy as $file) {
             @copy(_PS_ROOT_DIR_ . '/img/l' . $file, $to . str_replace('/en', '/' . $iso_code, $file));
         }
     }
     return true;
 }