Beispiel #1
0
/**
 * Gets available locales list.
 *
 * Returns a list of locales wich are exist and are enabled in the system. That
 * list is statically cached inside the function.
 *
 * @return string[] List of available locales codes.
 */
function get_available_locales()
{
    static $available_locales = null;

    if (is_null($available_locales)) {
        if (get_maintenance_mode() !== false) {
            // We cannot rely on the database during maintenance, thus we only
            // can use discovered locales as available locales.
            $available_locales = discover_locales();
        } else {
            // Get list of enabled locales from the database.
            $rows = Database::getInstance()->query(
                "SELECT code FROM {locale} WHERE enabled = 1",
                array(),
                array('return_rows' => Database::RETURN_ALL_ROWS)
            );
            $enabled_locales = array();
            foreach ($rows as $row) {
                $enabled_locales[] = $row['code'];
            }

            $fs_locales = discover_locales();

            $available_locales = array_intersect($fs_locales, $enabled_locales);
        }
    }

    return $available_locales;
}
Beispiel #2
0
 /**
  * Import all available locales to the database.
  *
  * @return boolean Indicates if the locales were imported correctly. True if
  *   everything is OK and false otherwise.
  */
 protected function doImportLocales()
 {
     if (!($db = $this->getDatabase())) {
         return false;
     }
     try {
         $rows = $db->query('SELECT code FROM {locale}', null, array('return_rows' => Database::RETURN_ALL_ROWS));
         $exist_locales = array();
         foreach ($rows as $row) {
             $exist_locales[] = $row['code'];
         }
         $fs_locales = discover_locales();
         foreach ($fs_locales as $locale) {
             if (in_array($locale, $exist_locales)) {
                 // Do not create locales twice.
                 continue;
             }
             $db->query('INSERT INTO {locale} (code, enabled) values (:code, :enabled)', array(':code' => $locale, ':enabled' => 0));
         }
     } catch (\Exception $e) {
         $this->errors[] = getlocal('Cannot import locales. Error: {0}', array($e->getMessage()));
         return false;
     }
     return true;
 }
 /**
  * Disables a locale.
  *
  * @param Request $request Incoming request.
  * @return \Symfony\Component\HttpFoundation\Response A response object.
  * @throws NotFoundException If the locale which should be disabled is not
  *   found.
  */
 public function disableAction(Request $request)
 {
     csrf_check_token($request);
     $locale = $request->attributes->get('locale');
     $errors = array();
     // Check if locale exists.
     if (!in_array($locale, discover_locales())) {
         throw new NotFoundException();
     }
     // Disable locale if we can do so.
     $available_locales = get_available_locales();
     if (in_array($locale, $available_locales)) {
         if (count($available_locales) > 1) {
             disable_locale($locale);
         } else {
             $errors[] = getlocal('Cannot disable all locales.');
         }
     }
     if (count($errors) != 0) {
         // Something went wrong. Re-render locales list.
         $request->attributes->set('errors', $errors);
         return $this->indexAction($request);
     }
     return $this->redirect($this->generateUrl('locales'));
 }