• Resolved Max

    (@wppunk)


    Howdy, Loco support.

    If I use an _n* functions with static text as detecting a singular or plural as an example _n( 'Krya 1', 'Krya 2', 1, 'krya' ) it works well. But if in the code a have the same string translated using another translate function then the _n function broken.

    Works good:

    
    _n( 'Krya 1', 'Krya 2', 1, 'krya' );
    _n( 'Krya 1', 'Krya 2', 2, 'krya' );
    

    Works bad:

    
    __( 'Krya 1',  'krya' );
    __( 'Krya 2', 'krya' );
    _n( 'Krya 1', 'Krya 2', 1, 'krya' );
    _n( 'Krya 1', 'Krya 2', 2, 'krya' ); // Broken
    
    • This topic was modified 4 years, 6 months ago by Max.
    • This topic was modified 4 years, 6 months ago by Max.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Tim W

    (@timwhitlock)

    Firstly: all of your examples above have a syntax error. No closing single quote after 'Krya 1. I’ll assume this is just an error on your post and not the issue?

    String extraction is designed to work only with static text. using a dynamic value such as another translation as source text will not work. However your examples don’t actually show this, so you’ve lost me there.

    Thread Starter Max

    (@wppunk)

    @timwhitlock I’ve updated the example. So can I use an _n* functions to detect singular/plural?

    Plugin Author Tim W

    (@timwhitlock)

    Your last two broken examples are now identical to your working examples.

    What exactly are you reporting as broken?

    Thread Starter Max

    (@wppunk)

    Yep, exactly. Just try to add a translation to some language. For example, I use the Russian language. In the admin area:
    https://a.supportally.com/PsnOwg
    https://a.supportally.com/4zPaxx
    https://a.supportally.com/RTp1x2

    Code:

    
    esc_html_e( 'Krya 1',  'krya' ) . '<br>';
    esc_html_e( 'Krya 2', 'krya' ) . '<br>';
    echo esc_html( _n( 'Krya 1', 'Krya 2', 1, 'krya' ) ) . '<br>';
    echo esc_html( _n( 'Krya 1', 'Krya 2', 2, 'krya' ) ) . '<br>'; // Broken
    

    Current result:

    
    Ne krya 1
    Ne krya 1
    Ne krya 1
    Krya 2
    

    Expected behavior of my code:

    
    Ne krya 1
    Ne krya 2
    Ne krya 1
    Ne krya 2
    
    • This reply was modified 4 years, 6 months ago by Max.
    Plugin Author Tim W

    (@timwhitlock)

    Please post the generated PO file.

    I need a direct download of the file from your site.

    Thread Starter Max

    (@wppunk)

    I’ve created a PO file from the Loco plugin.

    Plugin Author Tim W

    (@timwhitlock)

    So post it

    Thread Starter Max

    (@wppunk)

    @timwhitlock I seeing what these translate created incorrectly. I think using my example is impossible.

    krya.pot:

    
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: Krya\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2020-09-09 13:01+0000\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: \n"
    "Language: \n"
    "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "X-Generator: Loco https://localise.biz/\n"
    "X-Loco-Version: 2.4.3; wp-4.9.15"
    
    #. Name of the plugin
    msgid "Krya"
    msgstr ""
    
    #: krya.php:12 krya.php:14 krya.php:15
    msgid "Krya 1"
    msgstr ""
    
    #: krya.php:13
    msgid "Krya 2"
    msgid_plural "Krya 2"
    msgstr[0] ""
    msgstr[1] ""
    
    Plugin Author Tim W

    (@timwhitlock)

    The problem here is that you are using both a singular and a pluralized message with the same unique key (Krya 1).

    The plural string is not part of the unique key. This is baked into the MO file format and you can see that in WordPress here. Note that only the singular string and message context are used to look up the translation.

    However, I note that running the command line xgettext program gives a different result to Loco Translate:

    e.g. xgettext -LPHP -kesc_html_e:1 -k_n:1,2 krya.php -o-

    Produces:

    #: krya.phps:12 krya.phps:14 krya.phps:15
    msgid "Krya 1"
    msgid_plural "Krya 2"
    msgstr[0] ""
    msgstr[1] ""
    
    #: krya.phps:13
    msgid "Krya 2"
    msgstr ""

    I will look into why Loco Translate doesn’t behave the same way.

    Plugin Author Tim W

    (@timwhitlock)

    Ok there is definitely a bug. When the string extractor encounters a singular message key and then later encounters it again as a pluralized one, the plural forms get combined wrongly.

    This is a bit of an edge case, but I will fix it in the next release. I will post back here when the development branch has been patched.

    Plugin Author Tim W

    (@timwhitlock)

    I’ve updated the Dev branch with a fix for the issue extracting plurals that are also used as singulars.

    I can’t say if this fixes any display issues you’re having, but the POT generation of your example code now matches that of the xgettext output.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Incorrect work with _n* functions’ is closed to new replies.