如何处理 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:User***eskeremfoo.comwp-contentthemesfooassetsscriptsfunctions.js让我们假设函数内部存在以下代码。

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

现在的目标是使该英语句子可翻译。 当单击此文件中的按钮时,将执行上述 js 代码。 C:User***eskeremfoo.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;

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

WordPress怎么添加语言WordPress怎么添加语言

类型一、一个独立网站的多种语言在一个独立网站的基础上,使用wordpress多语言插件进行翻译,这样操作方便,但是切换时会造成网站负荷加大。3)PolylangPolylang是WordPress.org上列出的最受欢迎的WordPress翻

如何在 WordPress 中调用 ajax如何在 WordPress 中调用 ajax

这里,wp_ajax_nopriv。在用户未登录时调用,wp_ajax。这里在functions.php中add_actions:在上面添加这个函数,现在这个函数:这里在一些。

Polylang:如何翻译自定义字符串?Polylang:如何翻译自定义字符串?

//polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/pll_register_string允许插件在“字符串翻译”面板

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

phpquery_posts(‘showposts=10&orderby=new’);。phpquery_posts(‘showposts=10&orderby=rand’);。></a></li&gt

如何在wordpress中对the_content()和the_excerpt()设置字符限制如何在wordpress中对the_content()和the_excerpt()设置字符限制

例子:-用于使用函数(用于显示页面的主要内容)用于使用函数(用于显示页面的摘录短内容)用下面的代码替换php。只是为了帮助,如果有人想限制帖子长度..然后可以使用下面的代码来做到这一点..下面的代码只是对先生的修改我知道这篇文章有点旧,但我想

WordPress admin bar 添加自定义链接菜单WordPress admin bar 添加自定义链接菜单

bar中添加自定义链接菜单,您可以在主题的functions.php文件中添加代码。以下是一个示例代码,演示如何添加自定义链接菜单:在上述代码中,表示在admin。在这个示例中,我们添加了一个顶级菜单项,其ID为,标题为“自定义链接”,链接为

wordpress怎么实现文章分页wordpress怎么实现文章分页

underline”>’wp_more’,</SPAN></STRONG></SPAN>。underline”>’wp_more’,’wp_page’,</SPAN></STR

WordPress前台如何显示登录用户的最后登录时间WordPress前台如何显示登录用户的最后登录时间

s’));}add_action(‘wp_login’,’user_last_login’);//。get_last_login($userdata->ID);。