I've just built a Gutenberg block, and I'd like to have it translated into different languages. I am using my WordPress in Portuguese and I've made a Portuguese translation. I followed all the steps available in the documentation https://developer.wordpress.org/block-editor/developers/internationalization/ and it still didn't work.
My plugin is called spoiler-alert.
I have created a .pot file named spoileralert.pot in the /spoiler-alert/languages folder. Then, I've generated the md5 files and even created a new file with all my strings using a script handler. My languages folder structure looks like this:
languages
- spoileralert.pot
- spoileralert-pt_BR.po
- spoileralert-pt_BR-<HASH-CODE-1>.json
- spoileralert-pt_BR-<HASH-CODE-2>.json
- spoileralert-pt_BR-<HASH-CODE-3>.json
- spoileralert-pt_BR-spoileralert.json
And here is my PHP file spoiler-alert.php:
function spoiler_alert_spoiler_alert_block_init() {
$dir = dirname( __FILE__ );
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "spoiler-alert/spoiler-alert" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require( $script_asset_path );
wp_register_script(
'spoiler-alert-spoiler-alert-block-editor',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
$editor_css = 'build/index.css';
wp_register_style(
'spoiler-alert-spoiler-alert-block-editor',
plugins_url( $editor_css, __FILE__ ),
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'build/style-index.css';
wp_register_style(
'spoiler-alert-spoiler-alert-block',
plugins_url( $style_css, __FILE__ ),
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( 'spoiler-alert/spoiler-alert', array(
'editor_script' => 'spoiler-alert-spoiler-alert-block-editor',
'editor_style' => 'spoiler-alert-spoiler-alert-block-editor',
'style' => 'spoiler-alert-spoiler-alert-block',
) );
wp_set_script_translations( 'spoileralert', 'spoiler-alert', plugin_dir_path( __FILE__ ) . '/languages' );
}
add_action( 'init', 'spoiler_alert_spoiler_alert_block_init' );
In my .js files I've imported the translation package using: import { __ } from '#wordpress/i18n';
And I am using the translations like: title: __( 'Spoiler Alert', 'spoiler-alert' ),
How can I make the translation display correctly?
It's been a while already, but it might still be useful to you or someone else out there. I'm simply copying and pasting what I've already answered here.
I've been through this process quite a few times already, so I'm 100% sure this is working properly. Carefully go through each one of the steps and you should manage to solve any issues you might be running into ;)
Translation has finally been properly developed and documented: https://developer.wordpress.org/block-editor/developers/internationalization/
Basically, you need to:
Use the wp-i18n package to define which are the strings in your project to be translatable.
Compile your code (wp i18n make-pot will search for translation strings in your compiled code, not in your source).
Use the WP-CLI (wp i18n make-pot) to create a .pot file, just like we've always used with WP themes and plugins.
Generate a .po file based on our previously created .pot and translate its content.
Use the WP-CLI again (wp i18n make-json) to convert our .po file to the JSON format needed.
Use the wp_set_script_translations PHP function to load the translation files.
Related
This is my plugin directory access when I use plugin_dir_url(__FILE__)
http://127.0.0.1/WordPressProject/myplugin/wp-content/plugins/advanced-plugin/admin/
But I want to access to http://127.0.0.1/WordPressProject/myplugin/wp-content/plugins/advanced-plugin/
After using the function it includes with
admin
folder. not the base plugin base url
Thanks
Use plugin_dir_url( __FILE__ ); for the URL and plugin_dir_path( __FILE__ ); for the path.
Pass the plugin’s main file to both functions to get similar results.
plugins_url( string $path = '', string $plugin = '' )
Retrieves a URL within the plugins or mu-plugins directory.
For more information
Thanks
Finally I got a solution by using
plugin_dir_url(dirname(FILE))
in the index.php of a custom plugin i have put this :
wp_enqueue_style( 'style_overview', get_stylesheet_directory_uri() . 'http://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css',false,'1.1','all');
But it returns this in the final head :
href="http://127.0.0.1/wp-content/themes/alchemistshttp://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css?ver=1.1"
Thanks all.
You are defining this path in the enqueue style function. With the dot . you are connecting with the following string.
You can use this to get the current plugins url:
plugin_dir_url( __FILE__ ) . '/css/style_overview.css'
Or you can just define it hard coded as you did:
'style_overview', 'http://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css'
The function you are using gives you the url where the style.css of your active theme is located:
get_stylesheet_directory_uri()
That is why you are getting your theme path followed by the url you added as a string.
Use plugins_url() and then append your directory or file structure on the end of that.
https://developer.wordpress.org/reference/functions/plugins_url/
Example:
wp_register_style(
'my-css',
plugins_url( '/blocks/dist/style.css', __FILE__ ),
array( 'wp-blocks' )
);
I have a plugin with name "keydesign-addon" and I created file translation in my language and file loaded with below code:
add_action( 'plugins_loaded', 'keydesign_addon_load_textdomain' );
function keydesign_addon_load_textdomain() {
load_plugin_textdomain( 'keydesign', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
but plugin translation not working and it doesn't translate any words.
textdomain is "keydesign" and words are like this
esc_html__("App gallery", "keydesign")
I used "keydesign-addon-fa_IR.mo" and "fa_IR.mo" for file name but not working
also I installed "loco translate" to translate plugin but again not working
I'm trying to apply a translation to my cusom plugin. I've alredy created brau-ru_RU.mo and brau-ru_RU.po files. My translation domain is 'brau'.
What I've tried is to put files in wp-content/languages/plugins/ and execute this code in my plugin.
$domain = 'brau';
$mo_file = WP_LANG_DIR.'/plugins/'.$domain.'-'.get_locale(). '.mo';
var_dump(load_textdomain( $domain, $mo_file ));
var_dump(load_plugin_textdomain( $domain ));
var_dump(__('This is the test', 'brau'));
Result is:
bool(true) bool(true) string(16) "This is the test"
I also got this code in my config
define ('WPLANG', 'ru_RU');
The text shoud be translated from English to Russian, but it's not. What am I missing?
This is a link to test version of the plugin: https://github.com/Brezgalov/brezgalovauth
If you want to add language files to your plugin then you can create a folder called languages inside your plugin folder and in the plugin main file you can define like this
// Localiztion with language files
function custom_langs_i18n() {
load_plugin_textdomain( 'textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'custom_langs_i18n' );
Please remember to use textdomain where you are using string.
public function setup(){
//...
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
}
function register_plugin_styles(){
wp_register_style( 'dontwaste', JSC_DONT_WASTE . '/styles/dontwaste_styles.css' );
wp_enqueue_style( 'dontwaste', false, null, null );
}
The JSC_DONT_WASTE is the path to the plugin directory, so JSC_DONT_WASTE . '/styles/dontwaste_styles.css' returns the following:
/Applications/AMPPS/www/wpplugin/wp-content/plugins/dontwaste/styles/dontwaste_styles.css
and this is exactly the path I need to reach my css file.
But the code above returns this NOT FOUND error:
http://127.0.0.1/wpplugin/Applications/AMPPS/www/wpplugin/wp-content/plugins/dontwaste/styles/dontwaste_styles.css?ver=4.6.1 404 (Not Found)
You'll notice in the beginning there's a /wpplugin/ (the name of the plugin folder) right after the localhost. How do I get rid of that and get the path to work?
There's also the version number which even though I've set to NULL it still sets a version number.
Yes, that makes sense.
Javascript and CSS assets are loaded using their URI (or URL) and not path on the server directories. As they're loaded on the frontend side. So it'll be something like yoursite.com/wp-content/plugins/your-plugin/js/your-asset.js
So you can't use :
plugin_dir_path( __FILE__ )
You must use :
plugin_dir_url( __FILE__ )
The first one is for files loaded in your plugin files (server side). Like a require_once. And the second one if for assets loaded on the user side.
Therefore, you can do something like :
// For the files called in your php :
if ( ! defined( 'PLUGIN_DIR' ) ) {
define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// For the assets files loaded on the client side :
if ( ! defined( 'PLUGIN_ASSETS_URL' ) ) {
define( 'PLUGIN_ASSETS_URL', plugin_dir_url( __FILE__ ).'/assets' );
}