Skip to content

Commit

Permalink
added: option to exclude searching in translation keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalsem committed Dec 5, 2023
1 parent ac70bc9 commit c7fe3a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions languages/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' => [],
Expand Down
21 changes: 17 additions & 4 deletions views/default/forms/translation_editor/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion views/default/resources/translation_editor/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit c7fe3a9

Please sign in to comment.