tik*_*rht 21 javascript php wordpress
我有一个wordpress应用程序,<?php _e('foo', 'bar') ?>当我需要回显需要翻译的东西时,我通常会使用PHP函数.但是,现在我正在实现一个新功能,在我的.js文件中我有类似的东西
var confirmation = confirm("Are you sure you want to quit");
if(confirmation){
...
}
Run Code Online (Sandbox Code Playgroud)
上面代码的问题是我不能使用PHP函数_e()来翻译它,因为这是一个JS脚本.
反正有没有为JS中回应的文本翻译?
我提出了一个赏金,因为给出的问题是通用的,而我正在寻找可以解决我的问题的解决方案.
我正在研究之前由某人建造的WP项目.我应该只对存在于名为functions.jspath的js文件中的代码添加一个转换:C:\Users\meskerem\foo.com\wp-content\themes\foo\assets\scripts\functions.js让我们假设函数内部存在以下代码.
var confirmation = confirm("Are you sure you want to quit");
if(confirmation){
...
}
Run Code Online (Sandbox Code Playgroud)
现在的目标是使英语句子可以翻译.单击此文件中的按钮时,将执行上述js代码.
C:\Users\meskerem\foo.com\wp-content\plugins\wp-jobhunt\templates\dashboards\candidate\templates_ajax_functions.php
触发翻译的html代码非常简单:
<h1> <?= _e('good morning', 'jobhunt') ?> </h1>
<div> <i class='icon-trash' onclick="askConfirmation()"> x </i> </div>
Run Code Online (Sandbox Code Playgroud)
所以,脚本很简单,但翻译是我遇到问题的地方.
Ash*_*tel 10
在word press中你必须将翻译数组传递给相应的java脚本.
例如,
如果你是function.php文件中的如下队列脚本,
wp_enqueue_script( $handle, $src, $deps,$ver,$in_footer );
Run Code Online (Sandbox Code Playgroud)
你必须通过在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);
Run Code Online (Sandbox Code Playgroud)
在你的情况下
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);
Run Code Online (Sandbox Code Playgroud)
然后你可以访问js中的翻译,比如
var confirmboxmessage = langvars.messagekey;
var confirmation = confirm(langvars.messagekey);
Run Code Online (Sandbox Code Playgroud)
您应该使用wp_localize_script函数,因为这个原因,该函数已添加到WordPress中.
试试这样的事情:
wp_localize_script( $handle, $name, $data );
Run Code Online (Sandbox Code Playgroud)
例
<?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' );
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式访问JavaScript中的变量:
<script>
// alerts 'Some string to translate'
alert( object_name.some_string);
</script>
Run Code Online (Sandbox Code Playgroud)
注意:生成的JavaScript调用中的数据将作为文本传递.如果您尝试传递整数,则需要调用JavaScript parseInt()函数.
<script>
// Call a function that needs an int.
FinalZoom = map.getBoundsZoomLevel( bounds ) - parseInt( object_name.a_value, 10 );
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1340 次 |
| 最近记录: |