Location: PHPKode > projects > Open Power Template > docs/Opt/syntax.functions.pluralize.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="robots" content="all" />

	<title>pluralize() - Open Power Template</title>
	
	<link rel="stylesheet" type="text/css" href="design/generic.css" media="all"  />
	<link rel="stylesheet" type="text/css" href="design/print.css" media="print" />
	<!--[if lte IE 6]><link rel="stylesheet" href="design/ie.css" type="text/css" /><![endif]-->	
	<!--[if IE 7]><link rel="stylesheet" href="design/ie7.css" type="text/css" /><![endif]-->
</head>
<body>

<div id="wrap">
	<div id="header">
		<h1>Open Power Template 2.0</h1>
		<h2>pluralize()</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="syntax.html">Template syntax</a> &raquo; <a href="syntax.functions.html">Function reference</a> &raquo; <a href="syntax.functions.pluralize.html">pluralize()</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="syntax.functions.html">3.6. Function reference</a><br/>3.6.23. pluralize()</dt><dd class="prev">3.6.22. pad()<br/><a href="syntax.functions.pad.html">&laquo; Previous</a></dd><dd class="next">3.6.24. position()<br/><a href="syntax.functions.position.html">Next &raquo;</a></dd></dl>	<h1>3.6.23. pluralize()</h1><div class="tf_reference"><table><tr><th>Reference</th><td><code>string pluralize(int $number, string $singularForm [, string $pluralForm1, string $pluralForm2 ... ])</code></td></tr><tr><th>Versions</th><td>since <code>2.0.5</code></td></tr>
</table><hr/></div><p>Attempts to pluralize the singular form <code>$singularForm</code> for the given number <code>$number</code>. If the plural forms are not specified, the function asks the installed <a href="guide.i18n.html" title="4.9. Internationalization">translation interface</a> for the plural form. Otherwise it matches one of the specified plural forms according to the rules specified in the <code>$pluralForms</code> OPT configuration option. By default, the rules for English language are installed.</p>

<p>Sample use:</p>

<pre class="xml">{@userCount is 0}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;We have 0 users on our website.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>We have {$userCount} {pluralize($userCount, 'user', 'users')} on our website.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
{@userCount is 1}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;We have 1 user on our website.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>We have {$userCount} {pluralize($userCount, 'user', 'users')} on our website.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
{@userCount is 6}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;We have 6 users on our website.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>We have {$userCount} {pluralize($userCount, 'user', 'users')} on our website.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<h2>Plural forms for other languages</h2>

<p>Different languages may handle plural forms differently, i.e. by using different cases. The default behaviour can be easily reprogrammed on the script side through the <code>$pluralForms</code> configuration option. The option contains an associative list of PHP expressions matching different cases and the grammar forms to be used. The matching number must be encoded as <code>%d</code>. The final element is always taken as the final alternative and is assumed to be true.</p>

<p>Below, we can see an example for Polish language, where for numbers 0 and greater than 4 we have to change the case to genitive:</p>

<pre class="php"><span style="color: #000088;">$tpl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pluralForms</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'%d == 0 || %d &gt; 4'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// The genitive form will be the third on the argument list</span>
    <span style="color: #0000ff;">'%d == 1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Singular form, the first </span>
    <span style="color: #0000ff;">'%d'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>   <span style="color: #666666; font-style: italic;">// In all other cases, use second form</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<p>The same sample template, but for Polish laguage:</p>

<pre class="xml">{@userCount is 0}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;0 użytkowników.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{$userCount} {pluralize($userCount, 'użytkownik', 'użytkownicy', 'użytkowników')}.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
{@userCount is 1}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;1 użytkownik.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{$userCount} {pluralize($userCount, 'użytkownik', 'użytkownicy', 'użytkowników')}.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
{@userCount is 3}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;3 użytkownicy.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{$userCount} {pluralize($userCount, 'użytkownik', 'użytkownicy', 'użytkowników')}.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
{@userCount is 6}
<span style="color: #808080; font-style: italic;">&lt;!-- display: &quot;6 użytkowników.&quot; --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{$userCount} {pluralize($userCount, 'użytkownik', 'użytkownicy', 'użytkowników')}.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>If the option refers to an undefined form, an exception is thrown.</p>

<blockquote class="information">
  <p>On PHP 5.3 the <code>$pluralForms</code> may be a closure which returns the number of argument to be used for the given number.</p>
</blockquote>

<h2>Translation interface</h2>

<p>On multilingual websites, we do not know the number of available grammar forms for each language that might be implemented. Fortunately, if the plural forms are not specified, <code>pluralize()</code> asks the translation interface for the plural form and leaves to the programmer implementing the proper inflection algorithm depending on the currently selected language.</p>

<p>The function expects the translation interface to implement the <code>pluralize()</code> method that takes two arguments: the number and the singular form. The <code>$pluralForms</code> configuration option is ignored then. Note that the method is not required by <code>Opl_Translation_Interface</code> to be implemented. In this case, the function throws an exception.</p>
<h4>See also:</h4><ul><li><a href="guide.i18n.html">4.9. Internationalization</a></li></ul><dl class="location location-bottom"><dt>3.6.23. pluralize()<br/><a href="syntax.functions.html">3.6. Function reference</a></dt><dd class="prev"><a href="syntax.functions.pad.html">&laquo; Previous</a><br/>3.6.22. pad()</dd><dd class="next"><a href="syntax.functions.position.html">Next &raquo;</a><br/>3.6.24. position()</dd></dl>		</div>
	
	<div id="footer">
		<p>Copyright &copy; <a href="http://www.invenzzia.org/">Invenzzia Group 2008-2009</a></p>
		<p>Available under the terms of license: <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License 1.2</a></p>
		<p>Generated by <strong>TypeFriendly 0.1.4</strong> by <a href="http://www.invenzzia.org/">Invenzzia</a></p>
	</div>
</div>

</body>
</html>
Return current item: Open Power Template