Translate database content using __($message) function

I’ve written a plugin that allows users to add information in the database. It runs well but I can’t translate the information using WPML.
This is a simple example of the problem:

$message = ‘test’; // here I get the information of the DB
__($message, ‘my-plugin’);

Read More

Is there a way to workaround this problem? I don’t know if we can use a variable instead of a string to translate. When I analize the widget in the Admin panel it don’t show the string to translate.

This example works in WPML because is a string: __(‘test’, ‘my-plugin’);

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. If you want to have variables in your text to be translated the usual way would be with string formatting like so:

    <?php
    printf(__("text %s text2."), $message);
    ?>
    

    with integers:

    <?php
    printf(__("text %d text2."), $count);
    ?>
    

    with more then one placeholders:

    <?php
    printf(__("text %1$s text2 %2$S."), $message, $message2);
    ?>
    

    but the variable it self will not be translated in no way.

    WPML doesn’t run the code before __(); or _e(); function it only search the files for them, and that is why when you write something like this:

    $message = 'test'; // here I get the information of the DB
    __($message, 'my-plugin');
    

    WPML only sees __($message, 'my-plugin'); where $message is empty.