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?
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.
The string's first character is extracted using charAt() method. Here, str. charAt(0); gives j. The toUpperCase() method converts the string to uppercase.
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.
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.
Use PHP's native ucfirst
function:
{{ ucfirst(trans('messages.welcome')) }}
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') :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With