Reputation: 39
I have a problem with my advanced template when I try to change page using internationalization. I try to explain. To use the internationalization on my site I have follow this steps:
In params.php, I have added this:
<?php
return [
'adminEmail' => '[email protected]',
'languages' => [
'en' => 'English',
'it' => 'Italian',
],
];
I have added this in my frontend\views\layouts\main.php to call on my website the languages insert above:
<?php
foreach(Yii::$app->params['languages'] as $key => $language){
echo '<span class="language" id ="'.$key.'">'.$language.' | </span>';
}
?>
after I have created a new file named main.js. To permit to yii2 to see the main.js file I added this in the AppAsset.php (is it correct?). In this file I insert this:
$(function(){
$(document).on('click','.language', function(){
var lang = $(this).attr('id');
$.post('index.php?r=site/language', {'lang':lang},function(data){
location.reload();
});
});
$(document).on('click','.fc-day', function(){
var date = $(this).attr('data-date');
$get('index.php?r=event/create',{'date':date},function(data){
$('#modal').modal(show)
.find('#modalContent')
.html(data);
});
});
$('#modalButton').click(function(){
$('#modal').modal(show)
.find('#modalContent')
.load($(this).attr('value'));
});
});
after that in the sitecontroller.php I have added this:
public function actionLanguage()
{
if(isset($_POST['lang'])){
Yii::$app->language = $_POST['lang'];
$cookie = new Yii\web\cookie([
'name'=>'lang',
'value'=>$_POST['lang']
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
}
}
after in config\main.php I have added this in components:
'components' => [
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
//'basepath' => @app/messages,
'sourceLanguage' => 'it',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
],
],
finally i have create on the root a folder called messages\it and messages\en and inside two files (one per folder) called app.php I have insert this text:
<?php
return [
'Benvenuto' => 'Welcome'
];
that's all...but after load my home page (correctly) when i click on one of the languages (Italian or English) I see the following messages:
jquery.js?ver=1.11.3:4 POST http://localhost/mdf/frontend/web/index.php?r=site/language 400 (Bad Request)
And If I try to paste directly this url, I obtain only a blank page: http://localhost/mdf/frontend/web/index.php?r=site/language
Upvotes: 1
Views: 2319
Reputation: 133360
This is only a partial response to your question
The blank page when you invoke using browser could be related to the fact that you don't render nothings in this case.
try manage the ajax this way
public function actionLanguage()
{
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
if(isset($data['lang'])) {
Yii::$app->language = $data['lang'];
$cookie = new Yii\web\cookie([
'name'=>'lang',
'value'=>$_POST['lang']
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
return;
}
} else {
return $this->render('index', []);
}
}
Upvotes: 1