I am developing WC extension by its new way. Everything works like a charm instead of localization. I have generated JSON by wp i18n make-json and using in index.js like this:
import { __ } from '#wordpress/i18n';
__( 'Verify licence', 'domain' );
Unfortunatelly the strings are never been translated. Should I load the JSON myself or register the script wp_set_script_translations( 'domain-script', 'domain', plugin_dir_path( __FILE__ ) . '/languages' );
?
At server side translations work. I am really confused about the JS translation flow. Thanks for any help.
For future generations the solution is simple.
You have to call something like this:
public function extension_set_script_translations() {
$loaded = wp_set_script_translations( 'extension', 'extension', plugin_dir_path( __FILE__ ) . '/languages' );
}
private function initLocalizations() {
add_action( 'admin_enqueue_scripts', [$this, 'extension_set_script_translations'] );
}
Another important thing is name of the language JSON file.
{text-domain}-{locale}-{script-handle}.json
Related
In a plugin I'm working on, I'm trying to replace the OOTB customer-completed-order email with a template in the plugin
Constructor:
define( 'BKF_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
add_filter('wc_get_template', array($this, 'bkf_customer_completed_order_template'), PHP_INT_MAX, 5);
Function:
function bkf_customer_completed_order_template($template, $template_name, $args, $template_path, $default_path) {
if( $template_name == 'emails/customer-completed-order.php' ) {
$template = trailingslashit(BKF_WC_EMAIL_PATH) . 'templates/' . $template_name;
return $template;
}
}
note the template is still pulling the default woo one
Any thoughts/ideas are welcome!
Worked it out!
Instead of the method used in my original question, here's what worked for me:
I created a new class (similar to woocommerce/includes/emails/class-wc-email-customer-completed-order.php) - for demo purposes we'll call it My_Custom_Class
I then called like so in my constructor for the parent class I was working on:
add_action('woocommerce_email_classes', array( $this, 'bk_register_email' ), PHP_INT_MAX, 1 );
And added this function:
public function bk_register_email( $emails ) {
require_once 'emails/my-custom-class.php';
$emails['WC_Email_Customer_Completed_Order'] = new My_Custom_Class();
return $emails;
}
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.
//settings.php
<div>
<p>Settings</p>
<p><?php echo $text ?></p>
</div>
I render template while developing wordpress plugin using require_once
public function display_admin_page() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/templates/settings.php';
}
When I call the function display_admin_page, I can display the template. However I want to pass some args to template, how can I do it. I want to write a function so I can use like this (but I don't know how to write the function
public function display_plugin_admin_page() {
$args = [
'text' => 'Hello'
];
get_plugin_template('admin/templates/settings.php', $args);
}
Could you guy help me how to write get_plugin_template function? Thanks
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.
My folder:
my-plugin/
---inc/
------class-my-plugin.php
---my-plugin.php
In file my-plugin.php:
require_once( ABSPATH . 'wp-content/plugins/my-plugin/inc/class-my-plugin.php' );
In file class-my-plugin.php:
class MyPlugin {
public static function pluginActivate() {
add_option( '__MY_PLUGIN_ACTIVE__' , true );
}
public static function pluginDeactivate() {
update_option( '__MY_PLUGIN_ACTIVE__' , false );
}
}
register_activation_hook( __FILE__, array( 'MyPlugin', 'pluginActivate' ) );
register_deactivation_hook( __FILE__, array( 'MyPlugin', 'pluginDeactivate' ) );
But when i actvate or deactivate, it not working. Somebody can help me?
This is likely due to your use of add_option():
A safe way of adding a named option/value pair to the options database table. It does nothing if the option already exists.
Try using update_option() for both the activate and deactivate to properly affect the option value. If this doesn't work, add some error_log() writes to your actions to make sure they are firing.