public function setup() { self::$installed_locales = locales::installed(); self::$default_locale = module::get_var("gallery", "default_locale"); locales::update_installed(array_keys(locales::available())); module::set_var("gallery", "default_locale", "no_NO"); }
static function head($theme) { $session = Session::instance(); $buf = ""; $buf .= $theme->css("gallery.css"); if ($session->get("debug")) { $buf .= $theme->css("debug.css"); } if (module::is_active("rss")) { if ($item = $theme->item()) { if ($item->is_album()) { $buf .= rss::feed_link("gallery/album/{$item->id}"); } else { $buf .= rss::feed_link("gallery/album/{$item->parent()->id}"); } } else { if ($tag = $theme->tag()) { $buf .= rss::feed_link("tag/tag/{$tag->id}"); } } } if (count(locales::installed())) { // Needed by the languages block $buf .= $theme->script("jquery.cookie.js"); } if ($session->get("l10n_mode", false)) { $buf .= $theme->css("l10n_client.css") . $theme->script("jquery.cookie.js") . $theme->script("l10n_client.js"); } $buf .= $theme->css("uploadify/uploadify.css"); return $buf; }
static function head($theme) { if (count(locales::installed())) { // Needed by the languages block $theme->script("jquery.cookie.js"); } return ""; }
private static function _add_locale_dropdown(&$form, $user = null) { $locales = locales::installed(); if (count($locales) > 1) { // Put "none" at the first position in the array $locales = array_merge(array("" => t("« none »")), $locales); $selected_locale = $user && $user->locale ? $user->locale : ""; $form->dropdown("locale")->label(t("Language Preference"))->options($locales)->selected($selected_locale); } }
/** @todo combine with Admin_Users_Controller::_add_locale_dropdown */ private function _add_locale_dropdown(&$form, $user = null) { $locales = locales::installed(); foreach ($locales as $locale => $display_name) { $locales[$locale] = SafeString::of_safe_html($display_name); } if (count($locales) > 1) { // Put "none" at the first position in the array $locales = array_merge(array("" => t("« none »")), $locales); $selected_locale = $user && $user->locale ? $user->locale : ""; $form->dropdown("locale")->label(t("Language Preference"))->options($locales)->selected($selected_locale); } }
static function sidebar_blocks($theme) { $locales = locales::installed(); foreach ($locales as $locale => $display_name) { $locales[$locale] = SafeString::of_safe_html($display_name); } if (count($locales) > 1) { $block = new Block(); $block->css_id = "gUserLanguageBlock"; $block->title = t("Select Language Preference"); $block->content = new View("user_languages_block.html"); $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) user::cookie_locale(); return $block; } }
public function save() { access::verify_csrf(); locales::update_installed($this->input->post("installed_locales")); $installed_locales = array_keys(locales::installed()); $new_default_locale = $this->input->post("default_locale"); if (!in_array($new_default_locale, $installed_locales)) { if (!empty($installed_locales)) { $new_default_locale = $installed_locales[0]; } else { $new_default_locale = "en_US"; } } module::set_var("gallery", "default_locale", $new_default_locale); print json_encode(array("result" => "success")); }
static function get($block_id, $theme) { $block = ""; switch ($block_id) { case "language": $locales = locales::installed(); foreach ($locales as $locale => $display_name) { $locales[$locale] = SafeString::of_safe_html($display_name); } if (count($locales) > 1) { $block = new Block(); $block->css_id = "g-user-language-block"; $block->title = t("Language Preference"); $block->content = new View("user_languages_block.html"); $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) user::cookie_locale(); } break; } return $block; }
static function cookie_locale() { // Can't use Input framework for client side cookies since // they're not signed. $cookie_data = isset($_COOKIE["g_locale"]) ? $_COOKIE["g_locale"] : null; $locale = null; if ($cookie_data) { if (preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)\$/", trim($cookie_data), $matches)) { $requested_locale = $matches[1]; $installed_locales = locales::installed(); if (isset($installed_locales[$requested_locale])) { $locale = $requested_locale; } } } return $locale; }
static function cookie_locale() { $cookie_data = Input::instance()->cookie("g_locale"); $locale = null; if ($cookie_data) { if (preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)\$/", trim($cookie_data), $matches)) { $requested_locale = $matches[1]; $installed_locales = locales::installed(); if (isset($installed_locales[$requested_locale])) { $locale = $requested_locale; } } } return $locale; }
/** * @return an array of messages that will be written to the task log */ static function fetch_updates() { $request->locales = array(); $request->messages = new stdClass(); $locales = locales::installed(); foreach ($locales as $locale => $locale_data) { $request->locales[] = $locale; } // @todo Batch requests (max request size) foreach (Database::instance()->select("key", "locale", "revision", "translation")->from("incoming_translations")->get()->as_array() as $row) { if (!isset($request->messages->{$row->key})) { $request->messages->{$row->key} = 1; } if (!empty($row->revision) && !empty($row->translation)) { if (!is_object($request->messages->{$row->key})) { $request->messages->{$row->key} = new stdClass(); } $request->messages->{$row->key}->{$row->locale} = $row->revision; } } // @todo Include messages from outgoing_translations? $request_data = json_encode($request); $url = self::_server_url() . "?q=translations/fetch"; list($response_data, $response_status) = remote::post($url, array("data" => $request_data)); if (!remote::success($response_status)) { throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED " . $response_status); } if (empty($response_data)) { return array(t("Translations fetch request resulted in an empty response")); } $response = json_decode($response_data); // Response format (JSON payload): // [{key:<key_1>, translation: <JSON encoded translation>, rev:<rev>, locale:<locale>}, // {key:<key_2>, ...} // ] foreach ($response as $message_data) { // @todo Better input validation if (empty($message_data->key) || empty($message_data->translation) || empty($message_data->locale) || empty($message_data->rev)) { throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED: Invalid response data"); } $key = $message_data->key; $locale = $message_data->locale; $revision = $message_data->rev; $translation = json_decode($message_data->translation); if (!is_string($translation)) { // Normalize stdclass to array $translation = (array) $translation; } $translation = serialize($translation); // @todo Should we normalize the incoming_translations table into messages(id, key, message) // and incoming_translations(id, translation, locale, revision)? Or just allow // incoming_translations.message to be NULL? $locale = $message_data->locale; $entry = ORM::factory("incoming_translation")->where(array("key" => $key, "locale" => $locale))->find(); if (!$entry->loaded) { // @todo Load a message key -> message (text) dict into memory outside of this loop $root_entry = ORM::factory("incoming_translation")->where(array("key" => $key, "locale" => "root"))->find(); $entry->key = $key; $entry->message = $root_entry->message; $entry->locale = $locale; } $entry->revision = $revision; $entry->translation = $translation; $entry->save(); } }
private function _languages_form() { $all_locales = locales::available(); $installed_locales = locales::installed(); $form = new Forge("admin/languages/save", "", "post", array("id" => "gLanguageSettingsForm")); $group = $form->group("choose_language")->label(t("Language settings")); $group->dropdown("locale")->options($installed_locales)->selected(module::get_var("gallery", "default_locale"))->label(t("Default language"))->rules('required'); $installation_options = array(); foreach ($all_locales as $code => $display_name) { $installation_options[$code] = array($display_name, isset($installed_locales->{$code})); } $group->checklist("installed_locales")->label(t("Installed Languages"))->options($installation_options)->rules("required"); $group->submit("save")->value(t("Save settings")); return $form; }
/** * Fetches translations for l10n messages. Must be called repeatedly * until 0 is returned (which is a countdown indicating progress). * * @param $num_fetched in/out parameter to specify which batch of * messages to fetch translations for. * @return The number of messages for which we didn't fetch * translations for. */ static function fetch_updates(&$num_fetched) { $request = new stdClass(); $request->locales = array(); $request->messages = new stdClass(); $locales = locales::installed(); foreach ($locales as $locale => $locale_data) { $request->locales[] = $locale; } // See the server side code for how we arrive at this // number as a good limit for #locales * #messages. $max_messages = 2000 / count($locales); $num_messages = 0; $rows = db::build()->select("key", "locale", "revision", "translation")->from("incoming_translations")->order_by("key")->limit(1000000)->offset($num_fetched)->execute(); $num_remaining = $rows->count(); foreach ($rows as $row) { if (!isset($request->messages->{$row->key})) { if ($num_messages >= $max_messages) { break; } $request->messages->{$row->key} = 1; $num_messages++; } if (!empty($row->revision) && !empty($row->translation) && isset($locales[$row->locale])) { if (!is_object($request->messages->{$row->key})) { $request->messages->{$row->key} = new stdClass(); } $request->messages->{$row->key}->{$row->locale} = (int) $row->revision; } $num_fetched++; $num_remaining--; } // @todo Include messages from outgoing_translations? if (!$num_messages) { return $num_remaining; } $request_data = json_encode($request); $url = self::_server_url("fetch"); list($response_data, $response_status) = remote::post($url, array("data" => $request_data)); if (!remote::success($response_status)) { throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED " . $response_status); } if (empty($response_data)) { return $num_remaining; } $response = json_decode($response_data); // Response format (JSON payload): // [{key:<key_1>, translation: <JSON encoded translation>, rev:<rev>, locale:<locale>}, // {key:<key_2>, ...} // ] foreach ($response as $message_data) { // @todo Better input validation if (empty($message_data->key) || empty($message_data->translation) || empty($message_data->locale) || empty($message_data->rev)) { throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED: Invalid response data"); } $key = $message_data->key; $locale = $message_data->locale; $revision = $message_data->rev; $translation = json_decode($message_data->translation); if (!is_string($translation)) { // Normalize stdclass to array $translation = (array) $translation; } $translation = serialize($translation); // @todo Should we normalize the incoming_translations table into messages(id, key, message) // and incoming_translations(id, translation, locale, revision)? Or just allow // incoming_translations.message to be NULL? $locale = $message_data->locale; $entry = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", $locale)->find(); if (!$entry->loaded()) { // @todo Load a message key -> message (text) dict into memory outside of this loop $root_entry = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", "root")->find(); $entry->key = $key; $entry->message = $root_entry->message; $entry->locale = $locale; } $entry->revision = $revision; $entry->translation = $translation; $entry->save(); } return $num_remaining; }
static function get($block_id) { $block = new Block(); switch ($block_id) { case "welcome": $block->css_id = "g-welcome"; $block->title = t("Welcome to Gallery 3"); $block->content = new View("admin_block_welcome.html"); break; case "photo_stream": $block->css_id = "g-photo-stream"; $block->title = t("Photo Stream"); $block->content = new View("admin_block_photo_stream.html"); $block->content->photos = ORM::factory("item")->where("type", "photo")->orderby("created", "DESC")->find_all(10); break; case "log_entries": $block->css_id = "g-log-entries"; $block->title = t("Log Entries"); $block->content = new View("admin_block_log_entries.html"); $block->content->entries = ORM::factory("log")->orderby(array("timestamp" => "DESC", "id" => "DESC"))->find_all(5); break; case "stats": $block->css_id = "g-stats"; $block->title = t("Gallery Stats"); $block->content = new View("admin_block_stats.html"); $block->content->album_count = ORM::factory("item")->where("type", "album")->where("id <>", 1)->count_all(); $block->content->photo_count = ORM::factory("item")->where("type", "photo")->count_all(); break; case "platform_info": $block->css_id = "g-platform"; $block->title = t("Platform Information"); $block->content = new View("admin_block_platform.html"); if (is_readable("/proc/loadavg")) { $block->content->load_average = join(" ", array_slice(split(" ", array_shift(file("/proc/loadavg"))), 0, 3)); } else { $block->content->load_average = t("Unavailable"); } break; case "project_news": $block->css_id = "g-project-news"; $block->title = t("Gallery Project News"); $block->content = new View("admin_block_news.html"); $block->content->feed = feed::parse("http://gallery.menalto.com/node/feed", 3); break; case "block_adder": $block->css_id = "g-block-adder"; $block->title = t("Dashboard Content"); $block->content = self::get_add_block_form(); break; case "language": $locales = locales::installed(); if (count($locales)) { foreach ($locales as $locale => $display_name) { $locales[$locale] = SafeString::of_safe_html($display_name); } $block = new Block(); $block->css_id = "g-user-language-block"; $block->title = t("Language Preference"); $block->content = new View("user_languages_block.html"); $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) locales::cookie_locale(); } else { $block = ""; } break; } return $block; }
static function get($block_id) { $block = new Block(); switch ($block_id) { case "welcome": $block->css_id = "g-welcome"; $block->title = t("Welcome to Gallery 3"); $block->content = new View("admin_block_welcome.html"); break; case "photo_stream": $block->css_id = "g-photo-stream"; $block->title = t("Photo stream"); $block->content = new View("admin_block_photo_stream.html"); $block->content->photos = ORM::factory("item")->where("type", "=", "photo")->order_by("created", "DESC")->find_all(10); break; case "log_entries": $block->css_id = "g-log-entries"; $block->title = t("Log entries"); $block->content = new View("admin_block_log_entries.html"); $block->content->entries = ORM::factory("log")->order_by(array("timestamp" => "DESC", "id" => "DESC"))->find_all(5); break; case "stats": $block->css_id = "g-stats"; $block->title = t("Gallery stats"); $block->content = new View("admin_block_stats.html"); $block->content->album_count = ORM::factory("item")->where("type", "=", "album")->where("id", "<>", 1)->count_all(); $block->content->photo_count = ORM::factory("item")->where("type", "=", "photo")->count_all(); break; case "platform_info": $block->css_id = "g-platform"; $block->title = t("Platform information"); $block->content = new View("admin_block_platform.html"); break; case "project_news": $block->css_id = "g-project-news"; $block->title = t("Gallery project news"); $block->content = new View("admin_block_news.html"); $block->content->feed = feed::parse("http://gallery.menalto.com/node/feed", 3); break; case "block_adder": $block->css_id = "g-block-adder"; $block->title = t("Dashboard content"); $block->content = gallery_block::get_add_block_form(); break; case "language": $locales = locales::installed(); if (count($locales) > 1) { foreach ($locales as $locale => $display_name) { $locales[$locale] = SafeString::of_safe_html($display_name); } $block = new Block(); $block->css_id = "g-user-language-block"; $block->title = t("Language preference"); $block->content = new View("user_languages_block.html"); $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) locales::cookie_locale(); } else { $block = ""; } break; case "upgrade_checker": $block = new Block(); $block->css_id = "g-upgrade-available-block"; $block->title = t("Check for Gallery upgrades"); $block->content = new View("upgrade_checker_block.html"); $block->content->version_info = upgrade_checker::version_info(); $block->content->auto_check_enabled = upgrade_checker::auto_check_enabled(); $block->content->new_version = upgrade_checker::get_upgrade_message(); } return $block; }