php-translation / symfony-bundle

Symfony integration for Translations
MIT License
327 stars 94 forks source link

[EditInPlace] Add a default controller to enable / disable the editor #59

Open damienalexandre opened 7 years ago

damienalexandre commented 7 years ago

We may ship a small default controller to enable and disable the feature when using the default Activator.

This must be optional (maybe some commented out lines in the routing file?).

Nyholm commented 7 years ago

Tell me more. You want to enable and disable this in runtime. How would you store the data if it is enable or disabled?

damienalexandre commented 7 years ago

I would like to add this in the Bundle:

    /**
     * @Route("/on", name="edit_on")
     */
    public function onAction()
    {
        $this->container->get('php_translation.edit_in_place.activator')->activate();

        return $this->redirectToRoute('homepage');
    }

    /**
     * @Route("/off", name="edit_off")
     */
    public function offAction()
    {
        $this->container->get('php_translation.edit_in_place.activator')->deactivate();
        return $this->redirectToRoute('homepage');
    }

At the moment, I tell end user to call this them-self, and adding those action could be convenient.

This does not impact the storage which is the session at the moment, using the default Activator.

damienalexandre commented 7 years ago

The redirectToRoute part is why I didn't do it in the first place.

Maybe just a new paragraph in the documentation is enough. What to you think?

Nyholm commented 7 years ago

Okey. I misunderstood. I thought you wanted to disable it for all users. Im 👍

Maybe you can redirect the user back to where he/she came from?

rvanlaak commented 7 years ago

What about adding some sort of toggle to the translation block in the webprofiler toolbar itself?

damienalexandre commented 7 years ago

We could, but it's only working if the feature is used in "dev" environment; which should not be the case from my point of view, as this feature is designed for non technical users :nerd_face:

rvanlaak commented 7 years ago

Yes I agree, but I think it does not have to influence the original feature request. See it as addition ;)

Good to know: we will only use it in non-prod / debug environments, we want to both translate via the "edit in place" for the twig views and "profiler" for the controller translations.

rvanlaak commented 7 years ago

A toggle for this could also be added to the Web UI.

sergiotropea commented 6 years ago

My solution working fine:

Controller Function:

    /**
     * Attivazione dello strumento di Traduzione
     * @Route("/{_locale}/translation" , name="translation_action")
     * @IsGranted("ROLE_ADMIN")
     * @IsGranted("ROLE_TRADUTTORE")
     * @IsGranted("ROLE_ASSISTENTE")
     * @Method("GET")
     */
    public function translationAction(Request $request){

        $session = $this->get('session');
        if (!$result = $this->get('php_translation.edit_in_place.activator')->checkRequest($request)) {
            $session->set('translation',1);
            $this->get('php_translation.edit_in_place.activator')->activate();
        }
        else {
            $this->get('php_translation.edit_in_place.activator')->deactivate();
            $session->set('translation',0);
        }

        return $this->json(array('status' => 'SUCCESS', 'result' => $result));

    }

I set session variable translation. In Twig:

{% if app.request.session.get('translation') == 1 %}
      <a class="navbar-item bord-right-smoke padding-top-bottom-none" href="javascript:setTranslation()"><i class="fas fa-ban"></i> </a>
{% else %}
      <a class="navbar-item bord-right-smoke padding-top-bottom-none" href="javascript:setTranslation()"><i class="fas fa-edit"></i> </a>
{% endif %}

finally a easy jquery to reload the same page:

function setTranslation() {
      $.get('{{ app.request.locale }}/translation');
      location.reload();
}

I hope to help you