From c7fe3a97a9973f4e97bc1e153b87ce40f2ab941b Mon Sep 17 00:00:00 2001 From: Jeroen Dalsem Date: Tue, 5 Dec 2023 08:46:52 +0100 Subject: [PATCH] added: option to exclude searching in translation keys --- languages/en.php | 1 + languages/nl.php | 1 + lib/functions.php | 9 ++++---- .../forms/translation_editor/search.php | 21 +++++++++++++++---- .../resources/translation_editor/search.php | 5 ++++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/languages/en.php b/languages/en.php index 52eae66..3e14df1 100644 --- a/languages/en.php +++ b/languages/en.php @@ -75,6 +75,7 @@ // search 'translation_editor:search' => "Search results for '%s' in %s", 'translation_editor:forms:search:default' => "Find a translation", + 'translation_editor:forms:search:keys' => "Search in translations keys", 'translation_editor:search_results:no_results' => "No translation found", 'translation_editor:show_original' => "Show original translation", diff --git a/languages/nl.php b/languages/nl.php index ff433d5..efecf37 100644 --- a/languages/nl.php +++ b/languages/nl.php @@ -67,6 +67,7 @@ 'translation_editor:gatekeeper' => 'Je bent geen geautoriseerde vertaler', 'translation_editor:language' => 'Taal', 'translation_editor:forms:search:default' => 'Zoek een vertaling', + 'translation_editor:forms:search:keys' => 'Doorzoek de taalsleutels', 'translation_editor:search_results:no_results' => 'Geen vertalingen gevonden', 'translation_editor:show_original' => 'Toon originele vertaling', 'translation_editor:action:translate:error:input' => 'Onjuiste invoer voor het toevoegen van een vertaling', diff --git a/lib/functions.php b/lib/functions.php index 29da34e..c75e598 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -475,12 +475,13 @@ function translation_editor_is_translation_editor(int $user_guid = 0): bool { /** * Search for a translation * - * @param string $query the text to search for - * @param string $language the language to search in (defaults to English) + * @param string $query the text to search for + * @param string $language the language to search in (defaults to English) + * @param bool $search_keys should we search in the keys * * @return array|null */ -function translation_editor_search_translation(string $query, string $language = 'en'): ?array { +function translation_editor_search_translation(string $query, string $language = 'en', bool $search_keys = true): ?array { $plugins = translation_editor_get_plugins($language); if (empty($plugins)) { return null; @@ -494,7 +495,7 @@ function translation_editor_search_translation(string $query, string $language = } foreach ($translations['en'] as $key => $value) { - if (stristr($key, $query) || stristr($value, $query) || (array_key_exists($key, $translations['current_language']) && stristr($translations['current_language'][$key], $query))) { + if (($search_keys && stristr($key, $query)) || stristr($value, $query) || (array_key_exists($key, $translations['current_language']) && stristr($translations['current_language'][$key], $query))) { if (!array_key_exists($plugin, $found)) { $found[$plugin] = [ 'en' => [], diff --git a/views/default/forms/translation_editor/search.php b/views/default/forms/translation_editor/search.php index f438702..e386e18 100644 --- a/views/default/forms/translation_editor/search.php +++ b/views/default/forms/translation_editor/search.php @@ -13,11 +13,24 @@ '#type' => 'fieldset', 'fields' => [ [ - '#type' => 'text', + '#type' => 'fieldset', '#class' => 'elgg-field-stretch', - 'name' => 'q', - 'value' => elgg_extract('query', $vars), - 'placeholder' => elgg_echo('translation_editor:forms:search:default'), + 'fields' => [ + [ + '#type' => 'text', + 'name' => 'q', + 'value' => elgg_extract('query', $vars), + 'placeholder' => elgg_echo('translation_editor:forms:search:default'), + ], + [ + '#type' => 'checkbox', + '#label' => elgg_echo('translation_editor:forms:search:keys'), + 'name' => 'search_keys', + 'value' => 1, + 'checked' => (bool) elgg_extract('search_keys', $vars, true), + 'switch' => true, + ], + ], ], [ '#type' => 'submit', diff --git a/views/default/resources/translation_editor/search.php b/views/default/resources/translation_editor/search.php index 5ac98b5..b9baa03 100644 --- a/views/default/resources/translation_editor/search.php +++ b/views/default/resources/translation_editor/search.php @@ -13,9 +13,11 @@ throw $exception; } +$search_keys = (bool) get_input('search_keys', true); + $language = get_input('language', 'en'); -$found = translation_editor_search_translation($q, $language); +$found = translation_editor_search_translation($q, $language, $search_keys); $trans = elgg()->translator->getInstalledTranslations(); if (!array_key_exists($language, $trans)) { @@ -50,6 +52,7 @@ $body_vars = [ 'current_language' => $language, 'query' => $q, + 'search_keys' => $search_keys, ]; $body = elgg_view_form('translation_editor/search', $form_vars, $body_vars);