wordpress如何多语言化Javascript里的条目

在wordpress开发过程中,javascript是我们常用到的语言。不过在做多语言化的时候,往往只能写死代码。这个时候我们就需要用wp_localize_script()来传参,让JavaScript能读取php中的多语言条目,我们以前在如何多语言国际化WordPress项目文章中做了简单的讲解,今天我来具体记录下如何使用。

Wordpress Internationalization

我们只给例子,各位大佬依葫芦画瓢就行了,注意事项我们会在代码中标识出来。

<?php
function ie_load_scripts() {
    //第四行是您的主题或是插件正常加载的js
   wp_enqueue_script('ie-script', plugin_dir_url( __FILE__ ) . 'js/ie-script.js');
   wp_localize_script('ie-script', 'ie_script_vars', array(
         'alert' => __('Hey! You have clicked the button!', 'plugin name'),
         'message' => __('You have clicked the other button. Good job!', 'plugin name')
        )
    );
}
add_action('wp_enqueue_scripts', 'ie_load_scripts');
?>

ie-script.js里需要传递的多语言条目参数。用如下写法

jQuery(document).read(function($) {
   $('#button').on('click', function() {
       alert( ie_script_vars.alert );
   });
   $('#button_2').on('click', function() {
       alert( ie_script_vars.message );
   });
});

本文已在Ie主题99839发布

文章来源:https://ietheme.com/use-wp_localize_script-for-internationalize.html


撰写评论

您的邮箱地址不会被公开。 必填项已用 * 标注

加入我们

注册完成!

密码重置

请输入您的邮箱地址。 您将收到一个链接来创建新密码。

检查你的邮件中的确认链接。


Fatal error: Uncaught Error: Call to undefined function fnmatch() in /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/ObjectCache.php:426 Stack trace: #0 /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/ObjectCache.php(405): RedisCachePro\ObjectCaches\ObjectCache->isNonPrefetchableGroup() #1 /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/Concerns/PrefetchesKeys.php(139): RedisCachePro\ObjectCaches\ObjectCache->isPrefetchableGroup() #2 [internal function]: RedisCachePro\ObjectCaches\PhpRedisObjectCache->RedisCachePro\ObjectCaches\Concerns\{closure}() #3 /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/Concerns/PrefetchesKeys.php(138): array_filter() #4 /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/PhpRedisObjectCache.php(206): RedisCachePro\ObjectCaches\PhpRedisObjectCache->storePrefetches() #5 [internal function]: RedisCachePro\ObjectCaches\PhpRedisObjectCache->close() #6 {main} thrown in /www/wwwroot/ietheme.com/wp-content/plugins/object-cache-pro/src/ObjectCaches/ObjectCache.php on line 426