php 如何在 Laravel Blade 中大写第一个字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32489324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:00:43  来源:igfitidea点击:

How to capitalize first letter in Laravel Blade

phplaravelbladelaravel-5.1

提问by user947668

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

我正在使用具有本地化功能的 laravel (5.1) 刀片模板引擎。

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

有一个语言文件messages.php的内部/resources/lang/en/文件夹:

return [
    'welcome' => 'welcome',

In my blade template the welcome message is called using the transmethod:

在我的刀片模板中,使用以下trans方法调用欢迎消息:

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

我该如何解决这个问题?

回答by Joseph Silber

Use PHP's native ucfirstfunction:

使用 PHP 的原生ucfirst函数:

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

回答by Krishnamoorthy Acharya

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

另一种使用 PHP 和刀片使首字母大写的方法。

Controller

控制器

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

View

看法

<h1>{{ ucfirst($name) }}</h1>

回答by Pim

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

在 app/Providers/AppServiceProvider 的 boot() 函数中添加一个刀片指令:

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

你是@lang_u('messages.welcome') :)

回答by Zahit Rios

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

我认为最好的选择是使用 CSS text-transform 属性

In your CSS file:

在您的 CSS 文件中:

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

Your blade (html) file:

您的刀片 (html) 文件:

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