Wordpress当前使用gettext
中提供的功能php
但不幸的是不在Javascript
.<我在网上搜索了这件事,并想出了this trick. 但是有一个问题,因为php文件需要通过Wordpress
的系统gettext
要启动的功能
我想知道是否有办法在Wordpress中调用php文件,以便我们可以使用内置函数和变量<如果有人能想出更好的解决方案,那就太好了。
Localiztion in javascript
1 个回复
最合适的回答,由SO网友:Bainternet 整理而成
WordPress主要有一个很好的功能wp_localize_script
要使用它,请首先将脚本排队:
wp_enqueue_script( \'My_Script_handle\', \'path/to/script.js\' );
然后创建要本地化的字符串数组:$data = array(
\'exit\' => __( \'Exit\',\'my-plugin-domain\' ),
\'open\' => __( \'Open\',\'my-plugin-domain\' ),
\'close\' => __( \'Close\',\'my-plugin-domain\' ),
\'next\' => __( \'Next\',\'my-plugin-domain\' ),
\'previous\' => __( \'Previous\',\'my-plugin-domain\' )
);
并使用wp\\u localize\\u脚本调用它wp_localize_script( \'My_Script_handle\', \'mystrings\', $data );
然后,您可以使用JavaScript在页面中访问它,如下所示:alert(mystrings.exit);
alert(mystrings.open);
。。。你明白了。
结束