How to capitalize first letter in Laravel Blade
Asked Answered
D

7

62

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?

Doloroso answered 9/9, 2015 at 21:19 Comment(0)
S
132

Use PHP's native ucfirst function:

{{ ucfirst(trans('messages.welcome')) }}
Seeseebeck answered 9/9, 2015 at 21:20 Comment(3)
Or use ucwords() if you have a string with multiple words and you want each word to be capitalized.Rolan
Also as a side note, I believe it doesn't matter what version of laravel is used because.. ucfirst, lcfirst, ucwords, are all standard php helper functions. lcfirst is lowecase first and came in on php 5.3+ where as ucfirst and ucwords was in php 5.0 i believe.Zared
Sadly, this is not a multibyte solution.. you can use mb_convert_case( $x, MB_CASE_TITLE, 'UTF-8') or CSS like this: .my-class:first-letter { text-transform: capitalize; }Scissel
B
10

Use Laravel helper Str::title()

{{ Str::title('messages.welcome') }}
Blankly answered 16/12, 2021 at 8:18 Comment(2)
This will capitalize all wordsPartlet
yep all first words if more than 1 good for nouns first name last nameBlankly
R
4

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

Reims answered 5/9, 2017 at 9:44 Comment(2)
Watch out with this one as your output will not be escaped this will make you vulnerable to XSS.Unworldly
@Unworldly thanks, but only in certain specific use cases. The blade directive @ lang does not escape as well; this way, you can use html in your language files. The directive is usually used to retrieve text from static language files, so that is not a danger. If you want to show user generated input, you should escape it of course.Reims
P
4

Laravel get the class Str, with a lot of string format functions.

To capitalize first letter of all words :

Str::title($string)

To capitalize first letter of first word :

Str::ucfirst($string)
Partlet answered 2/11, 2022 at 7:33 Comment(0)
K
3

Another way to make capitalize first letter using PHP and blade.

Controller

return view('stock.uk-lse', ['name' => 'djan']);

View

<h1>{{ ucfirst($name) }}</h1>
Kandicekandinsky answered 14/8, 2018 at 5:3 Comment(0)
B
2

I think that the best option is use CSS text-transform property

In your CSS file:

.lowercase {
    text-transform: lowercase;
}
.uppercase {
    text-transform: uppercase;
}
.capitalize {
    text-transform: capitalize;
}

Your blade (html) file:

<p class="lowercase">{{ trans('messages.welcome') }}</p> <!-- This will display welcome -->
<p class="uppercase">{{ trans('messages.welcome') }}</p> <!-- This will display WELCOME -->
<p class="capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->

Or, the best option for me, use bootstrap

<p class="text-lowercase">{{ trans('messages.welcome') }}</p><!-- This will display welcome -->
<p class="text-uppercase">{{ trans('messages.welcome') }}</p><!-- This will display WELCOME -->
<p class="text-capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->
Bali answered 29/4, 2019 at 20:41 Comment(0)
J
2
{{ ucfirst(strtolower(trans('messages.welcome'))) }}

If you use the above code, you can make sure that only the first letter get capitalized even if the string contains more than one capital letters.

Jochbed answered 1/4, 2022 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.