Skip to content

Commit

Permalink
fix: added missing translations in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jun 16, 2024
1 parent c50ad2a commit 33964e9
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 6 deletions.
12 changes: 9 additions & 3 deletions phpmyfaq/assets/templates/admin/configuration/macros.twig
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@

<label for="edit[{{ key }}]" class="form-label small mb-1">{{ label | raw }}</label>
<div class="input-group">
<input class="form-control" type="{{ type }}" name="edit[{{ key }}]" id="edit[{{ key }}]" value="{{ value|e('html_attr') }}" step="1" min="0">
<input class="form-control" type="{{ type }}" name="edit[{{ key }}]" id="edit[{{ key }}]"
value="{{ value|e('html_attr') }}" step="1" min="0">

{% if key == 'api.apiClientToken' %}
<div class="input-group-append">
<button class="btn btn-dark" id="pmf-generate-api-token" type="button" onclick="generateApiToken()">Generate API Client Token</button>
<button class="btn btn-dark" id="pmf-generate-api-token" type="button" onclick="generateApiToken()">
{{ 'msgGenerateApiClientToken' | translate }}
</button>
</div>
{% endif %}
</div>
Expand All @@ -66,7 +69,10 @@
{# button #}
{% macro sendTestMailButton(label, key) %}
<label for="edit[{{ key }}]" class="form-label small mb-1">{{ label | raw }}</label>
<button type="button" class="btn btn-primary" id="btn-phpmyfaq-{{ key|replace({'.': '-'}) }}" onclick="handleSendTestMail()">{{ key }}</button>
<button type="button" class="btn btn-primary" id="btn-phpmyfaq-{{ key|replace({'.': '-'}) }}"
onclick="handleSendTestMail()">
{{ key | translate }}
</button>
{% endmacro %}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% for key, configItem in configurationList %}
<div class="row mb-2 p-1">
<div class="col-9 border-start border-5">
<div class="col-9 border-start border-bottom border-2 pb-2">
{% if configItem.element == 'area' %}
{{ macros.generateTextarea(configItem.label, key, configurationData[key]) }}
{% elseif configItem.element == 'checkbox' %}
Expand Down
2 changes: 2 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Template\TemplateException;
use phpMyFAQ\Template\TranslateTwigExtension;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function render(string $pathToTwigFile, array $templateVars = [], Respons
$response ??= new Response();
$twigWrapper = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twigWrapper->addExtension(new DebugExtension());
$twigWrapper->addExtension(new TranslateTwigExtension());
$template = $twigWrapper->loadTemplate($pathToTwigFile);

$response->setContent($template->render($templateVars));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function list(Request $request): Response
'ldapSupport' => extension_loaded('ldap'),
'useSslForLogins' => Request::createFromGlobals()->isSecure(),
'useSslOnly' => Request::createFromGlobals()->isSecure(),
'ssoSupport' => Request::createFromGlobals()->server->get('REMOTE_USER')
'ssoSupport' => Request::createFromGlobals()->server->get('REMOTE_USER'),
'buttonTes'
]
]
);
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Faq/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Statistics
private array $groups;

/** Flag for Group support. */
private bool $groupSupport;
private bool $groupSupport = false;

/** Plural form support. */
private readonly Plurals $plurals;
Expand Down
47 changes: 47 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Template/TranslateTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Twig extension to translate the given translation key
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ\Template
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-04-21
*/

declare(strict_types=1);

namespace phpMyFAQ\Template;

use phpMyFAQ\Translation;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\TwigFilter;

class TranslateTwigExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('translate', $this->translate(...)),
];
}

public function getFilters(): array
{
return [
new TwigFilter('translate', $this->translate(...)),
];
}

public function translate(string $translationKey): string
{
return Translation::get($translationKey);
}
}
1 change: 1 addition & 0 deletions phpmyfaq/translations/language_de.php
Original file line number Diff line number Diff line change
Expand Up @@ -1437,5 +1437,6 @@
$PMF_LANG['msgNoLogMeOut'] = 'Nein, logge mich aus';
$PMF_LANG['msgYesKeepMeLoggedIn'] = 'Ja, ich will eingeloggt bleiben';
$PMF_LANG['msgTrendingFAQs'] = 'FAQ Trends';
$PMF_LANG['msgGenerateApiClientToken'] = 'Erstelle einen API Client Token';

return $PMF_LANG;
1 change: 1 addition & 0 deletions phpmyfaq/translations/language_en.php
Original file line number Diff line number Diff line change
Expand Up @@ -1457,5 +1457,6 @@
$PMF_LANG['msgNoLogMeOut'] = 'No, log me out';
$PMF_LANG['msgYesKeepMeLoggedIn'] = 'Yes, keep me logged in';
$PMF_LANG['msgTrendingFAQs'] = 'Trending FAQs';
$PMF_LANG['msgGenerateApiClientToken'] = 'Generate API Client Token';

return $PMF_LANG;
56 changes: 56 additions & 0 deletions tests/phpMyFAQ/Template/TranslateTwigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace phpMyFAQ\Template;

use phpMyFAQ\Strings;
use phpMyFAQ\Translation;
use PHPUnit\Framework\TestCase;
use Twig\TwigFilter;

class TranslateTwigExtensionTest extends TestCase
{
private TranslateTwigExtension $translateTwigExtension;

protected function setUp(): void
{
parent::setUp();

Strings::init();

Translation::create()
->setLanguagesDir(PMF_TRANSLATION_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage('en')
->setMultiByteLanguage();

$this->translateTwigExtension = new TranslateTwigExtension();
}
public function testGetFunctions(): void
{
$functions = $this->translateTwigExtension->getFunctions();

$this->assertIsArray($functions);
$this->assertCount(1, $functions);
$this->assertEquals('translate', $functions[0]->getName());
}

public function testGetFilters(): void
{
$filters = $this->translateTwigExtension->getFilters();

$this->assertCount(1, $filters);

$this->assertInstanceOf(TwigFilter::class, $filters[0]);
$this->assertEquals('translate', $filters[0]->getName());
}

public function testTranslate(): void
{
$translationKey = 'msgCategory';
$expectedTranslation = 'Categories';

$actualTranslation = $this->translateTwigExtension->translate($translationKey);

$this->assertEquals($expectedTranslation, $actualTranslation);
}
}

0 comments on commit 33964e9

Please sign in to comment.