How to use localization in Blade tag templates? - laravel

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

Related

Laravel Stop SSL Redirect on FORM Helper

I have a web form in Laravel that is behind an ip. Example:
127.0.0.1/posts/create
With a simple form helper:
{{ Form::open(array('url' => '/posts','files'=>'true')) }}
#include('posts._form')
{!! Form::submit('Create Post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
When I inspect the html, https is added to the url. How can I stop this?
Found the problem, my nginx had the HTTPS variable set on. Laravel apparently reads that variables and adds the protocol + host_name to all redirect actions.

Not allowing code to excute

In Laravel 5, using {{ code }} will escape code and show it as text, but if the code is already encoded say HTML, and is displayed inside the {{ }} it will execute, is there a way for this not to happen?
Use {!! $your_code !!} instead of {{ }}
If you do not want your data to be escaped, you may use the following syntax: {!! $text !!} to display html
https://laravel.com/docs/5.5/blade#displaying-data

Form blade, can't understand translating from blade to html

I have this form in blade:
{{ Form::open(['action' => ['SearchController#searchUser'], 'method' => 'GET']) }}
{{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
{{ Form::submit('Search', array('class' => 'button expand')) }}
{{ Form::close() }}
How can I translate it into html?
If you're using Laravel 5.2, you must install HTML and Form as module.
follow these steps and it must be rendered your code:
https://laravelcollective.com/docs/5.2/html
If you are new to Laravel and feel its confusing with all those syntax you can use normal HTML that you already know. Its not a compulsion to use Laravel's blade shortcut.So something like
<form action="SearchController#searchUser" method="get">
//All your inputs
</form>
And to use form in your project you have to get a facade. You can follow the tutorial below :
http://www.easylaravelbook.com/blog/2015/02/09/creating-a-contact-form-in-laravel-5-using-the-form-request-feature/

laravel Blade {{ }} or {!! !!} statement

On laravel documentation, they use {{ }} but not {!! !!} for Blade statement.
But some people said they prefer !! over {{.
What's the reason of using !!?
{{ Escaped variable }}
{!! Unescaped variable !!}
Quote from the documentation:
By default, Blade {{ }} statements are automatically sent through
PHP's htmlentities function to prevent XSS attacks. If you do not want
your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}
{{ }} will escape all special characters to prevent xss attacks, meanwhile {!! !!} will give You raw results.
{{ "<script>alert('hi');</script>" }} == <script>alert('hi');</script>
{!! "<script>alert('hi');</script>" !!} == <script>alert('hi');</script>
Please read docs
By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
Though be extra careful while using the later.

Handling conditions in blade templates on laravel 4 in a form input

I would like to test the existence of value in blade on Laravel 4.
Like:
{{ Form::text('name', #if(isset($value)) {{$value}} #endif; }}
I tried this:
{{ Form::text('name', #if(isset($value)) $value #endif; }}
You can do it in one line.
I'd prefer a better approach, using condition?if:else , which also I use in my daily projects:
{{ Form::text('name',isset($value)?$value:'') }}
or even:
{{ Form::text('name',isset($value)?$value:null) }}
Anywhere before that, you can add the line <?php if(!isset($value)) { $value= ''; } ?>. Then you can proceed as usual {{ Form::text('name', $value) }}

Resources