I have been trying to implement the translation to a couple of strings in a .js file but nothing has worked.
First try to do it like I do in php, in the .js file add this variable var __ = wp.i18n.__;
and I put the strings like __( 'Hello World', 'text-domain'),
but it didn't work.
After looking for information about it, I found a post where they talk about passing the translated strings with wp_localize_script
something like that.
// 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 );
I did it and it didn't work either
Below I leave my js and php code, please tell me what I'm doing wrong
PHP
// Register script
function mediafolders_sidebar_plugin_register() {
wp_register_script(
'mediafolders-plugin-select-js',
MEDIAFOLDERS_PLUGIN_URL . 'assets/js/select.js',
array( 'wp-plugins', 'wp-edit-post', 'wp-element' )
);
$translation_array = array(
'textContent' => __( 'Select the folder where you want to save the images or files that you upload to this post.', MEDIAFOLDERS_TEXT_DOMAIN ),
'textButton' => __( 'Select Folder', MEDIAFOLDERS_TEXT_DOMAIN ),
);
wp_localize_script( 'mediafolders-plugin-select-js', 'MediaFolders', $translation_array );
}
add_action( 'init', 'mediafolders_sidebar_plugin_register' );
// Enqueue script
function mediafolders_sidebar_plugin_script_enqueue() {
wp_enqueue_script( 'mediafolders-plugin-select-js' );
}
add_action( 'enqueue_block_editor_assets', 'mediafolders_sidebar_plugin_script_enqueue' );
JAVASCRIPT
window.addEventListener( 'load', function() {
var registerPlugin = wp.plugins.registerPlugin;
var PluginDocumentSettingPanel = wp.editPost.PluginDocumentSettingPanel;
var Button = wp.components.Button;
var el = wp.element.createElement;
var MediaFolders = {
textContent: 'Select the folder where you want to save the images or files that you upload to this post.',
textButton: 'Select Folder',
};
function onButtonClick() {
var popup = document.querySelector( '.mediafolders-popup' );
popup.classList.add( 'open' );
}
// Create elements for panel on sidebar
function MediaFoldersDocumentSetting() {
return el(
PluginDocumentSettingPanel,
{
name: 'media-folders-lite',
title: 'Media Folders Lite',
icon: 'open-folder',
initialOpen: true,
},
MediaFolders.textContent,
el(
Button,
{
isDefault: true,
className: 'mediafolders-panel-button',
onClick: onButtonClick,
},
MediaFolders.textButton,
),
);
}
registerPlugin( 'media-folders-lite', {
render: MediaFoldersDocumentSetting,
} );
// Close the pop up by clicking on the X or Done!
var btnClosePopup1 = document.querySelector( '.mediafolders-popup-close' );
var btnClosePopup2 = document.querySelector( '.mediafolders-popup-button > button' );
if ( btnClosePopup1 ) {
btnClosePopup1.addEventListener( 'click', () => {
var popup = document.querySelector( '.mediafolders-popup' );
popup.classList.remove( 'open' );
});
}
if ( btnClosePopup2 ) {
btnClosePopup2.addEventListener( 'click', () => {
var popup = document.querySelector( '.mediafolders-popup' );
popup.classList.remove( 'open' );
});
}
});