我遇到这种情况:
unit:
sqkm: Square Kilometers
my_translation: Size is %size% ## I want to append the value of unit.sqkm here ##
有没有办法在unit.sqkm
密钥中引用my_translation
的翻译?
修改:请注意我确实知道如何通过树枝做到这一点。我的问题是:有没有办法在翻译文件中执行此操作。
答案 0 :(得分:2)
我为此扩展了Symfony Tanslator:
<?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转换器类:
parameters:
translator.class: Bundle\Program\Translation\Translator
现在您可以通过@@ other.translation @@ INSIDE您的yml文件参考其他翻译。
答案 1 :(得分:1)
您可以在其他翻译中使用翻译的值。
{{ 'paragraph' | trans({ '%size%': 3, '%unit%': 'unit' | trans()}) }}
unit
本身是翻译的另一个关键。但是,您的翻译文件可能如下所示:
paragraph: Size is %size% %unit%
unit: Square Kilometers
答案 2 :(得分:1)
在Twig模板中,试试这个:
{{ 'my_translation' | trans({'%size%': size, 'unit.sqkm' : ('unit.sqkm'|trans)}) }}
答案 3 :(得分:0)
如果您像我一样想要在Symfony 4.1中实现此目标,这是您的解决方案 (非常感谢@Kim的answer和@Aurelijus Rozenas的answer,所有的功劳-除了我尝试了4个小时外,所有功劳都归功于他们 )
首先,创建新的Translator类(此处: src / Common / ReferenceTranslator.php )
我无法找到在类代码中直接扩展基础翻译器类的解决方案,让我知道您是否有一个
\thispagestyle{empty}
然后编辑您的 services.yaml 文件
# src/Common/ReferenceTranslator.php
namespace App\Common;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
class ReferenceTranslator implements TranslatorInterface, TranslatorBagInterface
{
/** @var TranslatorBagInterface|TranslatorInterface */
protected $translator;
/**
* @param TranslatorInterface|TranslatorBagInterface $translator
*/
public function __construct($translator)
{
$this->translator = $translator;
}
/**
* Uses Symfony Translator to translate, but enables referencing other translations via @@code@@
* @param $id
* @param array $parameters
* @param null $domain
* @param null $locale
* @return mixed|string
*/
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
$text = $this->translator->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;
}
/**
* @param string $id
* @param int $number
* @param array $parameters
* @param null $domain
* @param null $locale
*
* @return string
*/
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
/**
* @param string $locale
*/
public function setLocale($locale)
{
$this->translator->setLocale($locale);
}
/**
* @return string
*/
public function getLocale()
{
return $this->translator->getLocale();
}
/**
* @param string|null $locale
*
* @return \Symfony\Component\Translation\MessageCatalogueInterface
*/
public function getCatalogue($locale = null)
{
return $this->translator->getCatalogue($locale);
}
}
瞧瞧! 再次,如果您知道如何改进此功能,请不要犹豫,因为我不是Symfony专家!