What's the equivalent function of i18n t() function in Laravel? - laravel

In i18next, people are defining t() function as translation utility, vue has vue-i18n, react i18n also has such thing.
Here comes my question, what's the equivalent t() function in Laravel?

In Laravel you can use __ helper function for the same.
As per Laravel documentation:
You may retrieve lines from language files using the __ helper function. The __ method accepts the file and key of the translation string as its first argument. For example, let's retrieve the welcome translation string from the resources/lang/messages.php language file:
echo __('messages.welcome');
echo __('I love programming.');
If you are using the Blade templating engine, you may use the {{ }} syntax to echo the translation string or use the #lang directive:
{{ __('messages.welcome') }}
#lang('messages.welcome')
Reference:
Laravel -> Localization -> Retrieving Translation Strings

trans('messages.welcome'), trans_choice('messages.apples', 10), and #lang('messages.welcome')
TO display in the blade wrap those in {{ }} like {{ trans('messages.welcome') }}
To display html contained in them in blade use {!! !!} like {!! trans('messages.welcome') !!}
https://laravel.com/docs/master/localization
https://laravel.com/docs/master/localization#pluralization
https://laravel.com/docs/master/localization#retrieving-translation-strings

Related

Use Carbon Model on laravel blade the path error

when I use the carbon on my blade
localhost:8000/todo.blade.php
I use
{{ Carbon::parse($chunk->time)->diffForHumans()}}
But when I use the cabon on my blade
localhost:8000/todo/todolist.blade.php
I must use
{{ Carbon\Carbon::parse($chunk->time)->diffForHumans()}}
why the url is different and how can I use the same code on the any location
just like
{{ Carbon::parse($chunk->time)->diffForHumans()}}
Or can I use some code like asset()??
{{ asset(Carbon::parse($chunk->time)->diffForHumans())}}
If you want use {{ Carbon::parse($chunk->time)->diffForHumans()}} in any location you need register Class Aliases. Go to config\app.php and add to existing aliases new one:
'aliases' => [
//...other aliases
'Carbon' => \Carbon\Carbon::class
];
Now you can use Carbon instead of \Carbon\Carbon everywhere.

Blade templating conditional encoding apostrophes

Using Lumen 5.4.5.
I'm attempting to use a Blade templating conditional to only display a value if it is populated. Here is my syntax:
{{ ($board->a1 != '') ? "a1: '$board->a1'" : '' }}
Should render like this in browser:
a1: 'wR'
Instead it's rendering the HTML encoding string like this:
a1: 'wR'
How can I successfully render the uncoded apostrophes in my Blade conditional?
You can avoid the the call to htmlspecialchars automatically done with {{ }} using {!! !!} instead. https://laravel.com/docs/5.4/blade

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

Blade templating engine - custom tags

I've seen some mentions of custom tags for the blade templating engine, but can't find an extensive documentation for blade.
Is there anything similar to {{ book.name|capfirst }} from the django templating engine in blade?
Is twig a nice alternative that could suit my needs?
If you want to make first character uppercase, use
{{ ucfirst($variable) }}
You can create a custom blade directive. Register it in the App\Providers\AppServiceProvider's boot method:
public function boot()
{
Blade::directive('ucfirst', function($string) {
return "<?php echo ucfirst($string); ?>";
});
}
Then in your blade views you can do:
<h1> #ucfirst($myString) </h1>

Get image name from database in laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

Resources