Permalink
Browse files

Fixes #743: Add a button to transliterate words in the main search view

  • Loading branch information...
Vincent Lequertier authored and TheoChevalier committed Jun 19, 2016
1 parent 65cae28 commit 382ee5f007bac3740a37118263394a51dfde1338
@@ -144,6 +144,21 @@ public static function getTranslationMemoryResults($strings, $search, $max_resul
return $output;
}
+ /**
+ * Use the API to return a transliterated string
+ *
+ * @param string $string The string to be transliterated
+ * @param string $locale The target locale
+ *
+ * @return string The string transliterated into the target locale
+ */
+ public static function getTransliteratedString($string, $locale)
+ {
+ $request = new API(parse_url(API_ROOT . "transliterate/$locale/$string"));
+ $json = include MODELS . 'api/transliterate.php';
+
+ return $json[0];
+ }
/**
* Return search results in a repository on strings/entities for the API
*
@@ -296,22 +311,46 @@ public static function resultsTable($search_object, $search_results, $page)
$target_string2 = '';
}
+ $string_id = md5($key . mt_rand());
+ $regular_string_id = 'string_' . $string_id;
+
+ /*
+ Find if we need to transliterate the string.
+ The string gets transliterated if the target local is serbian,
+ if we aren't in the 3locales view and if we have a $target_string
+ */
+ $transliterate = $locale2 == 'sr' && ! $extra_locale && $target_string && $target_string != '@@missing@@';
+
+ if ($transliterate) {
+ $transliterated_string = self::getTransliteratedString(urlencode($target_string), 'sr-Cyrl');
+ $transliterate_string_id = 'transliterate_' . $string_id;
+ }
+
foreach ($search as $val) {
$source_string = Utils::markString($val, $source_string);
$target_string = Utils::markString($val, $target_string);
if ($extra_locale) {
$target_string2 = Utils::markString($val, $target_string2);
}
+ if ($transliterate) {
+ $transliterated_string = Utils::markString($val, $transliterated_string);
+ }
}
// Escape HTML before highlighing search terms
$source_string = htmlspecialchars($source_string);
$target_string = htmlspecialchars($target_string);
+
$source_string = Utils::highlightString($source_string);
$target_string = Utils::highlightString($target_string);
$source_string = Strings::highlightSpecial($source_string);
$target_string = Strings::highlightSpecial($target_string);
+ if ($transliterate) {
+ $transliterated_string = htmlspecialchars($transliterated_string);
+ $transliterated_string = Utils::highlightString($transliterated_string);
+ }
+
if ($extra_locale) {
$target_string2 = htmlspecialchars($target_string2);
$target_string2 = Utils::highlightString($target_string2);
@@ -362,9 +401,11 @@ public static function resultsTable($search_object, $search_results, $page)
} elseif (! $target_string) {
$target_string = '<em class="error">Warning: Empty string</em>';
} else {
- $meta_target = "
- <span class='clipboard' data-clipboard-target='#{$clipboard_target_string}' alt='Copy to clipboard'></span>
- {$error_message}";
+ $meta_target = "<span class='clipboard' data-clipboard-target='#{$regular_string_id}' alt='Copy to clipboard'></span>";
+ if ($transliterate) {
+ $meta_target .= "<input type='button' value='To Latin' data-transliterated-id='{$string_id}' class='transliterate_button button action'>";
+ }
+ $meta_target .= $error_message;
}
// If there is no target_string2, display an error, otherwise display the string + meta links
@@ -428,7 +469,11 @@ public static function resultsTable($search_object, $search_results, $page)
<td dir='{$direction2}' lang='{$locale2}'>
<span class='celltitle'>{$locale2}</span>
- <div class='string' id='{$clipboard_target_string}'>{$target_string}</div>
+ <div class='string' id='{$regular_string_id}'>{$target_string}</div>";
+ if ($transliterate) {
+ $table .= "<div class='string toggle' id='{$transliterate_string_id}' style='display: none;'>{$transliterated_string}</div>";
+ }
+ $table .= "
<div dir='ltr' class='result_meta_link'>
<a class='source_link' href='{$locale2_path}'>
&lt;source&gt;
View
@@ -17,6 +17,7 @@
define('CACHE_ENABLED', isset($_GET['nocache']) ? false : true);
define('CACHE_PATH', INSTALL_ROOT . 'cache/');
define('APP_SCHEME', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://');
+define('API_ROOT', APP_SCHEME . htmlspecialchars($_SERVER['HTTP_HOST'], ENT_QUOTES, 'UTF-8') . '/api/v1/');
/*
Determine the last Git hash from cache/version.txt, ignoring new lines.
View
@@ -21,6 +21,7 @@
$js_files[] = '/js/component_filter.js';
$js_files[] = '/js/main_search.js';
$js_files[] = '/js/sorttable.js';
+ $js_files[] = '/js/toggle_transliterated_string.js';
$js_files[] = '/assets/jQuery-Autocomplete/dist/jquery.autocomplete.min.js';
break;
case '3locales':
@@ -21,4 +21,4 @@
$transliterator = \Transliterator::create($transliterated_locale);
-return [$transliterator->transliterate(Utils::secureText($request->parameters[3]))];
+return [html_entity_decode($transliterator->transliterate(Utils::secureText(urldecode($request->parameters[3]))))];
@@ -0,0 +1,17 @@
+$(document).ready(function() {
+ $('.transliterate_button').click(function(event) {
+ var id = event.target.getAttribute('data-transliterated-id');
+ var $transliterated = $('#transliterate_' + id);
+ var $normal = $('#string_' + id);
+ event.preventDefault();
+ $normal.toggle();
+ $transliterated.toggle();
+ if ($normal.css('display') === 'none') {
+ $(this).val('To Cyrillic');
+ $('span[data-clipboard-target="#string_'+id+'"]').attr('data-clipboard-target', '#transliterate_' + id);
+ } else {
+ $(this).val('To Latin');
+ $('span[data-clipboard-target="#transliterate_' + id +'"]').attr('data-clipboard-target', '#string_' + id);
+ }
+ });
+});
@@ -1271,7 +1271,6 @@ input[type="checkbox"]:disabled + label {
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"><path style="fill: rgb(0,149,221)" d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>') center no-repeat transparent;
}
-
@media only screen and (max-width: 850px) {
.clipboard img {
position: relative;
@@ -1280,6 +1279,11 @@ input[type="checkbox"]:disabled + label {
}
}
+.transliterate_button {
+ float: right;
+ cursor: pointer;
+}
+
.tooltip {
position: relative;
}

0 comments on commit 382ee5f

Please sign in to comment.