/** * 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; }