/** * Processes submitting of the form which is generated in * {@link \Mibew\Controller\TranslationImportController::showFormAction()} * method. * * @param Request $request Incoming request. * @return string Rendered page content. */ public function submitFormAction(Request $request) { csrf_check_token($request); $errors = array(); $target = $request->request->get('target'); if (!preg_match("/^[\\w-]{2,5}\$/", $target)) { $target = get_current_locale(); } $override = (bool) $request->request->get('override', false); // Validate uploaded file $file = $request->files->get('translation_file'); if ($file) { // Process uploaded file. $orig_filename = $file->getClientOriginalName(); $file_size = $file->getSize(); if ($file_size == 0 || $file_size > Settings::get('max_uploaded_file_size')) { $errors[] = failed_uploading_file($orig_filename, "Uploaded file size exceeded"); } elseif ($file->getClientOriginalExtension() != 'po') { $errors[] = failed_uploading_file($orig_filename, "Invalid file type"); } } else { $errors[] = getlocal("No file selected"); } // Try to process uploaded file if (count($errors) == 0) { try { // Try to import new messages. import_messages($target, $file->getRealPath(), $override); // Remove cached client side translations. $this->getCache()->getItem('translation/js/' . $target)->clear(); // The file is not needed any more. Remove it. unlink($file->getRealPath()); } catch (\Exception $e) { $errors[] = $e->getMessage(); } } if (count($errors) != 0) { $request->attributes->set('errors', $errors); // The form should be rebuild. Invoke appropriate action. return $this->showFormAction($request); } // Redirect the operator to the same page using GET method. $redirect_to = $this->generateUrl('translation_import', array('stored' => true)); return $this->redirect($redirect_to); }
/** * Enables specified locale. * * @param string $locale Locale code according to RFC 5646. * @todo Rewrite the function and move somewhere locale creation and its import. */ function enable_locale($locale) { $db = Database::getInstance(); // Check if the locale exists in the database list($count) = $db->query( "SELECT COUNT(*) FROM {locale} WHERE code = :code", array(':code' => $locale), array( 'return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM, ) ); if ($count == 0) { // The locale does not exist in the database. Create it. $db->query( "INSERT INTO {locale} (code, enabled) VALUES (:code, :enabled)", array( ':code' => $locale, ':enabled' => 1, ) ); // Import localized messages to the just created locale import_messages( $locale, MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po', true ); // Import canned messages for the locale if they exist in the locale's // files. $canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/canned_messages.yml'; if (is_readable($canned_messages_file)) { import_canned_messages($locale, $canned_messages_file); } // Import mail templates for the locale if they exist in the locale's // files. $mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/mail_templates.yml'; if (is_readable($mail_templates_file)) { MailUtils::importTemplates($locale, $mail_templates_file); } } else { // The locale exists in the database. Update it. $db->query( "UPDATE {locale} SET enabled = :enabled WHERE code = :code", array( ':enabled' => 1, ':code' => $locale, ) ); } }
/** * Imports all locale's content (messages, mail templates, configs) to database. * * This function does not create the locale in database so you have to create it * by yourself. * * @param string $locale Code of the locale to import. */ function import_locale_content($locale) { $config = (read_locale_config(MIBEW_FS_ROOT . '/locales/' . $locale . '/config.yml') ?: array()) + array('name' => $locale, 'rtl' => false, 'time_locale' => 'en_US', 'date_format' => array('full' => '%B %d, %Y %I:%M %p', 'date' => '%B %d, %Y', 'time' => '%I:%M %p')); Database::getInstance()->query('UPDATE {locale} SET ' . 'name = :name, rtl = :rtl, time_locale = :time_locale,' . 'date_format = :date_format ' . 'WHERE code = :code', array(':code' => $locale, ':name' => $config['name'], ':rtl' => $config['rtl'] ? 1 : 0, ':time_locale' => $config['time_locale'], ':date_format' => serialize($config['date_format']))); // Import localized messages to the just created locale import_messages($locale, MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po', true); // Import canned messages for the locale if they exist in the locale's // files. $canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/canned_messages.yml'; if (is_readable($canned_messages_file)) { import_canned_messages($locale, $canned_messages_file); } // Import mail templates for the locale if they exist in the locale's // files. $mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/mail_templates.yml'; if (is_readable($mail_templates_file)) { MailUtils::importTemplates($locale, $mail_templates_file); } }
/** * Import locales content, namely translations, canned messages and mail * templates. * * When the content will be imported the locale will be marked as enabled. * @return boolean True if all content was imported successfully and false * otherwise. */ protected function importLocalesContent() { if (!($db = $this->getDatabase())) { return false; } try { $locales = $db->query( 'SELECT * FROM {locale} WHERE enabled = :enabled', array(':enabled' => 0), array('return_rows' => Database::RETURN_ALL_ROWS) ); foreach ($locales as $locale_info) { $locale = $locale_info['code']; // Import localized messages import_messages( $locale, MIBEW_FS_ROOT . '/locales/' . $locale . '/translation.po', true ); // Import canned messages $canned_messages_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/canned_messages.yml'; if (is_readable($canned_messages_file)) { import_canned_messages($locale, $canned_messages_file); } // Import mail templates $mail_templates_file = MIBEW_FS_ROOT . '/locales/' . $locale . '/mail_templates.yml'; if (is_readable($mail_templates_file)) { MailUtils::importTemplates($locale, $mail_templates_file); } // Mark the locale as "enabled" to indicate that all its content // is imported. $db->query( 'UPDATE {locale} SET enabled = :enabled WHERE code = :locale', array( ':locale' => $locale, ':enabled' => 1, ) ); } } catch (\Exception $e) { $this->errors[] = getlocal( 'Cannot import locales content. Error: {0}', array($e->getMessage()) ); return false; } return true; }