如何处理 WP 主题的 js 中文本的翻译

更新于 2022年1月13日 wordpress教程

我有一个 wordpress 应用程序,当我需要回显需要翻译的内容时,我通常使用 PHP 函数<?php _e('foo', 'bar') ?> 。 但是,现在我正在实现一个新功能,在我的.js文件中我有类似的东西

var confirmation = confirm("Are you sure you want to quit"); 
if(confirmation){
 ... 
}

上面代码的问题是我不能使用 PHP 函数_e()来翻译它,因为这是一个 JS 脚本。

无论如何,是否可以对 JS 中回显的文本启用翻译?

我正在研究之前由某人构建的 WP 项目。 我应该只添加对存在于名为functions.js路径的 js 文件中的代码的翻译: C:Usersmeskeremfoo.comwp-contentthemesfooassetsscriptsfunctions.js让我们假设函数内部存在以下代码。

var confirmation = confirm("Are you sure you want to quit"); 
if(confirmation){
 ... 
}

现在的目标是使该英语句子可翻译。 当单击此文件中的按钮时,将执行上述 js 代码。 C:Usersmeskeremfoo.comwp-contentpluginswp-jobhunttemplatesdashboardscandidatetemplates_ajax_functions.php

触发翻译的html代码很简单:

<h1> <?= _e('good morning', 'jobhunt') ?> </h1>
<div> <i class='icon-trash' onclick="askConfirmation()"> x </i> </div>

所以,脚本很简单,但翻译是我遇到一些问题的地方。

解决方案

在 wordpress 中,您必须将翻译数组传递给相应的 javascript。

例如,

如果您在 function.php 文件中使用如下所示的队列脚本,

wp_enqueue_script( $handle, $src, $deps,$ver,$in_footer );

你必须通过在 wp_localize_script() 中使用他的句柄来添加从函数文件到特定 js 的翻译;

  e.g. wp_enqueue_script( 'your-handle', $src, $deps,$ver,$in_footer );

  $translation_array = array('messagekey' => __('Are you sure you want to quit', foo');                             );
  wp_localize_script('your-handle', 'langvars', $translation_array);

在你的情况下

wp_enqueue_script( 'cs_functions_js', plugins_url('/assets/scripts/functions.js', __FILE__ ), '', '', true );

just add below code after above code.

$translation_array = array('messagekey' => __('Are you sure you want to quit', foo');                                );
  wp_localize_script('cs_functions_js', 'langvars', $translation_array);

然后你可以在 js 中访问翻译,例如,

var confirmboxmessage = langvars.messagekey;
var confirmation = confirm(langvars.messagekey);

您应该使用wp_localize_script函数,正是出于这个原因,它被添加到 WordPress 中。

尝试这样的事情:

wp_localize_script( $handle, $name, $data );

例子

<?php

// Register the script
wp_register_script( 'some_handle', '<ENTER YOUR SCRIPT PATH HERE>' );

// Localize the script with new data
$translation_array = array(
    'some_string' => __( 'Some string to translate', 'plugin-domain' ),
    'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

您可以按如下方式访问 JavaScript 中的变量:

<script>
// alerts 'Some string to translate'
alert( object_name.some_string);
</script> 

注意:结果 JavaScript 调用中的数据将作为文本传递。 如果您尝试传递整数,则需要调用 JavaScript parseInt() 函数。

<script>
// Call a function that needs an int.
FinalZoom = map.getBoundsZoomLevel( bounds ) - parseInt( object_name.a_value, 10 ); 
</script>

如果我正确理解了问题,那么您有一个由第三方插件或主题排队的脚本,并且您希望在不修改原始脚本的情况下本地化window.confirm框。

/wp-content/plugins/jobhunt-client-translations/jobhunt-client-translations.php

<?php
/*
Plugin Name: Jobhunt Translations
Author: John Doe
*/

add_action( 'wp_enqueue_scripts', function() {

    wp_enqueue_script( 'translations', plugins_url( '/translations.js', __FILE__ ) );

    // change the translations domain from 'default' to match yours
    // you can also add other translations here in format "message" => "translated message"
    wp_localize_script( 'translations', 'DialogMessages', [ 'Are you sure you want to quit' => __( 'Are you sure you want to quit', 'default' ) ] );

});

/wp-content/plugins/jobhunt-client-translations/translations.js

(function( original ) {
    window.confirm = function( message ) {
        message = DialogMessages[message] || message;
        return original.call( this, message );
    };
})(window.confirm);

/wp-content/plugins/目录中创建新文件夹jobhunt-client-translations ,将这两个文件放入其中,并激活插件。 它将简单地覆盖默认的window.confirm对话框而不更改任何原始第三方文件,并且不修改对话框的默认行为,除了消息将被翻译。

代码已经过测试并且可以正常工作。

也许这会有所帮助

function addScript() {
    wp_enqueue_script( 'functions', get_template_directory_uri() . 'fooassetsscriptsfunctions.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'addScript' );

制作一个简单的 PHP 脚本,您的 JS 可以通过 AJAX 调用,它只是翻译通过 HTTP GET 发送的字符串(或多个字符串)并将其作为响应主体(可能使用 json_encode())回显。

然后你可以创建一个 JS 函数来进行 AJAX 调用,所以调用它就像调用一个 JS 函数一样简单

var confirmTxt = jstranslate('Are you sure you want to quit?');

并以 JQuery 为例:

function jstranslate(string)
{
    translations = $.get('/my-ajax-translate-url',{string: string}, function(e){
        return e.text; // console.log e to double check what to return, this is from memory
    });
}

在 PHP 中

// require_once() your _e() function.
$text = _e($_GET['string'], 'jobhunt');
header('Content-Type: application/json');
echo json_encode(array('text' => $text));
exit;

你可能还喜欢下面这些文章

为什么define(为什么define(“WP_DEBUG”,true); 不显示错误

我在 wp-config 文件中启用了错误:下面的代码插入到您的 wp-config.php 文件中,会将所有错误、通知和警告记录到 wp-content 目录中名为 debug.log 的文件中。

wprec推荐插件模板变量文档以及样式推荐wprec推荐插件模板变量文档以及样式推荐

wpac是一款wordpress自动配图插件,可以丰富文章内容,对提升排名有很大帮助。p style=”font-size:18px;”>你可能还喜欢下面这些文章<p>{excerpt}<

禁用Wordpress的默认的一些小工具禁用Wordpress的默认的一些小工具

主题开发者有时候会自己定制小工具并且在前台显示定制的样式,如果我们不想为wordpress自带的小工具定制样式或者不想我们自定义的小工具淹没在默认的小工具当中,最好移除不需要的小工具。

wprec - wordpress相关文章插件,最好的相似推荐插件wprec – wordpress相关文章插件,最好的相似推荐插件

一个理想的相关文章推荐插件应该是什么样子的?wprec就是一个能够提升用户体验,提升搜索引擎排名的相关文章推荐插件!插件的后台在 WP工具箱-文章推荐,进入即可看到设置。

安全禁用 WP REST API安全禁用 WP REST API

//developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions/#can-i-disable-the-rest-接口据此,Wordpress。

wordpress怎么调用特定文章列表wordpress怎么调用特定文章列表

a href=”< //cat=1为调用ID为1的分类下文章”title=”<

WordPress怎么增加文章排序方式WordPress怎么增加文章排序方式

WordPress怎么增加文章排序方式?实现过程也比较简单,一个是构造链接,另外一个是使用query_posts来改变一下主循环就可以了。 $orderby, ‘order’ =>

wordpress网站怎么设置不可被复制wordpress网站怎么设置不可被复制

原创内容经常被别人轻易复制转载?站长工具箱中自带内容保护插件,可禁止右键和复制功能,使用十分方便。登录WordPress后台,依次点击【外观】-【编辑】,找到footer.php并编辑,在<

好看 (0) 很好看 (0) 非常好看 (0)