Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <a href="{{ URL::to('text') }}">BLAH</a>?

Is there anyway to insert it with links?

Thanks in advance.

like image 265
erm_durr Avatar asked Aug 13 '13 23:08

erm_durr


People also ask

How do I set localization in Laravel?

Localization feature of Laravel supports different language to be used in application. You need to store all the strings of different language in a file and these files are stored at resources/views directory. You should create a separate directory for each supported language.

What is @lang in Laravel?

Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.

How do I get current locale in Laravel?

To fetch current locale you should use App::getLocale() or App::isLocale('...') . Localization. You can also use app()->getLocale() which in Blade would be {{ app()->getLocale() }} .

How can I change local language in Laravel?

Laravel's gives you the option to change the locale for a single Http Request by executing the setLocale method on App Facade App::setLocale($locale); , But what if once the language is changed you don't want to worry about setting the Locale and this should be taken care by an automatic code logic.


2 Answers

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:

<a href="{{ URL::to(Lang::get('message.key')) }}">BLAH</a>
like image 101
Rubens Mariuzzo Avatar answered Nov 15 '22 11:11

Rubens Mariuzzo


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.

like image 20
voitenkos Avatar answered Nov 15 '22 11:11

voitenkos