Constructing singular (versus plural) language constants Topic is solved

General questions regarding the use of languages in Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Constructing singular (versus plural) language constants

Post by sozzled » Mon Sep 06, 2021 6:11 am

As strange as it may seem for me to be asking a question in the language forum, I would like to know if there's a shorthand way of defining one language constant that will conditionally display a singular form of a word stem.

Traditionally, if you want to define a language constant (for example for the word "orange") in J! you would define two constants (a singular form and a plural form), for example:

Code: Select all

ORANGE_1="I have %d orange"
ORANGE_N="I have %d oranges"
In your PHP source, you would display the singular or plural form of "oranges", conditional upon the result of a some variable, (e.g. $orangeCount), like this:

Code: Select all

    if ($orangeCount == 1) echo '<p>' . sprintf(Jtext::_("ORANGE_1"), $orangeCount) . '</p>';
    else echo '<p>' . sprintf(Jtext::_("ORANGE_N"), $orangeCount) . '</p>';
... and the result would be:
I have 1 orange
if $orangeCount equals 1 or
I have 0 oranges
I have 2 oranges
I have 256 oranges
if $orangeCount equals 0, 2, or 256. Is that the only way to choose between using the singular form of a word stem or is there a simpler way?

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Constructing singular (versus plural) language constants

Post by SharkyKZ » Mon Sep 06, 2021 6:43 am

Use JText::plural():

Code: Select all

echo '<p>' . JText::plural('ORANGE', $orangeCount) . '</p>';
For that the strings need to be suffixed with suffixes defined by the language pack. Recommended suffixes for default en-GB language pack are 0 for 0 items, ONE for 1 item and OTHER for everything else.

Code: Select all

ORANGE_0="I have no oranges"
ORANGE_ONE="I have %d orange"
ORANGE_OTHER="I have %d oranges"
Last edited by SharkyKZ on Mon Sep 06, 2021 7:17 am, edited 1 time in total.

sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: Constructing singular (versus plural) language constants

Post by sozzled » Mon Sep 06, 2021 6:58 am

Ahhh ... https://api.joomla.org/cms-3/classes/Jo ... hod_plural!

OK. I get the picture. You need to define three language constants (there's no shortcut around that) but the coding is simpler with the JText::plural(string, integer) method. Thanks.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Constructing singular (versus plural) language constants

Post by SharkyKZ » Mon Sep 06, 2021 7:25 am

You can do without _0 if you want "I have 0 oranges" shown for 0 items (same as _OTHER).

It is possible to pluralize words programmatically but that would hardly be a shortcut. You'd still need 2 strings and some code that may not work nice with multiple languages.

Code: Select all

I_HAVE="I have %d %s"
ORANGE="orange"

Code: Select all

echo JText::sprintf('I_HAVE', $orangeCount, $orangeCount === 1 ? JText::_('ORANGE') : Joomla\String\Inflector::pluralize(JText::_('ORANGE')));

sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: Constructing singular (versus plural) language constants

Post by sozzled » Mon Sep 06, 2021 7:37 am

Yeah, I understand using the ternary operator as an alternative to an if/else. It's simpler to let JText::plural() do the hard work and define the singular or plural forms of words in the language definition file. I also agree that, in many applications, the "zero" case is unnecessary (unless you prefer to see no instead of 0). Thanks again.


Locked

Return to “Language - Joomla! 4.x”