wordpress custom plugin translation file not working - wordpress

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

Related

How to make Gutenberg Block Translation work?

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.

How to add custom javascript to Wordpress Admin using a plugin

I am setting up a new Wordpress plugin with a custom metabox. This box contains sets of fields that I store in an array. I want the user to be able to add a new set of fields by clicking a button on the admin-page.
I read different posts on how to accomplish this but I havent succeeded yet. For testing purposes I created a real simple setup in the metabox to change a text of a element. This is also not working so I think it is a problem with loading the scripts correctly.
So what I did so far:
- add action through admin_enqueue_scripts
- register the script using wp_register_script
- enqueue script using wp_enqueue_script
- setup the js file (choose for testpurpose to store it in same dir as the plugin
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', 'amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );
//HTML code I use in the function which is called by add_meta_box
echo '<p id="demo">Current text</p>';
echo '<button id="woordje_toevoegen" onclick="woordjesToevoegen();">Woorden toevoegen</button>';
//amai_woordjes_updaten.js
<script>
function woordjesToevoegen() {
document.getElementById("demo").innerHTML = "New texxt";
}
</script>
You need use function wp_enque_script(). You use incorrect script path.
plugin_dir_path( __DIR__ ) . path/to/script.js
First You have to set the file path
Create js folder in your plugin root directory and move the 'amai_woordjes_updaten.js' file in js folder
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', plugin_dir_url( __FILE__ ).'js/amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );

Wordpress - Plugin translation not working

My plugin name is eventcal.Textdomain is eventcal. I have a language pack in /languages/ folder - eventcal-pt_BR.po and eventcal-pt_BR.mo. I have added plugin textdomain function
public static function eventcalLoadTextdomain() {
load_plugin_textdomain( 'eventcal', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
// in __constructor()
add_action( 'init', array(self::getInstance(), 'eventcalLoadTextdomain' ));
but its not working when i am changing my site language to portugese( Brazil )
Nothing is changing.everything is in english. How can i check plugin textdomain is working or not??
I also tried get_plugin_data( __FILE__ ); but not working

'poedit' translation activation

I'm trying to make my wordpress site translation ready, using 'poedit'. I have problems with activating the translation. I want to translate the site only, not wordpress system. I have my .pot, .po and .mo files in a 'languages' folder in my theme directory, I have this code in my functions.php file:
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
well, after everything is saved and translated in the poedit program, how do I activate it in my site? I am working with a localhost for now.
First, you need to connect the method to an action hook in your theme functions.php:
function load_mytheme_textdomain() {
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'load_mytheme_textdomain' );
Then you have to use translation instead of echo for output:
_e( 'My Text', 'example' );
Read more here: https://developer.wordpress.org/themes/functionality/internationalization/

Applying translation to custom WordPress plugin

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.

Resources