Laravel default auth module translation - laravel-5

I have generated the default Laravel auth module.
Everywhere in the blades of the module, I see Double Underscore __ function assuming that translation is almost there.
for example
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('Login') }}
</a>
</li>
My question: Where is the translation file? Where should I put it, if I create one?
I mean, if I go to the Laravel documentation site there are examples like this
echo __('messages.welcome');
with explanations
For example, let's retrieve the welcome translation string from the resources/lang/messages.php language file:
BUT in my example above there is no file name specified. It is only text:
__('Login')
Question is: What is used for the language file if no file specified? Is there any default? Where does it sit? Where was it set?

All the language translation files in Laravel should be stored in PROJECT_DIRECTORY/resources/lang. When you make an Auth with artisan, it automatically creates it. But if you can't find it, then create manually.
(1)
There's a way to using translation strings as keys by the docs. In this method you can create a JSON file in PROJECT_DIRECTORY/resources/lang with the name of your local, for example for Spanish name it es.json or German de.json, it depends on your local name.
Now create a JSON object and put the translations with the string name you used in your blade:
{
"Login": "Welcome to Login Page!",
"Logout": "You are logged out!",
}
Then use the PHP double underscores method to call your translations in blades:
{{ __('Login') }}
(2)
Create a file named auth.php in PROJECT_DIRECTORY/resources/lang directory. then put a simple php array like this on it:
<?php
return [
/*
Translations go here...
*/
];`
Then add your translate strings to it:
<?php
return [
'Login' => 'Welcome to Login Page!',
'Logout' => 'You are logged out!',
];`
Now in the blade template simply do this:
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('auth.Login') }}
</a>
</li>

Laravel Docs
Have an instruction about the json file. Yes it is not php, but json file. Example would be:
resources/lang/es.json
content
{
"I love programming.": "Me encanta programar."
}
Usage
echo __('I love programming.');

It looks like there are no translation file for the default __('Login'), __('Register'), ... provided by laravel.
By default if no translation is found for __('foobar'), laravel just uses the string in the parentheses. So here, assuming there is no translation file, __('Login') is expanded to 'Login'.

Related

Is there a way to remove the %20 in Laravel URL?

Here's what I have so far.
in blade template
<a href='{{ url("/businessprofile/$business->id/$business->name") }}'>
and in web.php
Route::get('/businessprofile/{id}/{name}', 'BusinessController#show')
it shows
localhost:8000/businessprofile/User%20Info
is there a way to remove the %20 and just show localhost:8000/businessprofile/UserInfo instead?
The Str::slug method generates a URL friendly "slug" from the given string :
{{ url("/businessprofile/$business->id"."/" . Str::slug($business->name)) }}'>}}
Or,
{{ url("/businessprofile/$business->id"."/" . str_slug($business->name)) }}
If above method not work, then change your route & view as :
route :
Route::get('/businessprofile/{id}/{name}', 'BusinessController#show')->name('businessprofile.show');
view :
{{ route('businessprofile.show', ['id' => $business->id, 'name' => str_slug($business->name) ]) }}
See official documentation here
No, you should not do that. That's url encoding and ‰20 is code for space ( ).
change your url as below.
<a href="{{ url('businessprofile/'.$business->id.'/'.$business->name) }}">
Thank you for sharing your answers. I've tried mixing everyone's answer but the best I got is this
<a href='{{ url("/businessprofile/$business->id"."/".Str::slug($business->name)) }}'>
and it shows
http://localhost:8000/businessprofile/1/user-info

Laravel 5.4^ - How to customize notification email layout?

I am trying to customize the HTML email layout that is used when sending notifications via email.
I have published both the mail and notification views.
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
If I modify the /resources/views/vendor/notifications/email.blade.php file, I can only change the BODY content of the emails that get sent. I am looking to modify the footer, header, and every other part of the email layout as well.
I tried also modifying the views inside /resources/vendor/mail/html/, but whenever the notification gets sent, it is not even using these views and instead uses the default laravel framework ones.
I am aware I can set a view on the MailMessage returned by my Notification class, but I want to keep the standard line(), greeting(), etc. functions.
Does anyone know how I can get my notifications to send email using the views in /resources/vendor/mail/html ?
The following is my /resources/views/vendor/notifications/email.blade.php file, but it does not have anywhere to customize the header/footer/ overall layout.
#component('mail::message')
{{-- Greeting --}}
#if (! empty($greeting))
# {{ $greeting }}
#else
#if ($level == 'error')
# Whoops!
#else
# Hello!
#endif
#endif
{{-- Intro Lines --}}
#foreach ($introLines as $line)
{{ $line }}
#endforeach
{{-- Action Button --}}
#if (isset($actionText))
<?php
switch ($level) {
case 'success':
$color = 'green';
break;
case 'error':
$color = 'red';
break;
default:
$color = 'blue';
}
?>
#component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
#endcomponent
#endif
{{-- Outro Lines --}}
#foreach ($outroLines as $line)
{{ $line }}
#endforeach
<!-- Salutation -->
#if (! empty($salutation))
{{ $salutation }}
#else
Regards,<br>{{ config('app.name') }}
#endif
<!-- Subcopy -->
#if (isset($actionText))
#component('mail::subcopy')
If you’re having trouble clicking the "{{ $actionText }}" button, copy and paste the URL below
into your web browser: [{{ $actionUrl }}]({{ $actionUrl }})
#endcomponent
#endif
#endcomponent
Run this command
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
update for laravel 5.7+
php artisan vendor:publish
and then you will get:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
and then just type in that number in front to publish the file for editing
and then in
/resources/views/vendor/mail/html/
you can edit all the components and customize anything you want.
For example i have edited the sentence "All rights reserved". to "All test reserved" at the bottom of that image inside this file:
/resources/views/vendor/mail/html/message.blade.php
and this is what i got:
Make sure to have the right configuration in your config/mail.php :
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
]
],
I wrote an article on how to create a notification and modify your template including the header and footer.
It includes the explanation on how the Laravel components work and how to pass your data to a new email template.
https://medium.com/#adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c
The most important part is placing the following code inside your email template:
#component('mail::layout')
{{-- Header --}}
#slot('header')
#component('mail::header', ['url' => config('app.url')])
Header Title
#endcomponent
#endslot
{{-- Body --}}
This is our main message {{ $user }}
{{-- Subcopy --}}
#isset($subcopy)
#slot('subcopy')
#component('mail::subcopy')
{{ $subcopy }}
#endcomponent
#endslot
#endisset
{{-- Footer --}}
#slot('footer')
#component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
#endcomponent
#endslot
#endcomponent
You can check the medium article in case you want more details on how the components work and how to properly pass the data.
#Brian
You can just make change to the #component directives in your template file to use your custom templates. For example:
Replace #component('mail::message') with #component('vendor.mail.html.message'), assuming your template is located at /resources/views/vendor/mail/html/message.blade.php
I ended up just using a custom view rather than trying to get the built in Laravel ones to work.
I added the following use statement to my Notification class
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
Then in the toMail method:
public function toMail($notifiable)
{
$view_file = 'emails.teamInvitation';
$view = View::make($view_file, ['sender' => $this->sender, 'invitationToken' => $this->invitationToken, 'team' => $this->team ]);
$view = new HtmlString(with(new CssToInlineStyles)->convert($view));
return (new MailMessage)
->subject('PreSource Invitation From ' . $this->sender->name )
->view('emails.htmlBlank', ['bodyContent' => $view]);
}
emails.teamInvitation is my actual email template.
I compile the view in to a string, and then convert the stylesheets to be inline.
emails.htmlBlank is a view file but all it does is echo out bodyContent. This is necessary because the MailMessage->view method expects a view file, and not an HtmlString.
Do NOT do what is suggested here.
This works. Just remember that you should edit the templates contained in the 'vendor/mail/html' folder AND NOT the contents of the 'vendor/mail/markdown' folder, unless of course you are using markdown instead of the line() / greeting() email building functions
Instead, run the artisan commands and then edit the generated files in your resources folder that you end up with. Never overwrite the vendor files, as if you are working on a local version, then push it to a live server and run composer install, you will not have those changes anymore.
Laravel's inheritance allows you to easily overwrite pre-defined methods and files, so take advantage of that for cleaner version control and better ability to roll back changes to core functionality.
You are making email based on component #component('mail::message') This is a default and this is only one described in documentation. This component does not allow you to modify header. However if you look into it's file,
\vendor\laravel\framework\src\Illuminate\Mail\resources\views\markdown\message.blade.php
you will see that it uses another component #component('mail::layout'),
Just copy content of message.blade.php file into your .blade.php and replace {{ $slot }} with what you had in your file before.
And now you have all the flexibility in your file.
Plus
if you want to modify styles, go to file \config\mail.php
and change markdown section like so
'markdown' => [
'theme' => 'default0',
'paths' => [
resource_path('views/vendor/mail'),
base_path('resources/views/emails/vendor'),
],
],
In this case I replaced default theme with my own \resources\views\emails\vendor\html\themes\default0.css
or, if you don't want customising paths - put your default0.css into /resources/views/vendor/mail/html/themes - it is a default path and you don't need to mention it.
Tested on Laravel 5.7
Laravel 5.8
I found email layout in file -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php.
Like I don't use markdown to send my emails, i need of layout default of laravel (yes, because i want :)).
What i did? I sent for me email for me of reset password, saved the email like html and then copied html to my editor and it ready to changes \o/.

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]) }}

How to create multi language menu in Laravel 5

What is the best way to create a main menu in Laravel 5? And how to show menu items only when an user is logged-in? And What is the best way to make this multi language?
Laravel provides an easy way to check if the user is logged-in by using the facade Auth::check().
if (Auth::check()) {
// The user is logged in...
}
About the translation, you can check here: Localization
The structure is defined like this, as per documentation:
/resources
/lang
/en
messages.php
/es
messages.php
Laravel also provides an easy way to translate phrases using the trans('string.to.translate'), which can be seen here trans().
Inside messages.php (in both lang directories), you must set the translation string. In en/messages.php:
return [
'welcome' => 'Welcome'
];
In es/messages.php:
return [
'welcome' => 'Bienvenido'
];
With these two, you can do the following in you application for example:
// Get the user locale, for the sake of clarity, I'll use a fixed string.
// Make sure is the same as the directory under lang.
App::setLocale('en');
Inside your view:
// Using blade, we check if the user is logged in.
// If he is, we show 'Welcome" in the menu. If the lang is set to
// 'es', then it will show "Bienvenido".
#if (Auth::check())
<ul>
<li> {{ trans('messages.welcome') }} </li>
</ul>
#endif
Try with https://laravel.com/docs/5.1/authentication
For example:
if (Auth::check()) {
return something to view
}

Laravel: Render queried Data whith Blade functions

Currently I'm getting some data from the database and after that I want to render it within my Blade template.
In my queried data I have blade functions like url('/foo') combined with some html. And here is the problem.
When I'm using {!! $child->description !!} the HTML is rendered correctly, but my Blade function won't work:
Function: url('/foo)
Output: http://myurl.de/url('/foo')
When I'm using the "normal" Syntax like {{ $child->description }} the generated URL is correct (http://myurl.de/foo), but the HTML is not rendered.
So my question is:
How can I use my queried Blade function within rendered HTML? ^^
/Edit
Okay, perhaps my question is too abstract. So I want to show you my problem based on my example. (generated template image - only on german, sorry)
Every form is a database entry like:
categoryName
categoryParent
...
categoryDescription
As you can see on my image, the categoryDescription is the small text under my first input field with the small button.
I want to use this script abstract as possible so that I can fill the entry with every content I want to fill in.
In this case my content is:
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="url('foo')">dolor</a>
As you can see there is the mentioned Blade-function (url) and the HTML.
{!! !!} - this dont escapse string so if u have something from database like,
something it would output it like that.
While in other hand {{ }} this would give you just "something" without , it is used to protect you from injections.
Maybe blade error.{{}}
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="{{url('foo')}}">dolor</a>
Laravel Blade

Resources