簡體   English   中英

如何在 Laravel Blade 中大寫第一個字母

[英]How to capitalize first letter in Laravel Blade

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

/resources/lang/en/文件夾中有一個語言文件messages.php

return [
    'welcome' => 'welcome',

在我的刀片模板中,歡迎消息使用trans方法調用:

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

在某些情況下,我需要顯示相同的消息,但首字母大寫(“歡迎”)。 我不想在翻譯文件中使用重復的記錄。

我該如何解決這個問題?

使用PHP的本機ucfirst函數:

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

將blade指令添加到app / Providers / AppServiceProvider的boot()函數:

public function boot() {

    Blade::directive('lang_u', function ($s) {
        return "<?php echo ucfirst(trans($s)); ?>";
    });

}

這樣,您可以在刀片文件中使用以下內容:

@lang_u('messages.welcome')

哪個輸出:歡迎

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

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

調節器

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

視圖

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

我認為最好的選擇是使用CSS text-transform屬性

在您的CSS文件中:

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

你的刀片(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 -->

或者,對我來說最好的選擇,使用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 -->

使用 Laravel 助手Str::title()

{{ Str::title('messages.welcome') }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM