PHP code example of webmunkeez / i18n-bundle

1. Go to this page and download the library: Download webmunkeez/i18n-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webmunkeez / i18n-bundle example snippets


// config/bundles.php

return [
    // ...
    Webmunkeez\I18nBundle\WebmunkeezI18nBundle::class => ['all' => true],
    // ...
];

final class PostTranslation
{
    #[Webmunkeez\I18nBundle\Validator\Constraint\Locale]
    private string $locale;
}

final class PostTranslation implements Webmunkeez\I18nBundle\Model\LanguageAwareInterface
{
    private string $locale;

    private ?Webmunkeez\I18nBundle\Model\Language $language = null;

    public function getLocale(): string
    {
        return $this->locale;
    }

    public function getLanguage(): ?Webmunkeez\I18nBundle\Model\Language
    {
        return $this->language;
    }

    public function setLanguage(?Webmunkeez\I18nBundle\Model\Language $language): static
    {
        $this->language = $language;

        return $this;
    }
}

final class Post implements Webmunkeez\I18nBundle\Model\TranslationAwareInterface
{
    /** @var PostTranslation[] */
    private array $translations = [];

    public function getTranslations(): iterable
    {
        return $this->translations;
    }

    public function addTranslation(Webmunkeez\I18nBundle\Model\TranslationInterface $translation): self
    {
        $this->translations[] = $translation;

        return $this;
    }

    public function getTranslation(string $locale): Webmunkeez\I18nBundle\Model\TranslationInterface
    {
        foreach ($this->translations as $translation) {
            if ($locale === $translation->getLocale()) {
                return $translation;
            }
        }

        throw new Webmunkeez\I18nBundle\Exception\TranslationNotFoundException();
    }
}

final class PostNotifier implements Webmunkeez\I18nBundle\Translation\TranslatorAwareInterface
{
    use Webmunkeez\I18nBundle\Translation\TranslatorAwareTrait;

    public function notify(): string
    {
        return $this->trans('post.created');
    }
}