网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

HTML实体的WordPress翻译问题

GG网络技术分享 2025-03-18 16:12 4


问题描述:

I\'m currently trying to translate a text in WordPress. The problem is that I\'m trying to translate HTML characters which seems to be converted during the translation but I need them as they are in the JS I\'m localizing:

wp_localize_script( \'main-js\', \'main_object\', array(

\'homepage_typed_text\' => __( \'Apples & Bananas\', \'ewk\' )

) );

So in the JS it needs to be Apples & Bananas and not Apples & Bananas. When I copy it directly to the JS, everything works but not via the translation. Any ideas why this happens?

Update

I\'ve checked the code and found this little thing here in the localize function:

$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, \'UTF-8\' );

So this function transforms every entity back to his original sign. Damn. So no idea how to skip this..

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在尝试翻译WordPress中的文本。 问题是我正在尝试翻译似乎在翻译过程中转换的HTML字符,但我需要它们,因为它们在JS我正在本地化:</ p>

  wp_localize_script  (\'main-js\',\'main_object\',数组(

\'homepage_typed_text\'=&gt; __(\'苹果&amp; amp;香蕉\',\'ewk\')

));

</ code> </ pre >

所以在JS中它需要是 Apples&amp; amp; 香蕉</ code>而不是苹果&amp; 香蕉</代码>。 当我将它直接复制到JS时,一切都有效,但不是通过翻译。 有什么想法会发生这种情况吗?</ p>

更新</ strong> </ p>

我检查了代码并在此处找到了这个小东西 本地化函数:</ p>

  $ l10n [$ key] = html_entity_decode((string)$ value,ENT_QUOTES,\'UTF-8\'); 

</ code> </ pre >

因此,此函数将每个实体转换回其原始符号。 该死的。 所以不知道如何跳过这个.. </ p>

</ div>

网友观点:

I\'ve found no solution in PHP so I moved to JS:

main_object.homepage_typed_text.replace( /[\\u00A0-\\u9999<>\\&]/gim, function ( i ) {

return \\\"&#\\\" + i.charCodeAt( 0 ) + \\\";\\\";

} );

So replaces the &, ä, ü, etc. back to his entities. If someone has a better solution, let me know it.

标签:

提交需求或反馈

Demand feedback