• Resolved miguel

    (@miguelappstudio)


    I have dynamic text that Loco Translate isn’t recognizing.
    I added the text myself to a plugin file I downloaded from the WordPress repository.
    I ran several different tests; I’ll only include two here to keep it brief.

    This is the first one I tried:

    // We rendered the requested dynamic title.

    echo '<h2 class="product-results-page-title" style="margin-bottom: 30px; text-align: left;">';
    echo sprintf(
    /* translators: 1: cantidad de productos, 2: lista de alérgenos */
    /* translators: %$s: Number of products found */
    _n(
    'This is the %1$s product without %2$s',
    'These are the %1$s products without %2$s',
    $count,
    'product-search-for-woocommerce'
    ),
    '<strong>' . (int) $count . '</strong>',
    '<span class="product-search-name">' . esc_html($list_text) . '</span>'
    );
    echo '</h2>';
    echo '</h2>';


    It doesn’t recognize this one

    // 1. We prepared the text to be translated in isolation so that Loco could see it clearly.

    $texto_singular = 'This is the %1$s product without %2$s';

    $texto_plural = 'These are the %1$s products without %2$s';

    // 2. We obtain the translation (using the $count variable for the plural)

    $mensaje_traducido = _n( $texto_singular, $texto_plural, (int) $count, 'product-search-for-woocommerce' );

    // 3. We render safely

    echo '<h2 class="product-results-page-title" style="margin-bottom: 30px; text-align: left;">';

    echo sprintf(

    $mensaje_traducido,

    '<strong>' . (int) $count . '</strong>',

    '<span class="product-search-name">' . esc_html( $list_text ) . '</span>'

    );

    echo '</h2>';

    It doesn’t recognize this one either.

    What am I doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Tim W

    (@timwhitlock)

    Your second example is incorrect. Dynamic strings are not supported by Loco Translate’s string extractor, as documented, and as per WordPress best practice.

    The first example uses the _n function correctly, and will work with Loco Translate’s string extractor, as long as you’re extracting the product-search-for-woocommerce text domain.

    Hi @timwhitlock

    Is it possible for Loco Translate to recognize all strings if the .po file is edited?



    Thread Starter miguel

    (@miguelappstudio)

    Hi @timwhitlock thanks for your quick reply
    I’ve read your example and the WordPress documentation on this page about how to internationalize your plugin. However, I don’t understand how other sections of the plugin are translated.
    What’s missing from my code to make it useful?
    I tried adding the text domain to both strings, but it didn’t work; the site broke.

    _n(
    __('This is the %1$s product without %2$s', 'product-search-for-woocommerce' ),
    __('These are the %1$s products without %2$s', 'product-search-for-woocommerce'),
    $count,
    'product-search-for-woocommerce'
    ),
    Plugin Author Tim W

    (@timwhitlock)

    Nesting __ function calls inside _n function calls is incorrect. This makes the string arguments dynamic. As per my previous comment, string arguments must be literal.

    I suggest you read the WordPress documentation on i18n. My plugin simply follows those conventions.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.