WPML-更改媒体上传器语言

时间:2016-10-06 作者:Fredy31

我使用此功能打开前端wordpress的图像选择器

function save_image($id){
    var mediaUploader;
    event.preventDefault();
    // If the uploader object has already been created, reopen the dialog
    if (mediaUploader) {
        mediaUploader.open();
        return;
    }
    // Extend the wp.media object
    var popupTitle = \'Choisir Image\';
    if($(\'body\').hasClass(\'en\')){
        popupTitle = \'Choose Image\';
    }
    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: popupTitle,
        button: {
            text: popupTitle
        },
        multiple: false,
        type: \'image\'
    });

    // When a file is selected, grab the URL and set it as the text field\'s value
    mediaUploader.on(\'select\', function() {
        attachment = mediaUploader.state().get(\'selection\').first().toJSON();
    });
    // Open the uploader dialog
    mediaUploader.open();
}
我想知道如何定义它的语言?我有一个WPML双语网站,但弹出窗口现在总是用英语。

我可以更改什么选项以法语获取它(在网站的法语版本中)?我想这是你可以选择的wp.media, 但我不知道该呼吁什么。

1 个回复
最合适的回答,由SO网友:Benoti 整理而成

我也是法国人,但我必须用英语回答。您需要使用wp\\u localize\\u script(),它允许在js脚本中处理本地化。

add_action(\'wp_enqueue_scripts\', \'load_script\');
function load_script(){
    wp_register_script( \'popup\', plugins_url(\'yourscript/js/yourjs.js\'), array( \'jquery\' ), false, true );
    wp_localize_script(
        \'popup\',
        \'popUp\',
        array(
            \'popupTitle\'=> __(\'\', \'your-text-domain)
        )
    );
    wp_enqueue_script(\'popup\');
}
在js文件中,您将能够检索值:

function save_image($id){
    var mediaUploader;
    event.preventDefault();

    // If the uploader object has already been created, reopen the dialog
    if (mediaUploader) {
        mediaUploader.open();
        return;
    }

    // Extend the wp.media object
    var popupTitle = popUp.popupTitle;

    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: popupTitle,
        button: {
            text: popupTitle
        },
        multiple: false,
        type: \'image\'
    });

    // When a file is selected, grab the URL and set it as the text field\'s value
    mediaUploader.on(\'select\', function() {
        attachment = mediaUploader.state().get(\'selection\').first().toJSON();
    });

    // Open the uploader dialog
    mediaUploader.open();
}

https://codex.wordpress.org/Function_Reference/wp_localize_script

您可以安装插件WordPress Media Javascript指南。它通过屏幕截图和示例说明了wp必须使用的方法。媒体对象。

只需一个片段即可更改按钮文本和标题:

wp_register_script( \'media-bbss-uploader-js\', BBSSLIDERS_DIR_URL .\'/js/bbss-multiple-media-upload.js\', array(\'jquery\') );
wp_localize_script(
    \'media-bbss-uploader-js\',
    \'screenHelp\',
    array(
        \'title\' => __(\'Select or upload new slides\', $this->plugin_text_domain),
        \'buttomText\' => __(\'Add slides\', $this->plugin_text_domain),
    )
);
wp_enqueue_script( \'media-bbss-uploader-js\' );

相关推荐

Images with overlay

我有一些图片在一个容器中,我想添加一个覆盖和图标。这不是现成的,但我找到了一些有用的代码:HTML:<div class=\"main\"> <span class=\"featured\"><img src=\"http://joshrodg.com/IMG_001-258x258.jpg\" title=\"\" alt=\"\"></span> </div> CSS:.featured {