在另一个翻译中引用翻译 [英] Referencing translation inside of another translation

查看:31
本文介绍了在另一个翻译中引用翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

unit:
    sqkm: Square Kilometers

my_translation: Size is %size% ## I want to append the value of unit.sqkm here ##

有没有办法引用my_translation键内unit.sqkm的翻译?

Is there a way to reference the translation of the unit.sqkm inside the my_translation key?

编辑:请注意,我知道如何通过 twig 做到这一点.我的问题是:有没有办法在翻译文件中做到这一点.

Edit: Please note that i do know how i can do this via twig. My question is: is there a way to do this in the translation files.

推荐答案

我为此扩展了 Symfony Tanslator:

I extended Symfony Tanslator for this:

<?php

namespace Bundle\Program\Translation;

use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;

class Translator extends BaseTranslator
{
    /**
     * Uses Symfony Translator to translate, but enables referencing other translations via @@code@@
     */
    public function trans($id, array $parameters = array(), $domain = null, $locale = null)
    {
        $text = parent::trans($id, $parameters, $domain, $locale);

        $translations = [];

        $delimiter = "@@";
        $strLen = strlen($delimiter);
        $pos = strpos($text, $delimiter);

        while ($pos !== false) {
            $startsAt = $pos + $strLen;
            $endsAt = strpos($text, $delimiter, $startsAt);
            $translations[] = $delimiter . substr($text, $startsAt, $endsAt - $startsAt) . $delimiter;
            $pos = strpos($text, $delimiter, $endsAt + $strLen);
        }

        foreach ($translations as $translation) {
            $translationTrim = str_replace($delimiter, '', $translation);
            $text = str_replace($translation, $this->trans($translationTrim, $parameters, $domain, $locale), $text);
        }

        return $text;
    }
}

然后通过参数替换Symfony翻译器类:

Then replace the Symfony translator class via parameters:

parameters:

    translator.class: Bundle\Program\Translation\Translator

现在您可以通过 @@other.translation@@ INSIDE 您的 yml 文件引用其他翻译.

Now you can reference other translations via @@other.translation@@ INSIDE your yml file.

这篇关于在另一个翻译中引用翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆