Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello I want scan html files in Poedit to translate the text there. I have code in html file like this:

<a href="/test">_("translate me")</a>

and I want the Poedit scan the word translate me like he scan PHP file without using PHP codes only pure html.

Please give me helpful answer. I really stuck I can't translate my template.

I tried to add .html, .htm in Poedit preferences and actually it does not read my words, I asked Poedit service center in email and they give me a "shift for oneself" answer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is a complicated problem. The basic answer is "Poedit will not parse strings inside of PHP functions such as _() unless they are within <?php ?> wrappers". You did the right thing by adding *.html to the extensions list, but it still will not parse strings that are not contained within <?php ?> tags.

My solution to this is to place <?php ?> wrappers in the file, even if they will not be parsed by the server or render correctly, and then use some javascript to strip the PHP tags at load. This allows Poedit to parse the strings within the _() function calls, while quickly stripping away the ugly php tags before the user can see them.

Below is the js code I put together to solve this problem today (requires jQuery). Please be aware that it is not fully tested, and almost assuredly needs additional work. It only supports a small handful of element types currently, and only supports stripping of _() and __() functions. You must give elements that you want it to strip a class of i18n in order for this to work (complete example below):

function _get_elem_translatable_string(elem) {
    // Get attr_name
    attr_name = _get_attr_name(elem);

    // Get current translatable value
    if (attr_name == 'html') {
        str = $(elem).html();
    }else{
        str = $(elem).attr(attr_name);
    }

    // Return
    return str;
}
function _set_elem_string(elem, str) {
    // Get attr_name
    attr_name = _get_attr_name(elem);
    
    // Update the element
    if (attr_name == 'html') {
        // Set html for 'normal' elements
        $(elem).html(str);
    }else if (attr_name == 'value') {
        // Set value for 'value' elements (typically a submit input)
        $(elem).val(str);
    }else{
        // Set attr value for other elements
        $(elem).attr(attr_name, str);
    }
}
function _get_attr_name(elem) {
    // Determine attr that will be affected based on tag type of elem
    if ($(elem).is('input') && ($(elem).attr('type') == 'text' || $(elem).attr('type') == 'password')) {
        attr_name = 'placeholder';
    }else if ($(elem).is('input') && $(elem).attr('type') == 'submit') {
        attr_name = 'value';
    }else{
        attr_name = 'html';
    }

    // Return
    return attr_name;
}
function _contains_php_gettext(str) {
    // bool: Is the string is a php tag containing a call to 'echo _()'?
    regexp = _php_regexp();
    if (str.match(regexp))
        return true;
}
function _strip_php_gettext(str) {
    // If the string is a php tag containing a call to 'echo _()', strip to PHP tag
    regexp = _php_regexp();
    if (str.match(regexp)) {
        // Detect if delimieter is apostrophe or quotation mark
        delim = (str.match(/echo[ 	]*_('/) ? "'" : (str.match(/echo[ 	]*_("/) ? '"' : ''));

        // Strip tag
        str = str.replace(regexp, "$2");

        // Strip escape chars
        if (delim == "'")
            str = str.replace(/\'/, "'");
        if (delim == '"')
            str = str.replace(/"/, '"');
    }

    // Return
    return str;
}
function _php_regexp() {
    return /^<(!--)*?php[ 	]*echo[ 	]*_(['"](.*)['"])[ 	;]*?[-]*>/i;
}

// Start when document ready
$(document).ready(function() {
    // Convert non-parsed PHP tags (for instance if this page is running on a server that does not run PHP)
    $('.i18n').each(function(i, elem) {
        // Get translatable string from elem
        str = _get_elem_translatable_string(elem);

        // Strip PHP, ITIS
        if (_contains_php_gettext(str)) {
            // Set
            _set_elem_string(elem, _strip_php_gettext(str), true, true);
        }
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...