How to capitalize first letter in Laravel Blade

北城余情 提交于 2020-01-01 01:10:45

问题


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?


回答1:


Use PHP's native ucfirst function:

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



回答2:


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




回答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>



回答4:


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 -->


来源:https://stackoverflow.com/questions/32489324/how-to-capitalize-first-letter-in-laravel-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!