Laravel locale translate paragraph - laravel

I'd like to ask if this is the correct way to translate a story:
'story' => 'This is a story about <br> peanut butter. <br> Peanut butter tastes good.'
Should I use <br> tags like above or is there another way?
Thanks,
g3

You could make "story" an array.
For example:
'story' => [
'This is a story about',
'peanut butter',
'Penut butter tastes good'
];
Then in your code:
#foreach($story as $paragraph)
{{ $paragraph }}<br>
#endforeach
This way also supports unlimited paragraphs, which can be an issue across translation barriers.

This is implemented in my website.
#foreach (trans('menus.benefits_menu') as $key => $translation)
<a href="{{ route('benefits', ['slug' => $key]) }}" class="{{ ($key == $uri) ? 'active' : ''}}">
<div class="name">{{ $translation }}</div>
</a>
#endforeach
You can see it working here: http://www.metinet.co/benefits/cloud_technology
(as the left hand menu)

Related

Laravel blog rating 2 migrations

i made a blog in laravel and now created a new migration for a rating system. i call it with
#foreach($blogs as $key => $blog)
$blog->title....
#foreach($ratings as $key => $rating)
#if( $rating->blog_id == $blog->id)
<br> rating found!
<br> {{ $rating->rating_total }}
<br> {{ $rating->rating_amount }}
#endif
#endforeach
#endforeach
it works but it doesnt seem right to run a for loop on every blog post in order to get the rating.
how can i solve this better?
thanks
You could use one that show this. Like this: <input id="input-1" name="input-1" class="rating rating-loading" data-min="0" data-max="5" data-step="0.1" value="{{ $post->averageRating }}" data-size="xs" disabled="">
Since you have the relationship setup from Blog to Rating you can eager load the 'rating' relationship for each Blog post:
$blogs = Blog::with('rating')->...->get();
Then in Blade when you are looping your $blogs you will have access to it's rating:
#foreach($blogs as $blog)
{{ $blog->title }}
#if($blog->rating)
{{ $blog->rating->rating_total }}
{{ $blog->rating->rating_amount }}
#endif
#endforeach

how do I insert a different icon for each widgets

how do I insert a different icon for each widgets?
[
'type' => 'progress_white',
'class' => 'card mb-2',
'progressClass' => 'progress-bar bg-primary',
'value' => $userCount,
'description' => 'Utenti registrati.',
'progress' => (int)$userCount/10*100, // integer
'hint' => 'Numero di utenti registrati al portale.',
],
the html part is this this part below
<div class="{{ $widget['wrapperClass'] ?? 'col-sm-6 col-md-4' }}">
<div class="{{ $widget['class'] ?? 'card' }}">
<div class="card-body">
#if (isset($widget['value']))
<div class="text-value">{!! $widget['value'] !!}</div>
#endif
......
Unfortunately there is no way with the default widgets you will ave to create your own.
#if(isset($widget['icon']))
<div class="text-right" style="position:absolute;top:5px;right:15px;font-size: 40px;color: {{ $widget['icon_color'] ?? 'rgba(0,0,0,0.2)' }}">
<span class="{{ $widget['icon'] }}"></span>
</div>
#endif
Put that within the card-body class. Styling might need to be changed a little bit.
Other option is Backpack uses a new theme based on CoreUI. Can look at the documentation for CoreUI, or take a look at the source for the theme at https://backstrap.net
What kind of icons do you want? If you use icon set like material icon or awesome icon, you can pass your icon name and use it in "i" tag like this:
"icon" => "fa fa user"
and in your blade:
<i class"{{$widget['icon']}}"></i>

Laravel best way to call a controller inside a view?

I have a page who contains children blocs.
Each blocs need to have a specific render (with specific template).
For this i had to use #php in my blade template.
This is my code :
PageController.php
public function edit(Page $page)
{
return view('pages.edit', compact('page'));
}
View page/edit.blade.php
<section id="contents" class="contents ui-sortable">
#foreach ($page->blocs as $bloc)
#php
echo $bloc->id;
echo App\Http\Controllers\BlocController::renderBloc($bloc);
#endphp
#endforeach
</section>
BlocController.php
public static function renderBloc(Bloc $bloc) {
echo $bloc->id;
return view('blocs.show.' . $bloc->bloc_type, [
'bloc' => $bloc,
'data' => json_decode($bloc->data)
]);
}
And then an exemple of bloc
resources/views/blocs/show/text.blade.php
#extends('blocs.show')
#section('bloc')
{{ $bloc->id }}
#endsection
resources/views/blocs/show.blade.php
<section class="bloc bloc_{{ $bloc->bloc_type }}" data-bid="{{ $bloc->id }}">
{{$bloc->id}}
#yield('bloc')
</section>
I have 2 problems with this :
I think it's not really a good way to do ? I don't like to use #php in template. I would love to have an opinion about this ? Maybe i need to use a Service Provider ?
The $bloc->id inside the template (resources/views/blocs/show/text.blade.php) is wrong (it shows the id of the first child bloc in the foreach, even if all my echo $bloc->id before display the good id (page/edit/blade.php, BlocController.php, resources/view/blocs/show.blade.php). This is an other proof i'm doing something wrong i guess ?
Thanks
If you have to use controller in your views, it means only that you have not so good architecture. Laravel's Blade easily can do what you try to solve via controller.
You can use #include with parameters and get rid of #php:
resources/views/pages/edit.blade.php
#foreach ($page->blocs as $bloc)
#include('blocs.show', ['bloc' => $bloc])
#endforeach
resources/views/blocs/show.blade.php
<section class="bloc bloc_{{ $bloc->bloc_type }}" data-bid="{{ $bloc->id }}">
{{$bloc->id}}
#include('blocs.show.' . $bloc->bloc_type, [
'bloc' => $bloc,
'data' => json_decode($bloc->data)
])
</section>
resources/views/blocs/show/text.blade.php
Bloc ID = {{ $bloc->id }}
Bloc Text = {{ $data->text }}

Laravel Loop through array

I stuck an something really easy (I guess).
I have an array:
array(30) {
["gericht_1_mo"]=>
string(6) "Nudeln"
["preis_1_mo"]=>
string(4) "5,50"
["gericht_2_mo"]=>
string(64) "Paniertes Schweineschnitzel
mit Pommes frites und Beilagensalat"
["preis_2_mo"]=>
string(2) "13"
["gericht_1_di"]=>
string(75) "Gebratenes Hähnchenbrustfilet auf asiatischer Gemüsesoße mit Basmatireis"
["preis_1_di"]=>
string(4) "4,70"
["gericht_2_di"]=>
string(83) "Geröstete Hörnchennudeln mit Ei, Käse, Speck
und Zwiebeln dazu ein bunter Salat"
["preis_2_di"]=>
string(4) "7,90"
}
and want to loop through the array in my view
#foreach($input as $key => $item)
<div class="col-xs-6" style="width: 100%;">
<h3>Day</h3>
<hr />
<div class="dishContainer">
<h4 class="dishTitle">item</h4>
<p class="dishDesc">{{ $item }}</p>
<p class="priceTag">{{ How caN I display the Price of every item ?}}€</p>
</div>
</div>
#endforeach
After a long time playing around I cant find the solution to output the data properly.
I would argue there is something wrong with your data model. At least, in order to render the data as you would like, I would convert the data before calling the view. Something like:
$output = [];
foreach($input as $key => $value){
if(preg_match('/(preis|gericht)_([0-9]+_[a-z]{2})/', $key, $matches)){
$output[$matches[2]][$matches[1] = $value;
}
}
Mind you, this code is unchecked, so it should probably be tested/corrected before use.
#for ($x = 0; $x < count($input); $x+=2)
<div class="col-xs-6" style="width: 100%;">
<h3>Day</h3>
<hr />
<div class="dishContainer">
<h4 class="dishTitle">item</h4>
<p class="dishDesc">{{ $input[$x] }}</p>
<p class="priceTag">{{ $input[$x+1] }}€</p>
</div>
</div>
#endfor

How to use localization in Blade tag templates?

1st question:
I've inserted the localization in many types of texts and things, but I don't know how to import it into in the following forms:
{{ Form::label('name', 'here') }}
{{ Form::text('name', null, array('placeholder' => 'here')) }}
{{ Form::submit('here', array('class' => 'btn btn-success')) }}
{{ Form::button('here', array('class' => 'btn btn-default')) }}
I want it to be in the form label 'here' and in the placeholder of the text 'here'.
2nd question:
I am not allowed to insert it with links in my language file: text here blah blah BLAH?
Is there anyway to insert it with links?
Thanks in advance.
Supposing that your messages are stored in app/lang/en/message.php you can use the same way for all your cases:
In Blade template:
{{ Form::label('name', Lang::get('message.key')) }}
{{ Form::text('name', null, array('placeholder' => Lang::get('message.key'))) }}
{{ Form::submit(Lang::get('message.key'), array('class' => 'btn btn-success')) }}
In HTML tag mixed with some Blade expression:
BLAH
You can also use localization in Blade templates using strictly Blade syntax.
Given that you have a message in your /app/lang/en/messages.php that corresponds to the key "message_key", you can do :
#lang('messages.message_key')
to render the message in the locale that your application is configured to use.
So, The answers for both of your questions are:-
1) {{ Form::label('name', 'here') }}
Here, you need to change the "here" text hence laravel localization method can be used.For eg:-
{{ Form::label('name', '__("Here")' }} or
{{ Form::label('name', '__('message.here') }}.
2)< a href="{{ URL::to('text') }}">BLAH< /a>
Here, you need to change the label instead of link.
< a href="URL::to('text') ">{{__('message.BLAH')}}< /a>.
This is the simplest which works for me !
{!! Form::label('title', trans('users.addNewRecordsNameFieldLabel'),['class' => 'control-label']) !!}
I feel, already blade parsing is started the moment {!! or {{ is started
A possible answer to your second question:
You could set up a language file resources/lang/en/page.php like this:
return [
'sentence' => 'A sentence with a :link in the middle.',
'link_text' => 'link to a another page'
];
And use it in a Blade template like this:
{!! trans('page.sentence', [
'link' => '' . trans('page.link_text') . ''
]) !!}}
The result would be:
A sentence with a link to a another page in the middle.
You alse can use __() method
{{ __('app.name') }}

Resources