Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress PHP function _n() only translates singular text

Tags:

php

wordpress

I'm in the process of making our Wordpress plugin compatible with translate.wordpress.org and use this code for easy pluralisation/translation:

echo _n( 'size', 'sizes', $count, 'my-domain' );

We still have .po/.mo files that contain e.g.:

msgid "size"
msgstr "afmeting"

msgid "sizes"
msgstr "afmetingen"

This is the output I get:

echo _n( 'size', 'sizes', 1, 'my-domain' );
// expected: 1 afmeting 
// actual: 1 afmeting 

echo _n( 'size', 'sizes', 2, 'my-domain' );
// expected: 2 afmetingen
// actual: 2 sizes

The translations are there, if I reverse the plural/single texts still only the single is translated.

Any thoughts what I'm doing wrong and how I should get it to work?

like image 736
sourcx Avatar asked Oct 18 '22 18:10

sourcx


1 Answers

It seems I was not storing plural translations correctly. I've modified the plugin code to use translate.wordpress.org and the translation is now regarded as one having both a singular and plural version.

The solution for storing it in the .po/.mo files would have been something like this (not tested):

msgid "size"
msgid_plural "sizes"
msgstr[0] "afmeting"
msgstr[1] "afmetingen"

Also see this: Why is msgid_plural necessary in gettext translation files?.

like image 89
sourcx Avatar answered Nov 23 '22 07:11

sourcx