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!!!!!