How to Translate Your Plugin
You finished coding your Disciple.Tools plugin and now you want to make it accessible to the whole community. Before uploading it, you can add translations in order to make the content understadable for every user.
There are a few different ways of translating your plugin. Here are a few.
Using Poedit Software
Download Poedit: link here
Open the software and select the
Edit a translation
optionSelect the
default.pot
file in your plugin's/languages
folderGo to the
Catalog
menu and click onUpdate from Source Code
. You should see the strings used in your plugin's code.Click the
Create New Translation
button at the bottom and select the language you want to translate intoTranslate each string by clicking on them and add its respective translation in the
Translation
text areaAfter translating every string, save the file. Poedit will suggest a file name that is the language code you selected for your translation. Don't delete or modify that text. Write your plugin name before it making sure you only use lowercase and replace spaces with hyphens. (
eg. my-plugin-name-es_ES.po
)Make sure your code echoes the translated strings with the following format:
<?php echo esc_html__( 'Hello, World!', 'my-plugin-name' ); ?>
Done! Your translation should appear for your plugin on the Disciple.Tools theme.
Using Poedit & Poeditor Website for community translations
Register in www.poeditor.com and create a new project for your plugin
Add a new language
In Poedit, load the
default.pot
file in your plugin's/languages
folderGo to the
Catalog
menu and click onUpdate from Source Code
. You should see the strings used in your plugin's code.Save the
default.pot
file with the imported stringsIn poeditor.com, go to the translation project for your plugin and upload the
default.pot
file by clicking theImport
button. Your plugin's terms should have been added automatically.Translate all the terms and export the
.po
and.mo
files from the project language menu. These files should be saved in your plugin's/languages
folder. The file name should be your plugin's name in lowercase and with hyphens instead of spaces followed by the language code. (eg.my-plugin-name-es_ES.po
)Make sure your code echoes the translated strings with the following format:
<?php echo esc_html__( 'Hello, World!', 'my-plugin-name' ); ?>
Done! Your translation should appear for your plugin on the Disciple.Tools theme.
How to translate strings in JS files
Open
dt-assets/functions/enqueue_scripts.php
.Search for the if statement that looks for the PHP file that calls your JS script. If it doesn't appear in
enqueue_scripts.php
, you will have to create an if statement that looks for it. Live examples of these if statements already exist in this file. You can use them as a reference.Inside the if statement, make sure that your JS script is added with
dt_theme_enqueue_script()
. We're going to need the handle parameter for the next step (the first parameter in thedt_theme_enqueue_script()
function).
Example:
Create a
$translations
array that contains the keys and values for what you want the string to show.
Example:
If it's not already there, add a
wp_localize_script()
function to localize your script. Script localization passes values obtained through PHP to your JS script.The
wp_localize_script()
should have the handle from step 3 passed as the first parameter. The second parameter should benew_record_localized
and the third parameter should be the information you want to localize. See example below.
Now we move to the JS file, find the place that contains the string you wish to translate and replace it with the translated string. Translated strings are inside the
window.new_record_localized.translations
object.
Example:
Last updated