From dce6548431c4d61ab6c532375a0f030d8de72fd1 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Mon, 9 Feb 2009 08:42:13 +0000 Subject: Add local localization functionality. Local = no means to upload / download translations to a translation server yet. - Added an outgoing_translations table to store translations from the local translation UI. - I18n class is checking incoming_ and outgoing_translations for translations, giving the latter priority. - Not handling plural strings in the translations UI yet. --- core/controllers/l10n_client.php | 45 ++++++++++++++++++++++++++++++------ core/helpers/core_installer.php | 13 +++++++++++ core/js/l10n_client.js | 2 ++ core/libraries/I18n.php | 14 +++++++++++ core/models/outgoing_translation.php | 21 +++++++++++++++++ core/tests/I18n_Test.php | 1 - 6 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 core/models/outgoing_translation.php (limited to 'core') diff --git a/core/controllers/l10n_client.php b/core/controllers/l10n_client.php index fb008ce0..29648ba4 100644 --- a/core/controllers/l10n_client.php +++ b/core/controllers/l10n_client.php @@ -21,12 +21,44 @@ class L10n_Client_Controller extends Controller { public function save($string) { access::verify_csrf(); + $input = Input::instance(); + $message = $input->post("l10n-message-source"); + $translation = $input->post("l10n-edit-target"); + $key = I18n::getMessageKey($message); + $locale = I18n::instance()->getLocale(); + + $entry = ORM::factory("outgoing_translation") + ->where(array("key" => $key, + "locale" => $locale)) + ->find(); + + if (!$entry->loaded) { + $entry->key = $key; + $entry->locale = $locale; + $entry->message = serialize($message); + $entry->base_revision = null; + } + + $entry->translation = serialize($translation); + + $entry_from_incoming = ORM::factory("incoming_translation") + ->where(array("key" => $key, + "locale" => $locale)) + ->find(); + + if (!$entry_from_incoming->loaded) { + $entry->base_revision = $entry_from_incoming->revision; + } + + $entry->save(); + print json_encode(new stdClass()); } private static function _l10n_client_form() { $form = new Forge("/l10n_client/save", "", "post", array("id" => "gL10nClientSaveForm")); $group = $form->group("l10n_message"); + $group->hidden("l10n-message-source")->value(""); $group->textarea("l10n-edit-target"); $group->submit("l10n-edit-save")->value(t("Save translation")); // TODO(andy_st): Avoiding multiple submit buttons for now (hassle with jQuery form plugin). @@ -53,16 +85,15 @@ class L10n_Client_Controller extends Controller { foreach ($calls as $call) { list ($message, $options) = $call; if (is_array($message)) { - // TODO: Translate each message. If it has a plural form, get - // the current locale's plural rules and all plural translations. - $options['count'] = 1; - $source = $message['one']; - } else { - $source = $message; + // TODO: Handle plural forms. + // Translate each message. If it has a plural form, get + // the current locale's plural rules and all plural translations. + continue; } + $source = $message; $translation = ''; if (I18n::instance()->hasTranslation($message, $options)) { - $translation = I18n::instance()->hasTtranslation($message, $options); + $translation = I18n::instance()->translate($message, $options); } $string_list[] = array('source' => $source, 'translation' => $translation); diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php index 24b9e993..7a83ec8f 100644 --- a/core/helpers/core_installer.php +++ b/core/helpers/core_installer.php @@ -140,6 +140,18 @@ class core_installer { KEY `locale_key` (`locale`, `key`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE `outgoing_translations` ( + `id` int(9) NOT NULL auto_increment, + `key` binary(16) NOT NULL, + `locale` char(10) NOT NULL, + `message` text NOT NULL, + `translation` text, + `base_revision` int(9) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY(`key`, `locale`), + KEY `locale_key` (`locale`, `key`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE `sessions` ( `session_id` varchar(127) NOT NULL, `last_activity` int(10) UNSIGNED NOT NULL, @@ -258,6 +270,7 @@ class core_installer { $db->query("DROP TABLE IF EXISTS `modules`;"); $db->query("DROP TABLE IF EXISTS `themes`;"); $db->query("DROP TABLE IF EXISTS `incoming_translations`;"); + $db->query("DROP TABLE IF EXISTS `outgoing_translations`;"); $db->query("DROP TABLE IF EXISTS `permissions`;"); $db->query("DROP TABLE IF EXISTS `sessions`;"); $db->query("DROP TABLE IF EXISTS `tasks`;"); diff --git a/core/js/l10n_client.js b/core/js/l10n_client.js index 7d2ca5e3..f43671f1 100644 --- a/core/js/l10n_client.js +++ b/core/js/l10n_client.js @@ -128,6 +128,7 @@ Gallery.behaviors.l10nClient = function(context) { var index = $('#l10n-client-string-select li').index(this); $('#l10n-client-string-editor .source-text').text(Gallery.l10nClient.getString(index, 'source')); + $("#gL10nClientSaveForm input[name='l10n-message-source']").val(Gallery.l10nClient.getString(index, 'source')); $('#gL10nClientSaveForm #l10n-edit-target').val(Gallery.l10nClient.getString(index, 'translation')); Gallery.l10nClient.selected = index; @@ -173,6 +174,7 @@ Gallery.behaviors.l10nClient = function(context) { // Empty input fields. $('#l10n-client-string-editor .source-text').html(''); $('#gL10nClientSaveForm #l10n-edit-target').val(''); + $("#gL10nClientSaveForm input[name='l10n-message-source']").val(''); }, error: function(xmlhttp) { // TODO: Localize this message diff --git a/core/libraries/I18n.php b/core/libraries/I18n.php index 51bc2a51..19215325 100644 --- a/core/libraries/I18n.php +++ b/core/libraries/I18n.php @@ -101,6 +101,10 @@ class I18n_Core { return $entry; } + public function getLocale() { + return $this->_config['default_locale']; + } + private function lookup($locale, $message) { if (!isset($this->_cache[$locale])) { $this->_cache[$locale] = array(); @@ -113,6 +117,16 @@ class I18n_Core { ->as_array() as $row) { $this->_cache[$locale][$row->key] = unserialize($row->translation); } + + // Override incoming with outgoing... + foreach (Database::instance() + ->select("key", "translation") + ->from("outgoing_translations") + ->where(array("locale" => $locale)) + ->get() + ->as_array() as $row) { + $this->_cache[$locale][$row->key] = unserialize($row->translation); + } } $key = self::getMessageKey($message); diff --git a/core/models/outgoing_translation.php b/core/models/outgoing_translation.php new file mode 100644 index 00000000..cd5d3267 --- /dev/null +++ b/core/models/outgoing_translation.php @@ -0,0 +1,21 @@ +key = I18n::getMessageKey($message); $entry->message = serialize($message); -- cgit v1.2.3