58

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?

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
user947668
  • 2,548
  • 5
  • 36
  • 58

7 Answers7

127

Use PHP's native ucfirst function:

{{ ucfirst(trans('messages.welcome')) }}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 16
    Or use `ucwords()` if you have a string with multiple words and you want each word to be capitalized. – orrd Mar 25 '17 at 16:53
  • 1
    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. – Birdy Mar 28 '17 at 11:00
  • 2
    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; } – SimZal Apr 02 '17 at 11:12
9

Use Laravel helper Str::title()

{{ Str::title('messages.welcome') }}
Amir Khan
  • 401
  • 5
  • 13
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') :)

Pim
  • 5,626
  • 4
  • 19
  • 27
  • 1
    Watch out with this one as your output will not be escaped this will make you vulnerable to XSS. – Maantje Sep 15 '17 at 22:08
  • @Maantje 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. – Pim Sep 18 '17 at 08:48
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>
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 -->
Zahit Rios
  • 111
  • 6
1
{{ 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.

Joyal
  • 2,587
  • 3
  • 30
  • 45
0

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)
Reign.85
  • 2,420
  • 1
  • 28
  • 28