Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is msgid_plural necessary in gettext translation files?

I've read the GNU Gettext manual about Translating plural forms and see its example:

 #, c-format
 msgid "One file removed"
 msgid_plural "%d files removed"
 msgstr[0] "%d slika je uklonjena"
 msgstr[1] "%d datoteke uklonjenih"
 msgstr[2] "%d slika uklonjenih"

Why is msgid_plural different from msgid, and doesn't that defeat the purpose of having translations be aware of plural forms?

I'd think that I could do something like this (for English):

 #, c-format
 msgid "X geese"
 msgstr[0] "%d goose"
 msgstr[1] "%d geese"

 #, c-format
 msgid "sentence_about_geese_at_the_lake"
 msgstr[0] "There is one goose at the lake."
 msgstr[1] "There are %d geese at the lake."

(using just one msgid).

Then in my code, I'd have something like:

<?php echo $this->translate('X geese', $numberA); ?> 
<?php echo $this->translate('sentence_about_geese_at_the_lake', $numberB); ?> 

If $numberA is 3, it would say "3 geese."

If $numberB is 0, the next line would say "There are 0 geese at the lake."

(because for English, the rule is (n != 1), so plural is used for any number that equals 0 or greater than 1).

It seems redundant for me to be required to specify 2 msgids for the same collection of phrases.

Thanks for your help!

like image 847
Ryan Avatar asked Aug 07 '13 18:08

Ryan


People also ask

What is gettext used for?

In computing, gettext is an internationalization and localization (i18n and l10n) system commonly used for writing multilingual programs on Unix-like computer operating systems. One of the main benefits of gettext is that it separates programming from translating.

How do you comment in a .po file?

Outside strings, white lines and comments may be used freely. Comments start at the beginning of a line with ' # ' and extend until the end of the PO file line. Comments written by translators should have the initial ' # ' immediately followed by some white space.

What are .po files?

A . PO file is a portable object file, which is text-based. These types of files are used in commonly in software development. The . PO file may be referenced by Java programs, GNU gettext, or other software programs as a properties file.

What is a PO file Wordpress?

PO (Portable Object) files Every translator will take the POT file and translate the msgstr sections into their own language. The result is a PO file with the same format as a POT , but with translations and some specific headers. There is one PO file per language.


1 Answers

One of the ideas behind gettext is that the msgid is extracted from source files to create POT files, which are used as base for translations stored in PO files, and later compiled to MO files. A msgid is also used if no suitable translation is found.

A msgid is not a "key" that is non-readable by users; it is a real phrase that can be used in the program. So when in your code you request a translation for a plural (pseudocode here):

ngettext("One file removed", "%d files removed", file_count)

...these two strings will be used a) to extract messages from the source code; these messages will serve as guide for translators b) as the default strings when no suitable translation is found for the current locale.

That's why a plural string has two msgid: to show how they are defined in the source program (for translators) and to be used as default (when no translation exists).

In other localization systems, like Android String Resources or Rails YAML files, it works like you imagined -- the equivalent to a msgid is a single "key" even for plurals, but then the real phrase is not used in the source code, and defining translations is a two-steps action even for the original language.

like image 187
moraes Avatar answered Oct 04 '22 22:10

moraes