-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added missing translations in configuration
- Loading branch information
Showing
9 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |