定制 sonofwinter/translation-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sonofwinter/translation-bundle

最新稳定版本:1.1.0

Composer 安装命令:

composer require sonofwinter/translation-bundle

包简介

This Bundle provides a translator from Entity

README 文档

README

This Bundle provides a translator for Symfony entities.

Prerequisites

  • PHP 8.2 or higher
  • Symfony 7.0 or higher
  • Composer

Breaking Changes in v1.1

In the Translatabe trait, the getId() method has been renamed to getIdentifier() to avoid conflicts with existing getId() methods in entities.

Installation

Open a command console, enter your project directory and execute:

$ composer require sonofwinter/translation-bundle

Configuration

Bundle Registration

Register the bundle in your config/bundles.php:

return [
    // ...
    SOW\TranslationBundle\SOWTranslationBundle::class => ['all' => true],
];

Available Locales

You can override the default available locales by setting the sow_translation.available_locales parameter:

parameters:
    sow_translation.available_locales: ['en', 'fr', 'es', 'de', 'it']

Custom Translation Entity

By default, a Translation entity class is provided, but you can create your own translation entity class that extends AbstractTranslation. To use it, set the sow_translation.translation_class_name parameter:

parameters:
    sow_translation.translation_class_name: App\Entity\YourTranslationClass

Usage

Setting Up Translatable Entities

Your translated entities must implement the Translatable interface. Then define translated properties in your entity using either annotations or attributes.

Using Attributes (PHP 8.0+)

use SOW\TranslationBundle\Attribute\Translation;

class MyClass {
    #[Translation(key: "firstname")]
    private string $firstname = '';

    #[Translation(key: "lastname", setter: "setOtherName")]
    private string $lastname = '';
}

Configuration Notes

  • The key property can be used to specify a different name for the translation key. If not provided, the property name is used.
  • The setter property allows you to specify a custom setter method. If the setter doesn't exist, a TranslatableConfigurationException will be thrown.

Translation Methods

Translating Entities

// Translate an entity to a specific language
$translator->translate($entity, 'en');

// Translate an entity to multiple languages
$translator->translateForLangs($entity, ['en', 'fr', 'de']);

Setting Translations

// Set a single translation
$translator->setTranslationForLangAndValue($entity, 'en', 'firstname', 'John');

// Set multiple values for one language
$translator->setTranslationForLangAndValues($entity, 'en', [
    'firstname' => 'John',
    'lastname' => 'Doe'
]);

// Set multiple translations for multiple languages
$translator->setTranslations($entity, [
    'en' => [
        'firstname' => 'John',
        'lastname' => 'Doe'
    ],
    'fr' => [
        'firstname' => 'Jean',
        'lastname' => 'Dupont'
    ]
]);

Removing Translations

// Remove a specific translation
$translator->removeByObjectKeyAndLang($entity, 'firstname', 'en');

// Remove all translations for an entity
$translator->removeAllForTranslatable($entity);

// Remove all translations for a specific key
$translator->removeAllByKey('firstname');

统计信息

  • 总下载量: 1.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-09