darkalchemy/twig-translate

该软件包已被废弃且不再维护。未建议替代软件包。

一个Twig翻译扩展

v0.3.5 2022-04-01 13:44 UTC

README

Codacy Badge

一个Twig翻译扩展。
这个twig扩展是从Odan/Twig-Translation分支出来的,因为我想要有更多可用的翻译方法。
因此,这个扩展使用delight-im/PHP-I18N来处理翻译。

要求

  • PHP 7.4或PHP 8.0

安装 - 这些说明假设使用php-di进行依赖注入

composer require darkalchemy/twig-translate

集成

注册Twig扩展以及将要可用的区域代码

这会使函数可用于所有在twig模板中的字符串

在容器中

\Delight\I18n\I18n::class => DI\factory(function () {
    return new \Delight\I18n\I18n([
        \Delight\I18n\Codes::EN_US,
        \Delight\I18n\Codes::FR_FR,
    ]);
}),

\Slim\Views\Twig::class => function (\Psr\Container\ContainerInterface $container) {
    $settings = $container->get(Configuration::class)->all();
    $twig = \Slim\Views\Twig::create($settings['twig']['path'], [
        'cache' => $settings['twig']['cache'] ?? false,
    ]);
    $twig->addExtension(new \darkalchemy\Twig\TwigTranslationExtension($container->get(\Delight\I18n\I18n::class)));

    return $twig;
}

在app.php中添加一个$i18n,以便可以在helpers.php中调用全局

$i18n = $container->get(\Delight\I18n\I18n::class);

在文件如helpers.php中添加函数,这使得函数可用于不在twig模板中的所有字符串

function compile_twig_templates(\Psr\Container\ContainerInterface $container)
{
    $settings    = $container->get(\Selective\Config\Configuration::class)->all();
    $twig_config = $settings['twig'];
    $cache       = $twig_config['cache'] ?? $settings['root'] . '/resources/views/cache/';
    $twig        = $container->get(Twig::class)->getEnvironment();
    $compiler = new \darkalchemy\Twig\TwigCompiler($twig, $cache);

    try {
        $compiler->compile();
    } catch (Exception $e) {
        die($e->getMessage());
    }

    echo "\nCompiling twig templates completed\n\n";
    echo "to fix the permissions, you should run:\nsudo chown -R www-data:www-data {$cache}\nsudo chmod -R 0775 {$cache}\n";

    return 0;
}

function __f(string $text, ...$replacements)
{
    global $i18n;

    return $i18n->translateFormatted($text, ...$replacements);
}

function __fe(string $text, ...$replacements)
{
    global $i18n;

    return $i18n->translateFormattedExtended($text, ...$replacements);
}

function __p(string $text, string $alternative, int $count)
{
    global $i18n;

    return $i18n->translatePlural($text, $alternative, $count);
}

function __pf(string $text, string $alternative, int $count, ...$replacements)
{
    global $i18n;

    return $i18n->translatePluralFormatted($text, $alternative, $count, ...$replacements);
}

function __pfe(string $text, string $alternative, int $count, ...$replacements)
{
    global $i18n;

    return $i18n->translatePluralFormattedExtended($text, $alternative, $count, ...$replacements);
}

function __c(string $text, string $context)
{
    global $i18n;

    return $i18n->translateWithContext($text, $context);
}

function __m(string $text)
{
    global $i18n;

    return $i18n->markForTranslation($text);
}

将i18n.sh从I18n复制到bin目录

cp vendor/delight-im/i18n/i18n.sh bin/
chmod a+x bin/i18n.sh

为了翻译你的twig模板,你首先需要编译模板。创建一个文件bin/translate.php并将其添加到其中

<?php

declare(strict_types=1);

use Delight\I18n\I18n;
use Selective\Config\Configuration;

$container = (require_once __DIR__ . '/../bootstrap/app.php')->getContainer();

$root_path = $container->get(Configuration::class)->findString('root');
$processes = [
    'compile',
    'translate',
];

$languages = [];
$locales   = $container->get(I18n::class)->getSupportedLocales();
foreach ($locales as $locale) {
    $languages[] = str_replace('-', '_', $locale);
}
$process = 'compile';
$lang    = 'en_US';
foreach ($argv as $arg) {
    if (in_array($arg, $processes)) {
        $process = $arg;
    } elseif (in_array($arg, $languages)) {
        $lang = $arg;
    }
}

switch ($process) {
    case 'translate':
        copy($root_path . '/bin/i18n.sh', $root_path . '/i18n.sh');
        chmod($root_path . '/i18n.sh', 0755);
        passthru("sed -i -E 's/\\-\\-(keyword|flag)=\"_(f|p|c|m)/\\-\\-\\1=\"__\\2/g' {$file}");
        passthru("sed -i 's/\\-\\-keyword \\-\\-keyword/\\-\\-keyword \\-\\-keyword=\"translateFormatted:1\" \\-\\-keyword=\"translateFormattedExtended:1\" \\-\\-keyword=\"translatePlural:1,2,3t\" \\-\\-keyword=\"translatePluralFormatted:1,2\" \\-\\-keyword=\"translatePluralFormattedExtended:1,2\" \\-\\-keyword=\"translateWithContext:1,2c,2t\" \\-\\-keyword=\"markForTranslation:1,1t\" \\-\\-flag=\"translateFormatted:1:php\\-format\" \\-\\-flag=\"translateFormattedExtended:1:no\\-php\\-format\" \\-\\-flag=\"translatePluralFormatted:1:php\\-format\" \\-\\-flag=\"translatePluralFormattedExtended:1:no\\-php\\-format\" \\-\\-keyword/g' i18n.sh");
        passthru(sprintf('./i18n.sh %s', $lang));
        unlink($root_path . '/i18n.sh');

        break;
    default:
        compile_twig_templates($container);

        break;
}

要翻译为fr_FR区域,运行

php bin/translate.php fr_FR

然后,在poedit中编辑messages.po,验证并保存。

有关每个函数的完整文档,请参阅PHP-I18N,其中每个函数都有很好的文档。