Internationalization. Call a Action Controller from javascript and ajax.

Hi folks. I’am building a Little aplications with internationalization.

First, I have the files with the string /messaje/es.php, ca.php, en.php

Second, also I have in the ControllerBase this functions

<?php
// *************************************
// Afetgit per a l'internacionalització

protected function _getTransPath()
{
    $translationPath = '../app/mensajes/';
    $language = $this->session->get("language");
    if (!$language) {
        $this->session->set("language", "es");
    }
    if ($language === 'es' || $language === 'ca' || $language === 'en') {
        return $translationPath.$language;
    } else {
        return $translationPath.'es';
    }
}

public function loadMainTrans()
{
    $translationPath = $this->_getTransPath();
    require $translationPath.".php";
    //Return a translation object
    $mainTranslate = new Phalcon\Translate\Adapter\NativeArray(array("content" => $mensajes));
    //Set $mt as main translation object
    $this->view->setVar("mt", $mainTranslate);
}

 //public function setLanguageAction($language='')     Per quees crdat des de index.volt event del onchange del select i cride al Controller amb un ajax
public function setLanguageAction()
    {
        $this->flash->success("Entro");
        if ($this->request->isPost() && $this->request->isAjax()) {
            $this->flash->success("Entro");
        };
        $languaje = $request->getPost('paramLlengua');
        //Change the language, reload translations if needed
        if ($language === 'es' || $language === 'ca' || $language === 'en') {
            $this->session->set('language', $language);
            $this->loadMainTrans();
        }
        //Go to the last place
        $referer = $this->request->getHTTPReferer();
        if (strpos($referer, $this->request->getHttpHost()."/")!==false) {
            return $this->response->setHeader("Location", $referer);
        } else {
            return $this->dispatcher->forward(array('controller' => 'index', 'action' => 'index'));
        }
    }
// **************************************

After, I have in the view index.volt a select for select the lenguaje that the user need, with the event onchange where exists a call for javascript “callActvSetLanguaje(this)”.


                            <!-- Per al select dels idiomes -->
                            <ul class="navbar-right margenTop">
                                <!--<select id="llengua" class="selectpicker form-control colorDesplegableSelect" data-style="btn-primary">-->
                                <select id="llengua" class="selectpicker" data-style="btn-primary" onchange="callSActvSetLanguaje(this)">
                                    <option>
                                        <?php
                                            $msg =  $mt->_("Castella");
                                            echo $msg;
                                        ?>
                                    </option>
                                  <option>
                                        <?php
                                            $msg =  $mt->_("Angles");
                                            echo $msg;
                                        ?>
                                  </option>
                                  <option>
                                        <?php
                                            $msg =  $mt->_("Catala");
                                            echo $msg;
                                        ?>
                                  </option>
                                </select>
                            </ul>    

And finally, the javascript function is the next, where I’m calling the action controller setLanguaje with Ajax.

<script>
            function callSActvSetLanguaje(selectObject){
                var value = selectObject.value; 
                switch(value){
                    case 'Català':
                        llengua = 'ca';
                        break;
                    case 'Castellano':
                        llengua = 'es';
                        break;
                    case 'English':
                        llengua = 'en';
                        break;
                    default:
                        llengua = 'es';
                }
                $.ajax({
                    url: '/ControllerBase/setLanguage',
                    type: 'POST',
                    data: llengua,
                    dataType: 'text',
                    success: function(data){}
                });
            }
        </script>

But, the internationalization not is executed.

Can you see where is the fail? I think that the fail is in the Ajax call!!.

Happy weekend!!!!!



37.0k

Hi,

url: '/ControllerBase/setLanguage',

did you have a look at your browser debug console ? I'm quiet sure it should tell you that it can't find the requested url

Convention over configuration: controller's functions that are accessible throught controllerName/actionName/params must be written in ControlerNameController.php

<=> you can't access to setLanguageAction in ControllerBase.php

edited 20d ago

Well, the URL is fixed:

And then, about of your last coment : *you can't access to setLanguageAction in ControllerBase.php,

I will not be able to access any action despite being public???



37.0k

hi,

it seem's that you've removed one of your response .... (I've got two notifications in my mail box, but there is only one response here in the forum) ....

I will not be able to access any action despite being public

yes, convention over configuration.

if you want to call thisismycontroller/thisismyaction/params, that means :

  • controller should be written in ThisismycontrollerController.php
  • action should be written in public method thisismyactionAction($params)

so, regarding your problem:

  • you can keep function _getTransPath() and function loadMainTrans() in ControllerBase
  • create a new controller such as TranslationController that extend ControllerBase with function setLanguageAction() in it
  • change url in javascript: url: 'translation/setLanguage'

that should do the trick

edited 19d ago

GoodMorning, thanks again. Yes, I'm not khown this rulle for ControlleBase, but is logical, many logical this encapsulation.

I'll make another controller that extend ControllerBase and put it my functions like setLanguaje in this new controller. When I'ill have solved, I'll post the final solution.

Thanks for your time