Files
firefly-iii/app/Support/Twig/Translation.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2015-05-09 22:42:45 +02:00
<?php
2022-12-29 19:42:26 +01:00
/**
* Translation.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-09 22:42:45 +02:00
namespace FireflyIII\Support\Twig;
2019-12-28 09:44:56 +01:00
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
2015-05-09 22:42:45 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class Budget.
2015-05-09 22:42:45 +02:00
*/
2019-12-28 09:44:56 +01:00
class Translation extends AbstractExtension
2015-05-09 22:42:45 +02:00
{
/**
* @return array
*/
2016-04-05 22:00:03 +02:00
public function getFilters(): array
2015-05-09 22:42:45 +02:00
{
2019-12-28 09:44:56 +01:00
return [
new TwigFilter(
'_',
static function ($name) {
2022-12-29 19:42:26 +01:00
return (string)trans(sprintf('firefly.%s', $name));
2019-12-28 09:44:56 +01:00
},
['is_safe' => ['html']]
),
];
2015-05-09 22:42:45 +02:00
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->journalLinkTranslation(),
];
}
/**
2019-12-28 09:44:56 +01:00
* @return TwigFunction
*/
2019-12-28 09:44:56 +01:00
public function journalLinkTranslation(): TwigFunction
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
'journalLinkTranslation',
2019-12-28 09:44:56 +01:00
static function (string $direction, string $original) {
2022-03-29 15:00:29 +02:00
$key = sprintf('firefly.%s_%s', $original, $direction);
$translation = trans($key);
if ($key === $translation) {
return $original;
}
return $translation;
},
['is_safe' => ['html']]
);
}
2015-05-09 22:42:45 +02:00
}