PIKADEMIA

Projekt edukacji alternatywnej

How to create a wordpress plugin ready for translation

It shows how to prepare text in wordpress plugin to be translated. In this example, settings menu is created for the plugin so that we can see the translation when the language is changed.

Step 1. Add domain attributes in the comment section:
Text Domain: yourDomainName
Domain Path: /languages

Step 2. Create a const with domain name so that it can be addressed with ease

Step 3. Create empty subfolder called languages and register reference for plugin domain

Step 4. Keep using special syntax for all the text that needs to be translated
__(’Text to be translated’,’Domain name’) or escape syntax for extra safety esc_html__(’Text to be translated’,’Domain name’)

<?php
/*
Plugin Name: Add Text 
Description: It adds some text to the content
Author: pikademia
Version: 1.0.0
Author URI: https://pikademia.pl
Text Domain: atpi
Domain Path: /languages
*/

// It's a basic plugin with option page, prepared to be translated (polish -> english)
// It can be translated with plugin (eg. loco translate or other)
// Along with this file, create an empty subfolder named languages

if ( !defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

// For translation
define ('PITDOM','atpi');
add_action('init', 'languagesATPI');
function languagesATPI() {
    load_plugin_textdomain(PITDOM, false, dirname(plugin_basename(__FILE__)) . '/languages');
}

// Add page
add_action('admin_menu', 'addmenuATPI');

function addmenuATPI(){
    add_options_page(
        esc_html__('Wtyczka Dodaj tekst',PITDOM),
        esc_html__('Dodaj tekst',PITDOM),
        'manage_options',
        'addTextPage',
        'createPageATPI');
}
function createPageATPI(){
    echo esc_html__('Cześć',PITDOM);
}

Would you like to read more about: add_options_page()