Symfony 4 / Sonata: Quản lý Giao diện Quản trị Đa Ngôn ngữ

Chúng ta sẽ xem cách thiết lập giao diện quản trị đa ngôn ngữ với nút chọn ngôn ngữ (bộ chuyển đổi ngôn ngữ).

Cài đặt translationBundle

composer require sonata-project/translation-bundle

bin/console assets:install

Bộ chuyển đổi ngôn ngữ yêu cầu một bộ lọc twig cụ thể.
Nếu không, bạn sẽ nhận được lỗi như:

Unknown "language_name" filter.

Sau đó, bạn cần cài đặt hai gói này

composer require twig/intl-extra
composer require twig/extra-bundle
bin/console cache:clear

Chúng ta thêm cấu hình mặc định

# config/packages/sonata_translation.yaml

sonata_translation:
    locales: [en, fr, it, nl, es]
    default_locale: en
    # change default behavior for translated field filtering.
    default_filter_mode: gedmo # must be either 'gedmo' or 'knplabs', default: gedmo
    # here enable the types you need
    gedmo:
        enabled: true
    knplabs:
        enabled: true
    #phpcr:
    #    enabled: true

Và chúng ta bao gồm các kiểu trong quản trị viên

# config/packages/sonata_admin.yaml

sonata_admin:
    assets:
        extra_stylesheets:
            - bundles/sonatatranslation/css/sonata-translation.css

Chúng ta ghi đè hiển thị của mẫu sonata

# config/packages/sonata_admin.yaml

sonata_admin:
    templates:
        layout: '@SonataTranslation/standard_layout.html.twig'

Chúng ta thêm các tuyến đường

# config/routes.yaml

sonata_translation:
    resource: '@SonataTranslationBundle/Resources/config/routes.yaml'

Và chúng ta kích hoạt bộ chuyển đổi ngôn ngữ

# config/packages/sonata_translation.yaml

sonata_translation:
    locale_switcher: true

Và điểm cuối cùng để tránh lỗi

Argument 5 passed to Twig\Extra\Intl\IntlExtension::formatDateTime() must be of the type string, null given,



Bạn sẽ phải tải gói twig/extra-bundle trước SonataIntlBundle để chức năng formatDateTime của sonataIntl ghi đè lên chức năng của TwigExtraBundle.

Điều này cho chúng ta:

    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
    Sonata\IntlBundle\SonataIntlBundle::class => ['all' => true],
    Sonata\TranslationBundle\SonataTranslationBundle::class => ['all' => true],
Sélection_413