Applying translation to custom WordPress plugin - wordpress

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.

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.

wordpress custom plugin translation file not working

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

'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/

Using Timber for WordPress admin screens

I am building a WordPress plugin to show documentation related to the project. There is a good amount of HTML required in the views, with the content coming from markdown files.
I was wondering if it's at all possible to use Timber Plugin for admin screens, to help keep everything clean? I cannot find anything in the docs.
I have a function that renders a view (called by add_menu_page()) which is working fine. If I echo some text or HTML or even echo the contents of a file, I see what I'd expect.
Attempting to use Timber in the same way as I would in a theme, like this:
$context = Timber::get_context();
Timber::render(plugin_dir_url( __DIR__ ) . 'views/docs.twig', $context);
This just gives me an empty page, with no errors thrown at all.
Do I need to initialise Timber in some way first?
This is the first plugin I have made and my PHP isn't brilliant so apologies if this question isn't easy to follow or understand.
According to the Template Locations documentation, by default Timber looks in the views/ directory of the current theme for the template files. You can add other paths this way:
Timber\Timber::$locations = array(
plugin_dir_path( __FILE__ ) . 'views/'
);
Timber will look in that location first, so templates in the plugin will override any in the theme if they share the same name.
Yes! Just figured it out.
So, on your functions.php, do the usual procedure of adding a page on the admin side
prepare the function:
function documents_menu() {
add_menu_page('Docs', 'Docs', 'manage_options', '', 'myplguin_admin_page', 'dashicons-heart', 6);
}
then the hook:
add_action( 'admin_menu', 'documents_menu' );
then the renderer function:
function myplguin_admin_page(){
if ( ! class_exists( 'Timber' ) ) {
// if you want to show some error message, this is the right place
echo "Timber doesn't exist!";
return;`enter code here`
}
here's the key part:
Timber::render( 'docs.html.twig', array(
'args' => $args,
'instance' => $instance,
/* any other arguments */
));
}
then on your [theme]/views, create docs.html.twig and create your twig file!

Change language only on theme

I want to keep the admin in english but the theme in another language. By adding language files to the theme directory nothing happens, the option to switch language wont come up. Is this possible without adding languages to wp-content?
I found a solution using the locale filter and load theme textdomain
The solution looks like this:
define('LOCALE', 'en');
add_action( 'after_theme_setup', 'site_setup' );
function site_setup() {
//Make the theme available for translation
load_theme_textdomain( 'mytheme', get_template_directory() . '/languages' );
$locale = get_locale(); //Will get what you defined earlier
$locale_file = get_template_directory() . "/languages/$locale.php";
if ( is_readable( $locale_file ) ) require_once( $locale_file );
}

Resources