• The best practice for internationalisation of output that includes expressions is to wrap it in sprintf. For example:

    <?php
    $output = sprintf(
       // Translators: 1 = subject, 2 = adjective.
       __("The %s is %s.", $this->plugin_name)
       $subject, $object
    );
    ?>

    Is there a best practice way to do this in Javascript? I could use replace() fairly easily, but I wanted to know if there was a canonical solution.

    Thanks,

    Ian

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Translation resources are strictly server side, they are unavailable client side. What you can do is determine what phrases your JS needs ahead of time, and translate them as usual in PHP. Then pass the translated strings to JS by using wp_localize_script(). This is exactly the intended application for this function. It seems that it’s more often used for passing other PHP values besides translations.

    Thread Starter Ian McDonald

    (@drianmcdonald)

    Thanks, I should have said that I already knew that part of the answer, but was asking specifically about when there’s a variable in the middle of the translatable string. Obviously, I can just use replace( target, with, string), but was wondering if there’s anything translators are already used to dealing with.

    Moderator bcworkz

    (@bcworkz)

    This brief section on JavaScript pretty much sums up the situation. Basically what you just said. The idea is to have all the heavy lifting done ahead of time in PHP. When it comes to piecing together elements in JS, do whatever works.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Equivalent of sprintf for il18n on Javascript’ is closed to new replies.