No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
src
README.md
composer.json

README.md

translatable

Module for Zend Framework 3.

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.

composer require vim/translatable

Register Vim\Translatable in config/modules.config.php:

<?php
return [
    'Vim\Translatable',
];

Current Language

Get current language:

<?php
/** @var \Vim\Translatable\Service\CurrentLanguageService $currentLanguageService */
$currentLanguageService = $container->get(\Vim\Translatable\Service\CurrentLanguageService::class);
$currentLanguageService->get();

Language alias is taken from the locale (\Locale::getDefault()).

Set current language:

<?php
/** @var \Vim\Translatable\Service\CurrentLanguageService $currentLanguageService */
$currentLanguageService = $container->get(\Vim\Translatable\Service\CurrentLanguageService::class);
$currentLanguageService->set('en');

It is not recommended to use this method.

You can use initializer for \Vim\Translatable\Service\CurrentLanguageService.

To do this, register initializer in service manager config.

<?php
return [
    'service_manager' => [
        'initializers' => [
            \Vim\Translatable\ServiceManager\Initializer\Service\CurrentLanguageServiceInitializer::class,
        ],
    ],
];

Examples

Translatable Entity:

<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
use Vim\Translatable\Entity\TranslatableEntityAbstract;

/**
 * @ORM\Table(name="article")
 * @ORM\Entity
 * @Vim\Translatable\Annotation\TranslatableEntityAnnotation(
 *     className="Application\Entity\ArticleTranslationEntity",
 * )
 * @param string $title
 */
class ArticleEntity extends TranslatableEntityAbstract
{
    /**
     * @var string
     * @ORM\Column(name="alias", type="string", nullable=false)
     */
    public $alias;

    /**
     * @var Collection|Selectable
     *
     * @ORM\OneToMany(
     *   targetEntity="ArticleTranslationEntity",
     *   mappedBy="object",
     *   cascade={"persist", "remove"}
     * )
     */
    public $translation;
}

Translation Entity:

<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vim\Translatable\Entity\TranslationEntityAbstract;

/**
 * @ORM\Entity
 * @ORM\Table(name="article_translation",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="ux_article_translation_unique", columns={
 *         "language", "object_id"
 *     })}
 * )
 */
class ArticleTranslationEntity extends TranslationEntityAbstract
{
    /**
     * @var string
     * @ORM\Column(name="title", type="string", nullable=false)
     */
    public $title;

    /**
     * @var ArticleEntity
     * @ORM\ManyToOne(targetEntity="ArticleEntity", inversedBy="translation")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    public $object;
}