如何使用php将gettext复数形式存储在变量中 [英] How to store gettext plural form in a variable using php
问题描述
我是PHP的新手.我刚刚开始研究它.
I'm new to PHP. I just started working on it.
我正在尝试使用gettext
和PHP获得复数形式.我的代码如下:
I'm trying to get the plural form using gettext
and PHP. My code is as follows:
printf (ngettext ("%d product", "%d products", 2), 2);
输出:
2 products
但是,我想将此o/p包含在变量中.当我尝试将其指定为:
But, I want to have this o/p in a variable. When I try to give it as:
$totalItemsCount = printf (ngettext ("%d product", "%d products", 2), 2);
echo "Total count msgs::",$totalItemsCount;
输出:
10
我错过了什么吗?
推荐答案
printf()
显示一个字符串,sprintf()
返回它.但是,printf()
有一个返回值-它返回字符串长度.在这种情况下,结果字符串为2 products
,该字符串的长度为10
.
printf()
displays a string, sprintf()
returns it. However, printf()
has a return value - it returns the string length. In this case, the resultant string is 2 products
, and the length of this string is 10
.
来自printf()
的文档:
From the documentation for printf()
:
返回值:
返回输出字符串的长度.
Returns the length of the outputted string.
如果要将输出存储在变量中,请改用 sprintf()
.
If you want to store the output in a variable, use sprintf()
instead.
$totalItemsCount = sprintf (ngettext ("%d product", "%d products", 2), 2);
这篇关于如何使用php将gettext复数形式存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!