i was working on creating a custom wordpress plugin, but while trying to do the translation, facing this issue. everything to me is done correctly.
for example: plugin folder name 'translated_plugin' and the main file 'translated_plugin.php'
<?php
/**
* Plugin Name: Translated Plugin
* Plugin URI: https://example.com
* Description: Example Plugin Description
* Version: 1.0.0
* Author: dev
* Author URI: https://example.com
* License: GPL-2.0
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: translated_plugin
* Domain Path: /languages
*/
if ( !defined( 'WPINC' ) ) {
die;
}
require __DIR__ . '/vendor/autoload.php';
$app = new Application();
register_activation_hook( __FILE__, [$app, 'activate'] );
register_deactivation_hook( __FILE__, [$app, 'deactivate'] );
example 'class_Application' class
class Application {
public function __construct() {
add_action( 'init', [$this, 'translate_it'] );
echo __('Test Text', 'translated_plugin');
}
public function translate_it(){
$loaded = load_plugin_textdomain( 'translated_plugin', false, dirname(dirname( plugin_basename( __FILE__ ) )) . '/languages/' );
// $loaded returns true when vardump($loaded), means textdomain path is correct and domain is loaded
}
}
on languages folder , the pot file 'translated_plugin.pot' is generated by loco translate plugin. and .mo file is also generated through translation process from loco translate plugin. but text doesn't change based on language from WordPress setting.
(similar process on a test theme worked correctly but not sure why not working for plugin even though translation files are correct, file name as same as text domain, loaded is true) what could be wrong here or maybe a bug from WordPress? Thank you!
CodePudding user response:
EDIT AFTER OP UPDATED CODE:
Ok, now I see this: That echo command is happening before translate_it()
fires. The add_action()
function adds the function to the queue to fire when the init
actions are set to fire, but it's not immediate. Try putting your echo command in the translate_it()
function below load_plugin_textdomain()
.
class Application {
public function __construct() {
add_action( 'init', [$this, 'translate_it'] );
echo __('Test Text', 'translated_plugin'); //Will fire before text domain is loaded
}
public function translate_it(){
$loaded = load_plugin_textdomain( 'translated_plugin', false, dirname(dirname( plugin_basename( __FILE__ ) )) . '/languages/' );
// $loaded returns true when vardump($loaded), means textdomain path is correct and domain is loaded
echo __('Test Text', 'translated_plugin'); //Will fire after text domain is loaded
}
}
If you want a visual of the order of events, here's how it goes
- WordPress starts
- WordPress loads plugins
$app = new Application();
happens which runs the __construct() functionadd_action( 'init', [$this, 'translate_it'] );
adds the function to the queue- "Test Text" is echoed
- WordPress does some other stuff...
- WordPress gets to
do_action( 'init' );
- NOW your
translate_it()
function runs, loading the text domain
ORIGINAL ANSWER (Now outdated):
It looks like you're using two different text domains: translated_plugin
in your __()
call, and adev_translated_plugin
in your load_plugin_textdomain()
call. I'm fairly certain those need to be the same. It seems like you meant to use adev_translated_plugin
in all places, and that's probably the best choice. So that would make your construct function look like this:
public function __construct() {
add_action( 'init', [$this, 'translate_it'] );
echo __('Test Text', 'adev_translated_plugin');
}