Laravel 8 復(fù)數(shù)

2021-07-17 16:53 更新

復(fù)數(shù)是一個復(fù)雜的問題,不同的語言對復(fù)數(shù)有不同的規(guī)則。您使用「管道」符來區(qū)分單數(shù)還是復(fù)數(shù)的形式:

'apples' => 'There is one apple|There are many apples',

您甚至可以創(chuàng)建更復(fù)雜的復(fù)數(shù)規(guī)則,為多個數(shù)字范圍指定翻譯字符串:

'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many', 

在定義了具有復(fù)數(shù)選項(xiàng)的翻譯字符串之后,您可以傳遞「數(shù)量」給 trans_choice 函數(shù)來解析翻譯字符串。此時,如果給定的數(shù)量大于 1 ,將會返回復(fù)數(shù)形式的翻譯字符串:

echo trans_choice('messages.apples', 10);

您亦可在復(fù)數(shù)字符串中定義一個占位符。這些占位符可以被 trans_choice 函數(shù)的第三個數(shù)組函數(shù)替換:

'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',

echo trans_choice('time.minutes_ago', 5, ['value' => 5]);

您可以使用 :count 占位符來在顯示傳遞給 trans_choice 函數(shù)的整數(shù)值:

'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號