PHP code example of letkode / locale-bundle

1. Go to this page and download the library: Download letkode/locale-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/ */

    

letkode / locale-bundle example snippets


// config/bundles.php
return [
    Letkode\LocaleBundle\LetkodeLocaleBundle::class => ['all' => true],
];

use Letkode\LocaleBundle\Provider\LocaleProvider;

public function __construct(private readonly LocaleProvider $localeProvider) {}

public function doSomething(): void
{
    $locale = $this->localeProvider->get(); // 'en' | 'es' | ...
}

use Letkode\LocaleBundle\Applier\TranslatableFieldApplier;

// $entity has: public array|null $translations = null; and setTranslation(string $locale, string $field, string|null $value): void

$this->translatableFieldApplier->apply($entity, 'name', ['en' => 'Module', 'es' => 'Módulo']);
// $entity->name is now 'Module' (default locale), $entity->translations holds both

use Letkode\LocaleBundle\Trait\HasEnumTranslationLabelTrait;

enum IdentityTypeEnum: string
{
    use HasEnumTranslationLabelTrait;

    case PRINCIPAL = 'principal';

    private static function translationDomain(): string
    {
        return 'identity_type'; // reads identity_type.{locale}.yaml
    }
}

$label = IdentityTypeEnum::PRINCIPAL->getLabel($translator); // translated for the current locale

use Letkode\LocaleBundle\Trait\HasTranslationsTrait;

#[ORM\Entity]
class Product
{
    use HasTranslationsTrait;
}

$entity->setTranslation('es', 'name', 'Producto');
$entity->getTranslation('es', 'name'); // 'Producto'

src/
  LetkodeLocaleBundle.php
  EventListener/LocaleListener.php
  Provider/LocaleProvider.php
  Applier/TranslatableFieldApplier.php
  Trait/HasEnumTranslationLabelTrait.php
  Trait/HasTranslationsTrait.php