From 791abc1c2b0f654f3cbc1a044246210a877ffd66 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 27 Jun 2014 13:17:19 +0000 Subject: [PATCH] Make translation interface play nice with new localization system --- .../Localization/TranslationController.php | 138 ++++++++++-------- src/mibew/libs/routing.yml | 4 +- .../server_side/translation_edit.handlebars | 5 +- .../server_side/translations.handlebars | 23 +-- 4 files changed, 85 insertions(+), 85 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php index 2c9ca536..78a5e238 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Localization/TranslationController.php @@ -17,6 +17,8 @@ namespace Mibew\Controller\Localization; +use Mibew\Database; +use Mibew\Http\Exception\NotFoundException; use Symfony\Component\HttpFoundation\Request; /** @@ -34,24 +36,13 @@ class TranslationController extends AbstractController { $operator = $this->getOperator(); - $source = $request->query->get('source'); - if (!preg_match("/^[\w-]{2,5}$/", $source)) { - $source = DEFAULT_LOCALE; - } - $target = $request->query->get('target'); if (!preg_match("/^[\w-]{2,5}$/", $target)) { $target = CURRENT_LOCALE; } - $lang1 = load_messages($source); - $lang2 = load_messages($target); - $page = array( - 'lang1' => $source, - 'lang2' => $target, - 'title1' => $this->getLocaleName($source), - 'title2' => $this->getLocaleName($target), + 'localeName' => $this->getLocaleName($target), 'errors' => array(), ); @@ -63,53 +54,47 @@ class TranslationController extends AbstractController } // Prepare localization constants to display. - $result = array(); - $all_keys = array_keys($lang1); - foreach ($all_keys as $key) { - $t_source = htmlspecialchars($lang1[$key]); - if (isset($lang2[$key])) { - $value = htmlspecialchars($lang2[$key]); - } else { - $value = "absent"; - } - $result[] = array( - 'id' => $key, - 'l1' => $t_source, - 'l2' => $value, + $strings = $this->loadStrings($target); + foreach ($strings as $key => $item) { + $strings[$key] = array( + 'id' => $item['stringid'], + 'source' => htmlentities($item['source']), + 'translation' => (empty($item['translation']) + ? "absent" + : htmlspecialchars($item['translation'])), ); } // Sort localization constants in the specified order. $order = $request->query->get('sort'); - if (!in_array($order, array('id', 'l1'))) { - $order = 'id'; + if (!in_array($order, array('source', 'translation'))) { + $order = 'source'; } - if ($order == 'id') { + if ($order == 'source') { usort( - $result, + $strings, function ($a, $b) { - return strcmp($a['id'], $b['id']); + return strcmp($a['source'], $b['source']); } ); - } elseif ($order == 'l1') { + } elseif ($order == 'translation') { usort( - $result, + $strings, function ($a, $b) { - return strcmp($a['l1'], $b['l1']); + return strcmp($a['translation'], $b['translation']); } ); } - $pagination = setup_pagination($result, 100); + $pagination = setup_pagination($strings, 100); $page['pagination'] = $pagination['info']; $page['pagination.items'] = $pagination['items']; $page['formtarget'] = $target; - $page['formsource'] = $source; $page['availableLocales'] = $locales_list; $page['availableOrders'] = array( - array('id' => 'id', 'name' => getlocal('Key identifier')), - array('id' => 'l1', 'name' => getlocal('Source language string')), + array('id' => 'source', 'name' => getlocal('Source string')), + array('id' => 'translation', 'name' => getlocal('Translation')), ); $page['formsort'] = $order; $page['title'] = getlocal('Translations'); @@ -132,41 +117,27 @@ class TranslationController extends AbstractController $operator = $this->getOperator(); $string_id = $request->attributes->get('string_id'); - - $source = $request->query->get('source'); - if (!preg_match("/^[\w-]{2,5}$/", $source)) { - $source = DEFAULT_LOCALE; + $string = $this->loadString($string_id); + if (!$string) { + throw new NotFoundException('The string is not found.'); } - $target = $request->query->has('target') - ? $request->query->get('target') - : $request->request->get('target'); - if (!preg_match("/^[\w-]{2,5}$/", $target)) { - $target = CURRENT_LOCALE; - } - - $lang1 = load_messages($source); - $lang2 = load_messages($target); + $target = $string['locale']; $page = array( - 'title1' => $this->getLocaleName($source), - 'title2' => $this->getLocaleName($target), + 'localeName' => $this->getLocaleName($target), // Use errors list stored in the request. We need to do so to have // an ability to pass the request from the "submitEditForm" action. 'errors' => $request->attributes->get('errors', array()), ); - - $translation = isset($lang2[$string_id]) ? $lang2[$string_id] : ''; // Override translation value from the request if needed. - if ($request->request->has('translation')) { - $translation = $request->request->get('translation'); - } + $translation = $request->request->get('translation', $string['translation']); $page['saved'] = false; $page['key'] = $string_id; $page['target'] = $target; - $page['formoriginal'] = isset($lang1[$string_id]) ? $lang1[$string_id] : ''; + $page['formoriginal'] = $string['source']; $page['formtranslation'] = $translation; $page['title'] = getlocal('Translations'); $page = array_merge( @@ -190,14 +161,16 @@ class TranslationController extends AbstractController csrf_check_token($request); $operator = $this->getOperator(); - $string_id = $request->attributes->get('string_id'); $errors = array(); - $target = $request->request->get('target'); - if (!preg_match("/^[\w-]{2,5}$/", $target)) { - $target = CURRENT_LOCALE; + $string_id = $request->attributes->get('string_id'); + $string = $this->loadString($string_id); + if (!$string) { + throw new NotFoundException('The string is not found.'); } + $target = $string['locale']; + $translation = $request->request->get('translation'); if (!$translation) { $errors[] = no_field("Translation"); @@ -210,7 +183,7 @@ class TranslationController extends AbstractController return $this->showEditFormAction($request); } - save_message($target, $string_id, $translation); + save_message($target, $string['source'], $translation); $page['saved'] = true; $page['title'] = getlocal("Translations"); @@ -238,4 +211,43 @@ class TranslationController extends AbstractController return $locale; } + + /** + * Loads all editable translation strings. + * + * @param string $locale Locale code. + * @return array List of translated strings. Each element of this array is + * an associative with the following keys: + * - id: int, ID of the translated string in the database. + * - source: string, english string. + * - translation: string, translated string. + */ + protected function loadStrings($locale) + { + $rows = Database::getInstance()->query( + "SELECT * FROM {translation} WHERE locale = :locale", + array(':locale' => $locale), + array('return_rows' => Database::RETURN_ALL_ROWS) + ); + + return $rows ? $rows : array(); + } + + /** + * Loads translated string from the database by its ID. + * + * @param int $id ID of the translated string in the database. + * @return array|boolean Associative array with string info or boolean false + * it the string is not found. + */ + protected function loadString($id) + { + $string = Database::getInstance()->query( + "SELECT * FROM {translation} WHERE stringid = :id", + array(':id' => $id), + array('return_rows' => Database::RETURN_ONE_ROW) + ); + + return $string ? $string : false; + } } diff --git a/src/mibew/libs/routing.yml b/src/mibew/libs/routing.yml index b81f0d80..b12442c2 100644 --- a/src/mibew/libs/routing.yml +++ b/src/mibew/libs/routing.yml @@ -618,7 +618,7 @@ translation_edit: _access_check: Mibew\AccessControl\Check\PermissionsCheck _access_permissions: [CAN_ADMINISTRATE] requirements: - string_id: "[_\.\w]+" + string_id: "\d{1,10}" methods: [GET] translation_edit_save: @@ -628,7 +628,7 @@ translation_edit_save: _access_check: Mibew\AccessControl\Check\PermissionsCheck _access_permissions: [CAN_ADMINISTRATE] requirements: - string_id: "[_\.\w]+" + string_id: "\d{1,10}" methods: [POST] translations: diff --git a/src/mibew/styles/pages/default/templates_src/server_side/translation_edit.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/translation_edit.handlebars index 710b9de1..49e2a1d6 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/translation_edit.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/translation_edit.handlebars @@ -19,7 +19,6 @@
{{csrfTokenInput}} -
@@ -29,14 +28,14 @@
- +
- +
diff --git a/src/mibew/styles/pages/default/templates_src/server_side/translations.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/translations.handlebars index 15676e4b..a3747b2e 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/translations.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/translations.handlebars @@ -17,13 +17,7 @@
- {{l10n "Direction:"}}
- - => + {{l10n "For language:"}}