public function setup() { $config = array('root_locale' => 'en', 'default_locale' => 'te_ST', 'locale_dir' => VARPATH . 'locale/'); $this->i18n = Gallery_I18n::instance($config); db::build()->delete("incoming_translations")->where("locale", "=", "te_ST")->execute(); $messages_te_ST = array(array('Hello world', 'Hallo Welt'), array(array('one' => 'One item has been added', 'other' => '%count elements have been added'), array('one' => 'Ein Element wurde hinzugefuegt.', 'other' => '%count Elemente wurden hinzugefuegt.')), array('Hello %name, how are you today?', 'Hallo %name, wie geht es Dir heute?')); foreach ($messages_te_ST as $data) { list($message, $translation) = $data; $entry = ORM::factory("incoming_translation"); $entry->key = Gallery_I18n::get_message_key($message); $entry->message = serialize($message); $entry->translation = serialize($translation); $entry->locale = 'te_ST'; $entry->revision = null; $entry->save(); } }
static function process_message($message, &$cache) { if (empty($cache)) { foreach (db::build()->select("key")->from("incoming_translations")->where("locale", "=", "root")->execute() as $row) { $cache[$row->key] = true; } } $key = Gallery_I18n::get_message_key($message); if (array_key_exists($key, $cache)) { return $cache[$key]; } $entry = ORM::factory("incoming_translation")->where("key", "=", $key)->find(); if (!$entry->loaded()) { $entry->key = $key; $entry->message = serialize($message); $entry->locale = "root"; $entry->save(); } }
static function set_request_locale() { // 1. Check the session specific preference (cookie) $locale = locales::cookie_locale(); // 2. Check the user's preference if (!$locale) { $locale = identity::active_user()->locale; } // 3. Check the browser's / OS' preference if (!$locale) { $locale = locales::locale_from_http_request(); } // If we have any preference, override the site's default locale if ($locale) { Gallery_I18n::instance()->locale($locale); } }
public static function l10n_form() { if (Input::instance()->get("show_all_l10n_messages")) { $calls = array(); foreach (db::build()->select("key", "message")->from("incoming_translations")->where("locale", "=", "root")->execute() as $row) { $calls[$row->key] = array(unserialize($row->message), array()); } } else { $calls = Gallery_I18n::instance()->call_log(); } $locale = Gallery_I18n::instance()->locale(); if ($calls) { $translations = array(); foreach (db::build()->select("key", "translation")->from("incoming_translations")->where("locale", "=", $locale)->execute() as $row) { $translations[$row->key] = unserialize($row->translation); } // Override incoming with outgoing... foreach (db::build()->select("key", "translation")->from("outgoing_translations")->where("locale", "=", $locale)->execute() as $row) { $translations[$row->key] = unserialize($row->translation); } $string_list = array(); $cache = array(); foreach ($calls as $key => $call) { list($message, $options) = $call; // Ensure that the message is in the DB l10n_scanner::process_message($message, $cache); // Note: Not interpolating placeholders for the actual translation input field. // TODO: Might show a preview w/ interpolations (using $options) $translation = isset($translations[$key]) ? $translations[$key] : ''; $string_list[] = array('source' => $message, 'key' => $key, 'translation' => $translation); } $v = new View('l10n_client.html'); $v->string_list = $string_list; $v->l10n_search_form = self::_l10n_client_search_form(); $v->plural_forms = l10n_client::plural_forms($locale); return $v; } return ''; }
/** * Translates a localizable message with plural forms. * @param $singular String The message to be translated. E.g. "There is one album." * @param $plural String The plural message to be translated. E.g. * "There are %count albums." * @param $count Number The number which is inserted for the %count placeholder and * which is used to select the proper plural form ($singular or $plural). * @param $options array (optional) Options array for key value pairs which are used * for pluralization and interpolation. Special key: "locale" to override the * currently configured locale. * @return String The translated message string. */ function t2($singular, $plural, $count, $options = array()) { return Gallery_I18n::instance()->translate(array("one" => $singular, "other" => $plural), array_merge($options, array("count" => $count))); }
static function update_l10n($task) { $errors = array(); try { $start = microtime(true); $data = Cache::instance()->get("update_l10n_cache:{$task->id}"); if ($data) { list($dirs, $files, $cache, $num_fetched) = unserialize($data); } $i = 0; switch ($task->get("mode", "init")) { case "init": // 0% $dirs = array("gallery", "modules", "themes", "installer"); $files = $cache = array(); $num_fetched = 0; $task->set("mode", "find_files"); $task->status = t("Finding files"); break; case "find_files": // 0% - 10% while (($dir = array_pop($dirs)) && microtime(true) - $start < 0.5) { if (in_array(basename($dir), array("tests", "lib"))) { continue; } foreach (glob(DOCROOT . "{$dir}/*") as $path) { $relative_path = str_replace(DOCROOT, "", $path); if (is_dir($path)) { $dirs[] = $relative_path; } else { $files[] = $relative_path; } } } $task->status = t2("Finding files: found 1 file", "Finding files: found %count files", count($files)); if (!$dirs) { $task->set("mode", "scan_files"); $task->set("total_files", count($files)); $task->status = t("Scanning files"); $task->percent_complete = 10; } break; case "scan_files": // 10% - 70% while (($file = array_pop($files)) && microtime(true) - $start < 0.5) { $file = DOCROOT . $file; switch (pathinfo($file, PATHINFO_EXTENSION)) { case "php": l10n_scanner::scan_php_file($file, $cache); break; case "info": l10n_scanner::scan_info_file($file, $cache); break; } } $total_files = $task->get("total_files"); $task->status = t2("Scanning files: scanned 1 file", "Scanning files: scanned %count files", $total_files - count($files)); $task->percent_complete = 10 + 60 * ($total_files - count($files)) / $total_files; if (empty($files)) { $task->set("mode", "fetch_updates"); $task->status = t("Fetching updates"); $task->percent_complete = 70; } break; case "fetch_updates": // 70% - 100% // Send fetch requests in batches until we're done $num_remaining = l10n_client::fetch_updates($num_fetched); if ($num_remaining) { $total = $num_fetched + $num_remaining; $task->percent_complete = 70 + 30 * ((double) $num_fetched / $total); } else { Gallery_I18n::clear_cache(); $task->done = true; $task->state = "success"; $task->status = t("Translations installed/updated"); $task->percent_complete = 100; } } if (!$task->done) { Cache::instance()->set("update_l10n_cache:{$task->id}", serialize(array($dirs, $files, $cache, $num_fetched))); } else { Cache::instance()->delete("update_l10n_cache:{$task->id}"); } } catch (Exception $e) { Kohana_Log::add("error", (string) $e); $task->done = true; $task->state = "error"; $task->status = $e->getMessage(); $errors[] = (string) $e; } if ($errors) { $task->log($errors); } }