Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize first letter in Laravel Blade

I'm using laravel (5.1) blade template engine with the localization feature.

There is a language file messages.php within the /resources/lang/en/ folder:

return [     'welcome' => 'welcome', 

In my blade template the welcome message is called using the trans method:

{{ trans('messages.welcome') }} 

In some cases I need to show the same message but with first letter capitalized ("Welcome"). I don't want to use duplicate records in the translation file.

How can I approach this?

like image 368
user947668 Avatar asked Sep 09 '15 21:09

user947668


People also ask

How do you capitalize the first letter in Laravel?

The ucfirst helper method is used to uppercase the first letter of a string. It defines only one parameter— $string —which is the string that should have its first letter upper cased.

How do you get the first letter of a string Capital?

The string's first character is extracted using charAt() method. Here, str. charAt(0); gives j. The toUpperCase() method converts the string to uppercase.

How can we capitalize just the first letter in a word Javascript?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you make the first letter capital in SAS?

In SAS you can use the LOWCASE function to convert a string to lowercase. Likewise, you can use the UPCASE function to convert a string in uppercase. With the UPCASE function, you can make a string proper case, i.e. the first letter of each word is in uppercase while the remainder is in lowercase.


2 Answers

Use PHP's native ucfirst function:

{{ ucfirst(trans('messages.welcome')) }} 
like image 68
Joseph Silber Avatar answered Oct 04 '22 08:10

Joseph Silber


Add a blade directive to the app/Providers/AppServiceProvider's boot() function:

public function boot() {      Blade::directive('lang_u', function ($s) {         return "<?php echo ucfirst(trans($s)); ?>";     });  } 

This way you can use the following in your blade files:

@lang_u('messages.welcome') 

which outputs: Welcome

 

You're @lang_u('messages.welcome') :)

like image 23
Pim Avatar answered Oct 04 '22 07:10

Pim