在poedit中扫描html文件 [英] Scan html files in poedit

查看:207
本文介绍了在poedit中扫描html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我希望在Poedit中扫描html文件来翻译那里的文本。
我有这样的html文件代码:

 < a href =/ test> _( 翻译我)< / a> 

我想要Poedit扫描这个词翻译我就像他扫描PHP文件而不使用PHP代码
纯html。



请给我有用的答案。



我试着添加 .html .htm 在Poedit首选项中,实际上它不会读取我的单词,
我在电子邮件中询问了Poedit服务中心,他们给我一个自己换班的答案。

解决方案

这是一个复杂的问题。基本答案是Poedit不会解析PHP函数内部的字符串,例如 _(),除非它们位于<?php?> 包装器。您通过在扩展名列表中添加* .html来做正确的事情,但它仍然无法解析<?php?> 标记中未包含的字符串。



我的解决方案是在文件中放置<?php?> 包装器,即使它们会不会被服务器解析或正确渲染,然后使用一些JavaScript在加载时剥离PHP标签。这允许Poedit解析 _()函数调用中的字符串,同时快速剥离丑陋的php标签,然后用户可以看到它们。



下面是我今天解决这个问题的js代码(需要jQuery)。请注意,它没有完全测试,并且几乎肯定需要额外的工作。它目前只支持少量的元素类型,并且只支持剥离 _() __()函数。您必须提供您希望它剥离一个i18n类的元素(以下完整示例):
$ b

  function _get_elem_translatable_string( elem){// Get attr_name attr_name = _get_attr_name(elem); //获取当前可翻译的值if(attr_name =='html'){str = $(elem).html(); } else {str = $(elem).attr(attr_name); } //返回return str;}函数_set_elem_string(elem,str){//获取attr_name attr_name = _get_attr_name(elem); //更新元素if(attr_name =='html'){//为'normal'元素设置html $(elem).html(str); (其他if(attr_name =='value')){//为'value'元素设置值(通常是提交输入)$(elem).val(str); } else {//为其他元素设置attr值$(elem).attr(attr_name,str); function _get_attr_name(elem){//根据元素的标签类型确定将受影响的attr 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'; } //返回返回attr_name;}函数_contains_php_gettext(str){// bool:字符串是一个包含对'echo _()'调用的php标记吗? regexp = _php_regexp(); if(str.match(regexp))return true;}函数_strip_php_gettext(str){//如果字符串是一个包含对'echo _()'的调用的php标签,去掉PHP标签regexp = _php_regexp();如果(str.match(regexp)){//检测定界符是否为撇号或引号标记delim =(str.match(/ echo [\t] * _ \('/)?':匹配(/ echo [\t] * _ \(/)?'':'')); //标签str = str.replace(regexp,$ 2); //脱离转义字符if (delim ==')str = str.replace(/ \\'/,'); if(delim ==''')str = str.replace(/ \\/, ')');} // return return str;}函数_php_regexp(){return / ^ <(! - )* \?php [\t] * echo [\t] * _ \( [''](。*)['] \)[\t;] * \?[ - ] *> / i;} //文档准备就绪时开始$(document).ready(function ){//转换非分析的PHP标签(例如,如果这个页面运行在不运行PHP的服务器上)$('。i18n')。each(function(i,elem){//获得可转换的字符串elem str = _get_elem_translatable_string(elem); //展开PHP,ITIS if(_contains_ php_gettext(str)){//设置_set_elem_string(elem,_strip_php_gettext(str),true,true); 

这将允许你放置以下内容放入.html / .php文件中:



< a href =/ testclass =i18n>< ?php echo _(翻译我); ?>< / a>

用户只会看到:



 < a href =/ testclass =i18n> translate me< / a>  


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.

解决方案

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[ \t]*_\('/) ? "'" : (str.match(/echo[ \t]*_\("/) ? '"' : ''));

        // 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[ \t]*echo[ \t]*_\(['"](.*)['"]\)[ \t;]*\?[-]*>/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);
        }
    });
});

This will allow you to place the following into your .html/.php file:

<a href="/test" class="i18n"><?php echo _("translate me"); ?></a>

But after the js runs the user will only see:

<a href="/test" class="i18n">translate me</a>

这篇关于在poedit中扫描html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆