plugins - 在 Shopware 6 插件中访问翻译器

标签 plugins shopware shopware6

我正在开发我的第一个 Shopware 6 插件,并且想知道如何访问 Plugin class 中的片段。我检查了Developer Guide但无法使其工作。
我想在 customField select options 中使用插件翻译作为 label

myfirstplugin.en-GB.json

{
    "myfirstplugin": {
        "my_custom_field_option_1": "Option 1",
        "my_custom_field_option_2": "Option 2",
    }
}

MyFirstPlugin.php

class MyFirstPlugin extends Plugin
{
// ....

    private function createCustomFields(Context $context): void
    {
        if ($this->customFieldSetExists($context)) {
            return;
        }

        $customFieldSetRepository = $this->container->get('custom_field_set.repository');
        $customFieldSetRepository->create([
            [
                'id' => '294865e5c81b434d8349db9ea6b4e135',
                'name' => 'my_custom_field_set',
                'customFields' => [
                    [
                        'name' => 'my_custom_field',
                        'type' => CustomFieldTypes::SELECT,
                        'config' => [
                            'label' => [ 'en-GB' => 'My custom field'],
                            'options' => [
                                [
                                    'value' => '294865e5c81b434d8349db9ea6b4e487',
                                    // Access my_custom_field_option_1 of snippet myfirstplugin.en-GB.json 
                                    'label' => 'my_custom_field_option_1', 
                                ],
                                [
                                    'value' => '1ce5abe719a04346930c7e43514ed4f1',
                                    // Access my_custom_field_option_2 of snippet myfirstplugin.en-GB.json 
                                    'label' => 'my_custom_field_option_2',
                                ],
                            ],
                            'customFieldType' => 'select',
                            'componentName' => 'sw-single-select',
                            'customFieldPosition' => 1,
                        ],
                    ],
                ]
            ],
        ], $context);
    }

}

最佳答案

您可以将 Translator 类型的参数注入(inject)到您的服务中

在 services.xml 中

<argument type="service" id="translator"/>

为您服务

use Shopware\Core\Framework\Adapter\Translation\Translator;

/**
 * @var Translator
 */
private $translator;
public function __construct($translator)
{
    $this->translator = $translator;
}

然后您可以使用与 Twig 模板几乎相同的方式:

$translated = $this->translator
    ->trans(
         'myfirstplugin.product.detail.294865e5c81b434d8349db9ea6b4e487');

关于plugins - 在 Shopware 6 插件中访问翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74638321/

相关文章:

java - 以编程方式更改 Eclipse 中的文件关联

Shopware 6 购物车 API

seo - 如何在 Shopware 6 中创建 SEO 网址

ubuntu - 需要帮助在 Ubuntu 上安装 Shopware6

shopware - 重新发送特定订单状态电子邮件,例如订单交付在 Shopware 6 中发货

php - 如何正确使用Flysystem和Local Adapter

php - 在带有中继器字段的单独 div 上显示多个图像

plugins - 个人 Wiki、TiddlyWiki5 和 MediaWiki 语法

java - SpringObjectFactory在struts2.3.7中创建应用程序时出错

migration - 移动不同版本的商店软件内容