Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the option to show the keys of translations #3015

Merged
merged 13 commits into from Aug 17, 2023
5 changes: 5 additions & 0 deletions config/vufind/config.ini
Expand Up @@ -1728,12 +1728,17 @@ skip_numeric = true
; directory. The value of each setting is the on-screen name of the language,
; and will itself be subject to translation through the language files!
;
; Enable "debug" to see the keys of the translation instead of a translation.
; This can be helpful in development environments but should be deactivated
; in production.
;
; The order of the settings is significant -- they will be displayed on screen
; in the same order they are defined here.
;
; Be sure that this section includes the default language set in the [Site]
; section above.
[Languages]
;debug = "Debug"
en = "English" ; American spellings
;en-gb = "English" ; British spellings
de = "German"
Expand Down
Expand Up @@ -117,6 +117,10 @@ public function addToPathStack($pathStack)
*/
public function load($locale, $filename)
{
if ($locale == 'debug') {
return null;
}

// Reset the loaded files list:
$this->resetLoadedFiles();

Expand Down
13 changes: 13 additions & 0 deletions module/VuFind/src/VuFind/I18n/Translator/TranslatorAwareTrait.php
Expand Up @@ -104,6 +104,19 @@ public function translate($target, $tokens = [], $default = null)
// Figure out the text domain for the string:
[$domain, $str] = $this->extractTextDomain($target);

if ($this->getTranslatorLocale() == 'debug') {
$targetString = $domain !== 'default' ? "$domain::$str" : $str;
$keyValueToString = function ($key, $val) {
return "$key = $val";
};
$tokenDetails = empty($tokens)
? ''
: ' | [' .
implode(', ', array_map($keyValueToString, array_keys($tokens), array_values($tokens))) .
']';
return "*$targetString$tokenDetails*";
}

// Special case: deal with objects with a designated display value:
if ($str instanceof \VuFind\I18n\TranslatableStringInterface) {
if (!$str->isTranslatable()) {
Expand Down
8 changes: 6 additions & 2 deletions module/VuFind/src/VuFindTest/Feature/TranslatorTrait.php
Expand Up @@ -5,7 +5,7 @@
*
* PHP version 8
*
* Copyright (C) Villanova University 2010.
* Copyright (C) Villanova University 2010-2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -53,9 +53,13 @@ protected function getMockTranslator($translations)
return $translations[$domain][$str] ?? $str;
};
$translator
= $this->createMock(\Laminas\I18n\Translator\TranslatorInterface::class);
= $this->getMockBuilder(\Laminas\I18n\Translator\TranslatorInterface::class)
->addMethods(['getLocale'])
->getMockForAbstractClass();
$translator->expects($this->any())->method('translate')
->will($this->returnCallback($callback));
$translator->expects($this->any())->method('getLocale')
->will($this->returnValue('en'));
return $translator;
}
}
Expand Up @@ -45,6 +45,7 @@ class ResultFeedTest extends \PHPUnit\Framework\TestCase
use \VuFindTest\Feature\LiveDetectionTrait;
use \VuFindTest\Feature\LiveSolrTrait;
use \VuFindTest\Feature\ViewTrait;
use \VuFindTest\Feature\TranslatorTrait;

/**
* Standard setup method.
Expand Down Expand Up @@ -89,31 +90,6 @@ protected function getPlugins()
return compact('currentPath', 'recordLinker') + ['serverurl' => $serverUrl];
}

/**
* Mock out the translator.
*
* @return \Laminas\I18n\Translator\TranslatorInterface
*/
protected function getMockTranslator()
{
$translations = [
'Results for' => 'Results for',
'showing_results_of_html' => 'Showing <strong>%%start%% - %%end%%'
. '</strong> results of <strong>%%total%%</strong>',
];
$mock = $this->getMockBuilder(\Laminas\I18n\Translator\TranslatorInterface::class)
->getMock();
$mock->expects($this->any())->method('translate')
->will(
$this->returnCallback(
function ($str, $params, $default) use ($translations) {
return $translations[$str] ?? $default ?? $str;
}
)
);
return $mock;
}

/**
* Test feed generation
*
Expand All @@ -135,7 +111,16 @@ public function testRSS()

$helper = new ResultFeed();
$helper->registerExtensions(new \VuFindTest\Container\MockContainer($this));
$helper->setTranslator($this->getMockTranslator());
$translator = $this->getMockTranslator(
[
'default' => [
'Results for' => 'Results for',
'showing_results_of_html' => 'Showing <strong>%%start%% - %%end%%'
. '</strong> results of <strong>%%total%%</strong>',
],
]
);
$helper->setTranslator($translator);
$helper->setView($this->getPhpRenderer($this->getPlugins()));
$feed = $helper($results, '/test/path');
$this->assertIsObject($feed);
Expand Down
Expand Up @@ -43,6 +43,8 @@
*/
class VersionsTest extends \PHPUnit\Framework\TestCase
{
use \VuFindTest\Feature\TranslatorTrait;

/**
* Test getting Description.
*
Expand All @@ -61,11 +63,13 @@ public function testGetDescription(): void
->will($this->returnValue($count));
$obj = new Versions($config, $som);
$obj->setRecordDriver($recordDriver);
$translator = $this->getMockBuilder(\Laminas\I18n\Translator\TranslatorInterface::class)
->getMock();
$translator->expects($this->any())->method('translate')
->with($this->equalTo('other_versions_title'), $this->equalTo('default'))
->will($this->returnValue('Count:%%count%%'));
$translator = $this->getMockTranslator(
[
'default' => [
'other_versions_title' => 'Count:%%count%%',
],
]
);
$obj->setTranslator($translator);
$obj->getDescription();
$this->assertEquals("Count:$count", $obj->getDescription());
Expand Down
Expand Up @@ -31,7 +31,6 @@

namespace VuFindTest\Search\Solr;

use Laminas\I18n\Translator\TranslatorInterface;
use VuFind\Config\PluginManager;
use VuFind\I18n\TranslatableString;
use VuFind\Record\Loader;
Expand All @@ -56,6 +55,7 @@
class ResultsTest extends \PHPUnit\Framework\TestCase
{
use \VuFindTest\Feature\ConfigPluginManagerTrait;
use \VuFindTest\Feature\TranslatorTrait;

/**
* Test CursorMark functionality.
Expand All @@ -76,16 +76,16 @@ public function testCursorMark(): void
*/
public function testFacetTranslations(): void
{
$mockTranslator = $this->createMock(TranslatorInterface::class);
$mockTranslator->expects($this->exactly(2))
->method('translate')
->withConsecutive(
[$this->equalTo('000')],
[$this->equalTo('dewey_format_str')]
)->willReturnOnConsecutiveCalls(
'Computer science, information, general works',
'%%raw%% - %%translated%%'
);
$mockTranslator = $this->getMockTranslator(
[
'default' => [
'dewey_format_str' => '%%raw%% - %%translated%%',
],
'DDC23' => [
'000' => 'Computer science, information, general works',
],
]
);
$mockConfig = $this->createMock(PluginManager::class);
$options = new Options($mockConfig);
$options->setTranslator($mockTranslator);
Expand Down Expand Up @@ -120,8 +120,8 @@ public function testFacetTranslations(): void
$results = $this->getResults($params, $searchService);
$list = $results->getFacetList();
$this->assertEquals(
$list['dewey-raw']['list'][0]['displayText'],
'000 - Computer science, information, general works'
'000 - Computer science, information, general works',
$list['dewey-raw']['list'][0]['displayText']
);
}

Expand Down