首页 > 解决方案 > symfony实体函数autput in twig translate

问题描述

在我的项目中,我有属性类型的实体,为了在树枝模板中显示,我使用函数

public function getTypeString(): string
{
    $types = self::getPredefinedTypes();

    return $types[$this->type];
}

public static function getPredefinedTypes(): array
{
    return [
        self::PROJECT_TYPE_ONETIME => 'onetime',
        self::PROJECT_TYPE_ONGOING => 'ongoing',
    ];
}

我有我的messages.cs.yaml

project.type.onetime: první typ
project.type.ongoing: druhý typ

在树枝模板中我有

{{'project.type.' ~ project.getTypeString() | trans }}

但这不显示翻译

我如何用这个例子的关键翻译?

标签: symfonytranslation

解决方案


目前您只翻译 project.getTypeString() 的返回值

{{ 'project.type.' ~ project.getTypeString() | trans }} # only value of project.getTypeString() is being translated
{{ ('project.type.' ~ project.getTypeString())|trans }} # equals 'project.type.onetime'|trans

推荐阅读