尽管上述库带来了高级本地化能力(最重要的是_n
函数),使用wp_localize_script()
自WordPress 2.2以来,该功能已使用多年。以下是本地化的示例Magnific Popup 脚本(source) 从我的一个网站。首先,您应该更改脚本中所有硬编码的消息(这里是jquery.magnific-popup.js
显示如何执行的脚本):
--- jquery.magnific-popup.js
+++ jquery.magnific-popup.js
@@ -895,9 +895,9 @@
closeMarkup: \'<button title="%title%" type="button" class="mfp-close">×</button>\',
- tClose: \'Close (Esc)\',
+ tClose: mfp_msg.close,
- tLoading: \'Loading...\',
+ tLoading: mfp_msg.loading,
autoFocusLast: true
@@ -974,7 +974,7 @@
options: {
hiddenClass: \'hide\', // will be appended with `mfp-` prefix
markup: \'\',
- tNotFound: \'Content not found\'
+ tNotFound: mfp_msg.content_not_found
},
proto: {
@@ -1047,7 +1047,7 @@
options: {
settings: null,
cursor: \'mfp-ajax-cur\',
- tError: \'<a href="%url%">The content</a> could not be loaded.\'
+ tError: mfp_msg.content_not_loaded
},
proto: {
@@ -1144,7 +1144,7 @@
cursor: \'mfp-zoom-out-cur\',
titleSrc: \'title\',
verticalFit: true,
- tError: \'<a href="%url%">The image</a> could not be loaded.\'
+ tError: mfp_msg.image_not_loaded
},
proto: {
@@ -1677,9 +1677,9 @@
navigateByImgClick: true,
arrows: true,
- tPrev: \'Previous (Left arrow key)\',
- tNext: \'Next (Right arrow key)\',
- tCounter: \'%curr% of %total%\'
+ tPrev: mfp_msg.previous,
+ tNext: mfp_msg.next,
+ tCounter: mfp_msg.current
},
proto: {
接下来,按以下方式使用本地化脚本:
wp_enqueue_script( \'magnific-popup\', get_stylesheet_directory_uri() . \'/assets/lib/magnific-popup/magnific-popup.js\', array( \'jquery\' ) );
$magnific_popup_messages = array(
\'close\' => __( \'Close (Esc)\', \'your-text-domain\' ),
\'loading\' => __( \'Loading...\', \'your-text-domain\' ),
\'content_not_found\' => __( \'Content not found\', \'your-text-domain\' ),
\'content_not_loaded\' => __( \'<a href="%url%">The content</a> could not be loaded.\', \'your-text-domain\' ),
\'image_not_loaded\' => __( \'<a href="%url%">The image</a> could not be loaded.\', \'your-text-domain\' ),
\'previous\' => __( \'Previous (Left arrow key)\', \'your-text-domain\' ),
\'next\' => __( \'Next (Right arrow key)\', \'your-text-domain\' ),
\'current\' => __( \'%curr% of %total%\', \'your-text-domain\' )
);
wp_localize_script( \'magnific-popup\', \'mfp_msg\', $magnific_popup_messages );
当您需要将一些参数传递给排队的javascript时,此函数也很有用(虽然它不是为这个目的而设计的,但这种类型的使用非常流行)。