Why is msgid_plural necessary in gettext translation files?

I read the GNU Gettext manual on translating multiple forms and see an example of it:

#, 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 mean you have translations that know about multiple forms?

I would think 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 only one file).

Then in my code I would 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 says "3 geese."

If $ numberB is 0, the next line will say: "There are 0 geese on the lake."

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

It seems redundant for me to require 2 msgid to be specified for the same set of phrases.

Thank you for your help!

+1
source share
1 answer

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

A msgid not a "key" that is not read by users; This is a real phrase that can be used in the program. Therefore, when in your code you request a translation for the plural (here pseudocode):

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

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

Why two msgid are used for a multiple line: to show how they are defined in the source program (for translators) and used by default (when there is no translation).

In other localization systems, such as Android String Resources or Rails YAML files , it works as you imagine - the equivalent of msgid is one “key” even for plurals, but then the real phrase is not used in the source code, but the definition of translations - two-step action even for the source language.

+4
source

Source: https://habr.com/ru/post/1240545/


All Articles