Automatic translation does not work in Laravel 5.2 - php

After changing the locale name and then print {{ Config::get('languages')[App::getLocale()] }}, it's giving me the locale name i.e. Italiano or Français. But it does not translate the website text into changed locale!
I am using following article for the localization Laravel 5.2 localization also tried Laravel Localizationbut same also there.
From my understanding, it should translate the language automatically!
Need some expert help from the community.
Sample code from my view file:
#extends('layouts.app')
#section('content')
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ Config::get('languages')[App::getLocale()] }}
</a>
<ul class="dropdown-menu">
#foreach (Config::get('languages') as $lang => $language)
#if ($lang != App::getLocale())
<li>
{{$language}}
</li>
#endif
#endforeach
</ul>
</li>
<h2>Welcome to my website</h2>
<p>Please find your desired content by using website search function.</p>
#endsection

Are you expecting the text elements to translate? You need to use the trans() function and put your copy text in translation files.
EG
<h2>{{trans('welcome_message')}} </h2>
Then stick the copy in your language file

You are getting languages from nowhere, so if you want it to work, copy/paste following array in your app.php
'locales' => [
'en' => 'English',
'ka' => 'Georgian',
'ru' => 'Russian',
],
And if you call Config::get('app.locales') you will get this locales.
Here is how your code must look like:
#extends('layouts.app')
#section('content')
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ Config::get('app.locales')[App::getLocale()] }}
</a>
<ul class="dropdown-menu">
#foreach (Config::get('app.locales') as $lang => $language)
#if ($lang != App::getLocale())
<li>
{{$language}}
</li>
#endif
#endforeach
</ul>
</li>
<h2>Welcome to my website</h2>
<p>Please find your desired content by using website search function.</p>
#endsection

Related

Missing required parameters for route issue in laravel

In my laravel based application I have following link in my admin.blade.php
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="{{ route('cms.home.create') }}" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>{{ __('Home Page') }}</p>
</a>
</li>
</ul>
In my project I have another blade called, create.blade.php which is in the following path
views/cms/home/create.blade.php
I have a controller called, CmsHomeController.php for that blade
In CmsHomeController I have a method called create
public function create()
{
return view('cms.home.create');
}
Once the user clicks on the above mentioned link in the admin.blade.php, user should go to the create.blade.php blade.
And in my web.php I have registered my route as follows,
Route::resource('cms.home','CmsHomeController');
But now the issue is,
When I click on that link in admin blade, I'm getting an error saying
Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: cms.home.create] [URI: cms/{cm}/home/create]. (View: C:\xampp\htdocs\mylaravelproject\resources\views\layouts\admin.blade.php)
In create.blade.php , I'm having just a simple form
Where am I doing wrong and what would be the correct fix?
UPDATE:
I tried running
php artisan route:list
This is what I got
I don't have such a param called, 'cm'..
As you are using dot (.) operator in your resource route it will generating nested routes.
You need to change your route name with single name like cms-home or cms_home
Then you can simple use it as:
Route::resource('cms_home','CmsHomeController');
In your blade you can call it:
<a href="{{ route('cms-home.create') }}" class="nav-link">
Please have look at this referance
Your route need a parameter (cm)
You should have in your template href="{{ route('cms.home.create', ['cm' => $id_cm]) }}"
If you need the route be cms.home, try this:
Route::resource('cms.home', 'CmsHomeController', [
'parameters' => ['cms' => 'cms']
]);
Just Pass the value in your route cms.home.create like this.
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="{{ route('cms.home.create', ['cm' => $value]) }}" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>{{ __('Home Page') }}</p>
</a>
</li>
</ul>

On every click link changes in blade view

This is the blade view to create the url links
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection
when i click for the first time it's working fine and the url I get is
http://localhost/shopnew/public/glassfilm/Frosted
when I click for the secon time the url I get is
http://localhost/shopnew/public/glassfilm/glassfilm/Frosted
what I am doing wrong?
Change your route as below :
#section ('category')
<li class="main-nav-list">
<a href="{{ route('glassfilm-category',['name' => $category->category]) }}"> {{ $category->category }}
<span class="number">{{ $category->count}}</span>
</a>
</li>
#endsection
asset() method is used to include CSS/JavaScript/images files.
url() method used to generate a URL to a link.
Laravel's route() method is very helpful. So you can try:
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection

Symfony redirect to a part of page

I have a symfony page composed of 3 parts ( and i'm using tags to display each part) by navigation menu here is the navigation menu.
<ul class="nav nav-tabs centered">
<li class="active">
{{ 'fiche_pharmacie'|trans }}
</li>
<li>
{{ 'Mon équipe'|trans }}
</li>
<li>
{{ 'Configuration_automate'|trans }}
</li>
{# <li>
{{ 'Services'|trans }}
</li> #}
</ul>
After that i call 3 twig pages to display each page content.
<div class="tab-pane" id="fiche_patient">
{{ include(':prof/Entreprise:indexdetails.html.twig') }}
</div>
<div class="tab-pane" id="my_team2">
{{ include(':prof/Entreprise:my-team.html.twig') }}
</div>
<div class="tab-pane" id="config">
{{ include(':prof/Entreprise:config.html.twig') }}
</div>
My probleme is after submit i would like to display config.html.twig page.
In my controler i tried :
return $this->redirect($this-> generateUrl('entreprise_index'.'#config'));
but it doesn't work
Any one of you have an idea ?
The correct syntax for what you are trying to do there is:
return $this->redirect($this->generateUrl('entreprise_index').'#config');
You can use redirectToRoute()
$yourRouteParams = array('param1'=>1);
return $this->redirectToRoute('entreprise_index', $yourRouteParams);
You can send the tab name as a parameter to twig. And, in the template, set the active tab according to that parameter.

laravel breadcrumbs not rendering html in Laravel 5.4

Within a Layout
#section('breadcrumbs', Breadcrumbs::render('messages'))
#section('content')
#include('layouts.breadcrumbs')
breadcrumbs.blade.php
<div class="fluid-container">
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('breadcrumbs')
</div>
</div>
</div>
</div>
Standard BS3 view with DaveJamesMillar Breadcrumbs
#if ($breadcrumbs)
<ol class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if ($breadcrumb->url && !$breadcrumb->last)
<li>{{ $breadcrumb->title }}</li>
#else
<li class="active">{{ $breadcrumb->title }}</li>
#endif
#endforeach
</ol>
#endif
Appeared to be working fine until upgrading to L5.4, now rather than displaying the breadcrumbs it displays non-processed HTML
<ol class="breadcrumb"> <li>Home</li> <li> class="active">Messages</li></ol>
After reading the latest docs for davejamesmillar laravel-breadcrumbs with support for L5.4 https://media.readthedocs.org/pdf/laravel-breadcrumbs/latest/laravel-breadcrumbs.pdf with reference to 1.4.2 using Blade Sections nothing appears to have changed in the way this needs to be coded. Unsure why the HTML is not being processed to display as a link.
RAR, hours later! Appears Laravel 5.4 runs a htmlentities when injecting a variable into a #section
I changed
#section('breadcrumbs', Breadcrumbs::render('messages'))
to
#section('breadcrumbs') {!! Breadcrumbs::Render('messages') !!} #endsection
And the html is now being processed and displayed as it should.

laravel 4 - how to show and hide components in blade?

I have breadcrumbs in my master.blade.php file and i want the breadcrumbs to be used everywhere BUT the homepage.blade.php.
right now this is how i add links to the breadcrumbs in other pages like "About".
about.blade.php:
#section('breadcrumbs')
#parent
<li class="last-breadcrumb">
About
</li>
#stop
in the master.blade.php:
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
but i don't want the breadcrumbs code to display at all when homepage.blade been used.
copying the code for each about.blade/X.blade files seems like a bad idea..
Your can set a value in your controller that you pass with the make/redirect like $data['breadcrumb'] = true and wrap your breadcrumb code in an conditional. This kind of system also works well for error and success messages since you can alse send content from your controller. Then you would send an array with values instead of true.
Blade template
<div class="container">
#if($breadcrumb)
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
#endif
</div>
You can check the URI to see if you want to display it or not. It's logic in your view which is usually frowned upon, but it's the easiest way I can come up with.
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#if(Route::uri() == '/')
<li class="last-breadcrumb">
About
</li>
#endif
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
Depending on the URI you are using for your home page, you may need to tweak the condition.
If you use #yield in your master template then it will only display content when the section has been defined. #content requires the section to be always defined.

Categories