cakephp: __ function returns array when a term exists as both a singular an plural key

This is a (multiple allowed):

  • bug
  • enhancement
  • feature-discussion (RFC)
  • CakePHP Version: 3.2.8
  • Platform and Target: n/a, I think

I’m running into a problem with the __ function, and I’m not sure whether it’s a bug in Cake, Aura\Intl, or my code. I’ve tried the same thing in Cake 1.3, and it works as I expect it to, but it’s possible that my expectations are simply that way because that’s how it worked in 1.3. 😃

What you did

When I am building my menus, I use things like __('Teams'), but I also have pages that use things like __n('Team', 'Teams', count($player->teams)). The i18n shell extracts these into the default.pot separately, so when I translate it to French, it’s like this:

msgid "Teams"
msgstr "Équipe"

msgid "Team"
msgid_plural "Teams"
msgstr[0] "Équipe"
msgstr[1] "Équipes"

Expected Behavior

__('Team') should return ‘Équipe’ and __('Teams') should return ‘Équipes’.

Actual Behavior

If I call __('Team'), I correctly get ‘Équipe’ returned, and if I call __n('Team', 'Teams', $x), I correctly get ‘Équipe’ or ‘Équipes’ returned, depending on the value of $x. But if I call __('Teams') I get back

Array
(
    [0] => Équipe
    [1] => Équipes
)

This is the case even if I eliminate the msgid "Teams" section, leaving only the plural definition.

In Cake 1.3, __('Teams') would simply return ‘Équipes’. (Don’t know what it might do in 2.x, as I skipped over that entirely.) So, whose bug is this? Posted this on StackOverflow, and it was suggested that it looks like a bug to be reported here.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28 (28 by maintainers)

Commits related to this issue

Most upvoted comments

I think the question is to what extend CakePHP should stick to the behavior of gettext (the behavior most people are used to), which handles the exact same situation gracefully, ie it treats identical msgid and msgid_plural as separate IDs, that is gettext('Teams') will return Équipe and won’t touch the plurals (best tested using a different message to figure where exactly the message stems from), which will only be looked up when using ngettext.

No, we should fix this bug. I think you are talking about i18n things from the view point of English speakers. But, for example, Japanese language doesn’t have grammatical number. If a Japanese user developed an application, and if he want to translate it into English, the PO file would be like the following:

msgid "チーム"
msgid_plural "チーム"
msgstr[0] "Team"
msgstr[1] "Teams"

How does he avoid collisions? It would mean that any Japanese users cannot translate their applications into other languages right now.

But I understand the difficulty in implementing. Probably we would need to refactor I18n features.

If ‘Teams’ exists as a msgid, we should be try to honor and reflect that when __() is used in my opinion.

The only sensible thing would be to throw an exception or an error in that case. Since you cannot call a translation for plurals without specifying how many there are of the object