Browse Source

I18N

tags/0.1
Guillaume 3 years ago
parent
commit
da50a71cbc
11 changed files with 177 additions and 87 deletions
  1. +3
    -1
      Controller/Admin/UserController.php
  2. +23
    -6
      Resources/translations/admin.fr.yaml
  3. +1
    -1
      Resources/views/adminlte/crud/edit.html.twig
  4. +3
    -2
      Resources/views/adminlte/crud/form_theme.html.twig
  5. +0
    -2
      Resources/views/adminlte/crud/new.html.twig
  6. +1
    -2
      Resources/views/adminlte/crud/sort.html.twig
  7. +3
    -1
      Resources/views/adminlte/dashboard.html.twig
  8. +1
    -1
      Resources/views/user/change_password.html.twig
  9. +1
    -1
      Resources/views/user/profile.html.twig
  10. +125
    -0
      Translation/TranslatorAdmin.php
  11. +16
    -70
      Twig/TwigExtension.php

+ 3
- 1
Controller/Admin/UserController.php View File

@@ -2,9 +2,11 @@

namespace Lc\SovBundle\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Form\Type\User\ChangePasswordFormType;
use Lc\SovBundle\Form\Type\User\ProfileFormType;
use Lc\SovBundle\Model\User\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -37,7 +39,7 @@ class UserController extends AbstractController
return $this->render(
'@LcSov/user/profile.html.twig',
[
'form' => $form->createView()
'form' => $form->createView(),
]
);
}

+ 23
- 6
Resources/translations/admin.fr.yaml View File

@@ -3,20 +3,37 @@ flash_message:
update: Le contenu "%name%" a été mis à jour avec succès.
delete: Le contenu "%name%" a été supprimé avec succès.

form:
title:
edit: Modifier "%name%"
new: Ajouter "%name%"

menu:
dashboard: Tableau de bord
page: Pages
user:
label: Utilisateurs
childs:
index: Liste
account:
label: Mon compte
childs:
profile: Informations personnelles
password: Mot de passe

title:
dashboard: Tableau de bord
index: '%label_plurial%'
sort: Ordonner "%label_plurial%"
new: Ajouter "%label%"
edit: Modifier "%label%" (#%id%)
detail: Voir "%label%" (#%id%)
account:
profile: Mes informations personnelles
change_password: Changer de mot de passe

entity:
user:
label: Utilisateur
label_plurial: Utilisateurs
page:
label: Page
label_plurial: Pages
default:
fields:
title: Titre
@@ -26,7 +43,7 @@ entity:
description_help: Description help
file: Fichier
status: Statut
email: Email
panels:
general: Général
configuration: Configuration

+ 1
- 1
Resources/views/adminlte/crud/edit.html.twig View File

@@ -7,7 +7,7 @@
{% block body_class 'ea-edit ea-edit-' ~ entity.name %}

{% block content_title %}
{{ 'edit'|lc_trans_admin_title(ea.getEntity().getFqcn()) }}
{{ 'edit'|lc_trans_admin_title(ea.getEntity().getFqcn(), {id: ea.getEntity().getInstance().getId()}) }}
{% endblock %}

{% block delete_form %}

+ 3
- 2
Resources/views/adminlte/crud/form_theme.html.twig View File

@@ -43,7 +43,7 @@
</div>
{% endif %}

{% set help_message = name|lc_trans_admin_field(ea.getEntity().getFqcn(), true) %}
{% set help_message = name|lc_trans_admin_help(ea.getEntity().getFqcn()) %}
{% if help_message != '' %}
<small class="form-help">{{ help_message }}</small>
{% endif %}
@@ -283,7 +283,8 @@
{% if panel_config.icon|default(false) %}
<i class="{{ panel_config.icon }}"></i>
{% endif %}
{{ panel_config.label|raw }}

{{ panel_config.label|lc_trans_admin_panel(ea.getEntity().getFqcn()) }}

{% if collapsible %}
</a>

+ 0
- 2
Resources/views/adminlte/crud/new.html.twig View File

@@ -10,8 +10,6 @@
{{ 'new'|lc_trans_admin_title(ea.getEntity().getFqcn()) }}
{% endblock %}



{% block body_javascript %}
{{ parent() }}


+ 1
- 2
Resources/views/adminlte/crud/sort.html.twig View File

@@ -8,10 +8,9 @@
{% block body_class 'index' ~ (entities|length > 0 ? ' index-' ~ entities|first.name : '') %}

{% block content_title %}
Édition position
{{ 'sort'|lc_trans_admin_title(ea.getEntity().getFqcn()) }}
{% endblock %}


{% block content_breadcrumb %}
{{ include('@LcSov/adminlte/block/breadcrumb.html.twig') }}
{% endblock content_breadcrumb %}

+ 3
- 1
Resources/views/adminlte/dashboard.html.twig View File

@@ -1,6 +1,8 @@
{% extends '@LcSov/adminlte/layout.html.twig' %}

{% block content_title 'SovBundle' %}
{% block content_title %}
{{ 'dashboard'|lc_trans_admin_title }}
{% endblock %}

{% block main %}
Tableau de bord par défaut.

+ 1
- 1
Resources/views/user/change_password.html.twig View File

@@ -2,7 +2,7 @@
{% extends '@LcSov/adminlte/layout.html.twig' %}

{% block content_title %}
Changer de mot de passe
{{ 'account.change_password'|lc_trans_admin_title }}
{% endblock %}

{% block main %}

+ 1
- 1
Resources/views/user/profile.html.twig View File

@@ -1,7 +1,7 @@
{% extends '@LcSov/adminlte/layout.html.twig' %}

{% block content_title %}
Mes informations personnelles
{{ 'account.profile'|lc_trans_admin_title }}
{% endblock %}

{% block main %}

+ 125
- 0
Translation/TranslatorAdmin.php View File

@@ -0,0 +1,125 @@
<?php

namespace Lc\SovBundle\Translation;

use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use Symfony\Contracts\Translation\TranslatorInterface;

class TranslatorAdmin
{
protected $translator;

const DOMAIN = 'admin';

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function transField($fieldName, $entityClass): string
{
return $this->transEntityThenDefault(
$this->buildTransIdField($fieldName, $entityClass),
$this->buildTransIdField($fieldName, $entityClass, true)
);
}

public function transHelp($fieldName, $entityClass): string
{
$fieldName = $fieldName . '_help';

return $this->transEntityThenDefault(
$this->buildTransIdField($fieldName, $entityClass),
$this->buildTransIdField($fieldName, $entityClass, true),
true
);
}

public function transPanel($panelName, $entityClass): string
{
return $this->transEntityThenDefault(
$this->buildTransIdPanel($panelName, $entityClass),
$this->buildTransIdPanel($panelName, $entityClass, true)
);
}

public function transTitle($pageName, $entityClass = null, $params = [])
{
$entityName = $this->getEntityName($entityClass);

$paramsTranslation = [] ;

if($entityName) {
$baseIdEntityLabel = 'entity.' . $entityName ;
$paramsTranslation = [
'%label%' => $this->trans($baseIdEntityLabel . '.label'),
'%label_plurial%' => $this->trans($baseIdEntityLabel . '.label_plurial'),
] ;
}

if (isset($params['id'])) {
$paramsTranslation['%id%'] = $params['id'];
}

return $this->trans(
'title.' . $pageName,
$paramsTranslation
);
}

private function transEntityThenDefault($idTranslationEntity, $idTranslationDefault, $returnEmpty = false): string
{
$translation = $this->trans($idTranslationEntity);

if ($translation == $idTranslationEntity) {
$translation = $this->trans($idTranslationDefault);

if ($translation == $idTranslationDefault) {
if ($returnEmpty) {
$translation = '';
} else {
$translation = $idTranslationEntity;
}
}
}

return $translation;
}

private function buildTransIdField($fieldName, $entityClass, $default = false): string
{
if ($default) {
$entityName = 'default';
} else {
$entityName = $this->getEntityName($entityClass);
}

return 'entity.' . $entityName . '.fields.' . $fieldName;
}

private function buildTransIdPanel($panelName, $entityClass, $default = false): string
{
if ($default) {
$entityName = 'default';
} else {
$entityName = $this->getEntityName($entityClass);
}



return 'entity.' . $entityName . '.panels.' . $panelName;
}

private function trans($id, $params = [], $domain = self::DOMAIN): string
{
return $this->translator->trans($id, $params, $domain);
}

private function getEntityName($entityClass): string
{
$entityNameExplode = explode('\\', $entityClass);
return strtolower($entityNameExplode[count($entityNameExplode) - 1]);
}
}

?>

+ 16
- 70
Twig/TwigExtension.php View File

@@ -4,6 +4,7 @@ namespace Lc\SovBundle\Twig;

use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -23,6 +24,7 @@ class TwigExtension extends AbstractExtension
protected $requestStack;
protected $router;
protected $translator;
protected $translatorAdmin;

public function __construct(
KernelInterface $kernel,
@@ -31,7 +33,8 @@ class TwigExtension extends AbstractExtension
EntityManagerInterface $entityManager,
RequestStack $requestStack,
UrlGeneratorInterface $router,
TranslatorInterface $translator
TranslatorInterface $translator,
TranslatorAdmin $translatorAdmin
) {
$this->kernel = $kernel;
$this->parameterBag = $parameterBag;
@@ -40,6 +43,7 @@ class TwigExtension extends AbstractExtension
$this->requestStack = $requestStack;
$this->router = $router;
$this->translator = $translator;
$this->translatorAdmin = $translatorAdmin;
}

public function getFunctions()
@@ -58,6 +62,7 @@ class TwigExtension extends AbstractExtension
return [
new TwigFilter('lc_cache', [$this, 'lcCache']),
new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
new TwigFilter('lc_trans_admin_help', [$this, 'lcTransAdminHelp']),
new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
@@ -74,88 +79,29 @@ class TwigExtension extends AbstractExtension
}
}

public function lcTransAdminField($fieldName, $entityClass, $isHelp = false)
public function lcTransAdminField($fieldName, $entityClass)
{
if ($isHelp) {
$fieldName = $fieldName . '_help';
}

$entityName = $this->getEntityName($entityClass) ;

$entityLabel = 'entity.' . $entityName . '.fields.' . $fieldName;
$translatedLabel = $this->translator->trans($entityLabel, [], 'admin');

if ($translatedLabel == $entityLabel) {
$defaultLabel = 'entity.default.fields.' . $fieldName;
$translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');

if ($translatedLabel == $defaultLabel) {
if ($isHelp) {
$translatedLabel = '';
} else {
$translatedLabel = $entityLabel;
}
}
}

return $translatedLabel;
return $this->translatorAdmin->transField($fieldName, $entityClass) ;
}

public function lcTransAdminPanel($panelName, $entityClass)
public function lcTransAdminHelp($fieldName, $entityClass)
{
$entityName = $this->getEntityName($entityClass) ;

$panelLabel = 'entity.' . $entityName . '.panels.' . $panelName;
$translatedLabel = $this->translator->trans($panelLabel, [], 'admin');

if ($translatedLabel == $panelLabel) {
$defaultLabel = 'entity.default.panels.' . $panelName;
$translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');

if ($translatedLabel == $defaultLabel) {
$translatedLabel = $panelLabel;
}
}

return $translatedLabel;
return $this->translatorAdmin->transHelp($fieldName, $entityClass) ;
}

public function lcTransAdminMenu($menuName)
public function lcTransAdminPanel($panelName, $entityClass)
{
$menuLabel = 'menu.'.$menuName;
$translatedLabel = $this->translator->trans($menuLabel, [], 'admin');

return $translatedLabel ;
return $this->translatorAdmin->transPanel($panelName, $entityClass) ;
}

public function lcTransAdminTitle($pageName, $entityClass)
public function lcTransAdminMenu($menuName)
{
$entityName = $this->getEntityName($entityClass) ;
$translatedLabel = '' ;

if(Crud::PAGE_INDEX == $pageName) {
$titleLabel = 'entity.'.$entityName.'.label_plurial' ;
$translatedLabel = $this->translator->trans($titleLabel, [], 'admin');
}
elseif(Crud::PAGE_EDIT == $pageName) {
$entityLabel = 'entity.'.$entityName.'.label' ;
$translatedEntityLabel = $this->translator->trans($entityLabel, [],'admin');
$translatedLabel = $this->translator->trans('form.title.edit', ['%name%' => $translatedEntityLabel], 'admin');
}
elseif(Crud::PAGE_NEW == $pageName) {
$entityLabel = 'entity.'.$entityName.'.label' ;
$translatedEntityLabel = $this->translator->trans($entityLabel, [], 'admin');
$translatedLabel = $this->translator->trans('form.title.new', ['%name%' => $translatedEntityLabel], 'admin');
}

return $translatedLabel ;
return $this->translator->trans('menu.' . $menuName, [], 'admin');;
}

public function getEntityName($entityClass)
public function lcTransAdminTitle($pageName, $entityClass = null, $params = [])
{
$entityNameExplode = explode('\\', $entityClass);
$entityName = strtolower($entityNameExplode[count($entityNameExplode) - 1]);
return $entityName ;
return $this->translatorAdmin->transTitle($pageName, $entityClass, $params) ;
}

public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')

Loading…
Cancel
Save