Home Forums General Issues Wrap labels and descriptions with __() in the PHP Export file


Wrap labels and descriptions with __() in the PHP Export file

    • usaphp

    • August 21, 2013 at 7:30 am

    Hi there, first of all – awesome plugin. Quick quesion: when I do a php export for my settings, is there a way to wrap every field label and description in the outputted php code with a __() for easier translation with POEDIT?

    • Elliot

    • August 21, 2013 at 2:45 pm

    Hi @usaphp

    Yes, I’ll add this into the next version.



    • BaLu

    • September 15, 2013 at 7:37 am

    Weee! I was about to crate post for this topic and now I fount it on To-do.. sweeet!

    Code that I’m using right now:

    // add the __() function to specific strings for translation in theme
    $html = preg_replace("/'title'(.*?)('.+?',)/", "'title'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'label'(.*?)('.+?',)/", "'label'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'instructions'(.*?)('.+?',)/", "'instructions'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'button_label'(.*?)('.+?',)/", "'button_label'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'placeholder'(.*?)('.+?',)/", "'placeholder'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'message'(.*?)('.+?',)/", "'message'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'prepend'(.*?)('.+?',)/", "'prepend'$1__($2 'acf_export'),", $html);
    $html = preg_replace("/'append'(.*?)('.+?',)/", "'append'$1__($2 'acf_export'),", $html);

    The ‘acf_export’ helps out to find and replace it with theme text domain.
    This helps out, but could make some improvements and find a way to add translation functions to choices.

    • BaLu

    • September 16, 2013 at 2:41 am

    Ok I created a new code:

    // add the __() functions for theme translations (by BaLu.LT)
    $ignore_keys = array(
    $ikeys = "";
    foreach ( $ignore_keys as &$ikey ) $ikeys .= "(?<!\'" . $ikey . ")";
    $regular_expression = "/((?<=" . $ikeys . "\' => )\'.+\'(?=,))/";
    $html = preg_replace( $regular_expression, "__($1, 'acf_export')", $html );

    Few things to mention:
    No translation support for ‘default_value’ using it as default text (alternative: placeholder)
    No translation support for choices with identically matched keys to any of $ignore_keys or if it’s an integer

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.