Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/10/2011, 12:01
victorcruz
 
Fecha de Ingreso: septiembre-2010
Mensajes: 4
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Traducir formularios y no morir intento en syfmony2

Sí amigo Muchas Gracias por haber publicado la forma de poder internacionalizar los formularios generados por SF2, yo seguí todos tus pasos quedándome así:

//---Dudas----------------------------------------------------------------------------------------------------------------------------
¿A qué le digo id?
Mi formulario de traducción al español "src/Didetech/SisconBundle/Resources/translations/DidetechSisconBundle.es.yml" es así:

# Login [todo los demás están en el bundle FOSUserBundle]
login.welcome.head: Iniciar Sesión
login.vitual.keyboard: Escriba su contraseña usando el teclado virtual.

# Employee Form
employee.employeecode: Solapín

Aquí hay tres id:
login.welcome.head
login.vitual.keyboard
employee.employeecode


//---Fin Dudas-------------------------------------------------------------------------------------------------------------------------

Mis pasos fueron con la tabla Employee de SisCon:

En "src/Didetech/SisconBundle/Form/ScEmployeeType.php" hice lo siguiente:

---- Agregué la siguiente línea al inicio "use Symfony\Component\Translation\Translator;"

---- Dentro de la única clase que hay, en este caso "ScEmployeeType extends AbstractType" agregué:

protected $Translator;

public function setTranslator(Translator $Translator) {
///referenciamos el Translator
$this->Translator = $Translator;
}

public function getTranslator() {
//lo obtenemos
return $this->Translator;
}

--- Luego en la función "buildForm" modifiqué el campo "employeecode" para que se traduzca y en los formularios no se muestre el nombre employeecode sino "Código de Empleado", quedó así:

$builder->add('employeecode', 'text', array('label' => $this->getTranslator()->trans('employee.employeecode', array(), 'DidetechSisconBundle')))

donde:
employee.employeecode -> es el id de la palabra a traducir
DidetechSisconBundle -> es el fichero donde se encuentran las traducciones, en este caso son dos "DidetechSisconBundle.es.yml" y "DidetechSisconBundle.en.yml"

--- Luego nos vamos para el controlador, en este caso el controlador es "src/Didetech/SisconBundle/Controller/ScEmployeeController.php" y en la función "newAction" por ejemplo ponemos lo siguiente para que cuando se muestre el formulario lo haga traducido:

La función antes era así:
public function newAction()
{
$entity = new ScEmployee();
$form = $this->createForm(new ScEmployeeType(), $entity);

return $this->render('DidetechSisconBundle:ScEmployee:new.html. twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}

Pero ahora para que se traduzca el formulario quedaría así:
public function newAction()
{
$entity = new ScEmployee();
$formTranslater = new ScEmployeeType(); //nueva línea agregada
$formTranslater->setTranslator($this->get('translator')); //se instancia la clases "setTranslator" que está en "ScEmployeeType.php"

$form = $this->createForm($formTranslater, $entity); //modificada esta línea.

return $this->render('DidetechSisconBundle:ScEmployee:new.html. twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}

Finalmente ya está, saludos

//--------------------------------------------------------------------------------------------------------------
COMO QUEDÓ TODO AL FINAL
//--------------------------------------------------------------------------------------------------------------

#src/Didetech/SisconBundle/Resources/translations/DidetechSisconBundle.es.yml
# Navigation
navigation.welcome.head: Bienvenido al sistema.
navigation.who.are: Acerca de
navigation.configuration: Configuración
navigation.home: Inicio
navigation.search: Buscar
navigation.developed.for: Desarrollado por

# Login [todo los demás están en el bundle FOSUserBundle]
login.welcome.head: Iniciar Sesión
login.vitual.keyboard: Escriba su contraseña usando el teclado virtual.

# Employee Form
employee.employeecode: Solapín

//--------------------------------------------------------------------------------------------------------------

// src/Didetech/SisconBundle/Controller/ScEmployeeController.php

<?php

namespace Didetech\SisconBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controll er;

use Didetech\SisconBundle\Entity\ScEmployee;
use Didetech\SisconBundle\Form\ScEmployeeType;

/**
* Displays a form to create a new ScEmployee entity.
*
*/
public function newAction()
{
$entity = new ScEmployee();
$formTranslater = new ScEmployeeType();
$formTranslater->setTranslator($this->get('translator'));

$form = $this->createForm($formTranslater, $entity);

return $this->render('DidetechSisconBundle:ScEmployee:new.html. twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}

//--------------------------------------------------------------------------------------------------------------

// src/Didetech/SisconBundle/Form/ScEmployeeType.php

<?php

namespace Didetech\SisconBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Translation\Translator;

class ScEmployeeType extends AbstractType {

protected $Translator;

public function setTranslator(Translator $Translator) {
///referenciamos el Translator
$this->Translator = $Translator;
}

public function getTranslator() {
//lo optenemos
return $this->Translator;
}

public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('employeecode', 'text', array('label' => $this->getTranslator()->trans('employee.employeecode', array(), 'DidetechSisconBundle')))
;
}

public function getName() {
return 'didetech_sisconbundle_scemployeetype';
}

}

Saludos, Victor Cruz ([email protected])