Text Domain and Translations
-
Hi. I know there is a ton of documentation on this topic and I want to assure everyone that I read every bit I could find. The problem is that I still just dont get it. What the heck is text domain, why is it needed?
I know that I use _e() or __() for text that I want to be translatable, but how does this happen?
I apologize for asking a question about something that is clearly documented but it just makes no sense to me whatsoever.
I am reading up on deploying my plugin and I see that in the readme file I can/should include a “languages” directory. But this is just one more thing that makes no sense to me.
Would someone be kind enough to explain this whole thing to me in the simplest terms possible?
-
A WordPress site potentially has a lot of different things to translate; WordPress itself provides translations for its own things, your theme could provide translations, each of your plugins could provide translations, etc. A text domain is how PHP knows which translations to use.
When you translate your plugin, you need to generate a PO file that contains the translatable strings and your translators will use that to create a MO file that PHP can read. Poedit is a free tool that’s commonly used to do this.
Once you’ve got everything translated, you’ll stick the PO and MO files in the languages folder of your plugin; that’s where WordPress/PHP looks for the files.
Check out https://developer.www.ads-software.com/plugins/internationalization/localization/ and https://developer.www.ads-software.com/plugins/internationalization/how-to-internationalize-your-plugin/ for more information.
Im sorry but this still doesnt make any sense to me.
Here are some specific questions from your answer:“WordPress itself provides translations for its own things” What are its own things?
“your theme could provide translations, each of your plugins could provide translations” How?
“you need to generate a PO file that contains the translatable strings and your translators will use that to create a MO file that PHP can read” What is in the PO file? Each string and its translation?
I guess just looking at the editor you linked to will answer some (maybe all) of these questions. So thank you for the link. I will check it out.
“WordPress itself provides translations for its own things” What are its own things?
Things like the Dashboard itself, the post editing screen, the default widgets, etc.
“your theme could provide translations, each of your plugins could provide translations” How?
From a user’s perspective, you don’t have to do anything. You can select the appropriate language from the WordPress Dashboard and if your theme and plugins provide translations for that language, you’ll see them.
From a developer’s perspective, you use a tool such as Poedit to generate the appropriate PO and MO files and include them with your theme/plugin.
“you need to generate a PO file that contains the translatable strings and your translators will use that to create a MO file that PHP can read” What is in the PO file? Each string and its translation?
Yes. The way the system is set up, you’ll have a PO and a MO file for each different language:
fr-FR.po
for a French translation,es-ES.po
for a Spanish translation, etc.Im still a little confused, but I will do some trial and error with Poedit to try and figure it out. Thanks for the help.
Also, I seem to remember, in my researching, that sometimes others create the translation files for a site, theme, or plugin. Is that the way it works? Others do it as it is needed? Or as the plugin developer, do you create all of the translations you want available?
Oh and other question. I still dont understand text domain. Does the word domain have anything to do with a domain as it is used in the web? I dont think so. But can anyone explain how text domain factors into this, and how it is used? I just dont get its function.
Also, I seem to remember, in my researching, that sometimes others create the translation files for a site, theme, or plugin. Is that the way it works? Others do it as it is needed? Or as the plugin developer, do you create all of the translations you want available?
You can certainly send other people PO files and ask them to do the translations for you. If your plugin was popular enough, people might even contact you and offer to translate.
Oh and other question. I still dont understand text domain. Does the word domain have anything to do with a domain as it is used in the web? I dont think so. But can anyone explain how text domain factors into this, and how it is used? I just dont get its function.
The text domain doesn’t have anything to do with your site’s domain. The text domain is used so the PHP parser knows where to look to find the translations.
Ok, I seem to remember reading that it was sort of open, that others could create translations at will. But I must have mis-read. I guess they need to add the PO files to SVN, so it makes sense the the plugin creator has control.
As for the text domain, I guess what i dont understand is how it looks for the translations:
The text domain is used so the PHP parser knows where to look to find the translations.
So it isnt looking in a subdirectory of the plugin, then. Say the textdomain=”myplugin” what does the parser do with that info?
Every plugin has a slug, which is usually the name of the plugin with all punctuation removed, spaces converted to dashes, and all letters converted to lowercase. A plugin named “Steve’s Plugin” would have a slug of “steves-plugin”. That slug is typically used both as the text domain for that plugin and the name of the folder that contains the plugin.
When the PHP parser sees a translation function being used:
<?php _e( 'Hello', 'steves-plugin' ); ?>
The parser looks at the text domain (
steves-plugin
, in this case) and looks for the appropriate folder inwp-content
. When it finds the appropriate folder, it looks in thelanguages
folder for the appropriate MO file. If it finds the MO file for the current language, it replaces “Hello” with the translated string.The text domain must match the first part of the .mo file name. If textdomain=”myplugin” and the translation sought is German, a file named myplugin-de_DE.mo will be searched for. The file will be searched for in wp-content/languages/plugins, then wp-content/plugins. Plugins in the WP repository must use a text domain that matches their assigned plugin slug.
Probably too pedantic, but it’s not really the parser looking for .mo translation files, but the gettext PHP module. A parser is used by poedit to find strings to translate in order to generate a .pot file.
Also note that, for plugins residing in the WP repository, the informal translation process that we’ve relied on in the past is being replaced by a more organized WP community based process. See https://make.www.ads-software.com/plugins/2015/12/06/plugin-translations-for-all-plugins/ The post is over 2 years old now, the update mentioned has already been committed. Existing plugins need to request migration. New plugins are automatically enrolled IIRC.
Meant to answer as my original account. I just created a new one to match my plugin that I will be using in all posts after this thread.
I am so unbelievably confused by this.
If my plugin name, in the top of my PHP file is:
My Very First PluginI must make my text domain, used in every _e() and __() call:
my-very-first-pluginAs in:
echo 'untranslated text and ' . __('translated text', 'my-very-first-plugin')
If that is correct I will be sure to do that but, to be honest, I have no idea what this means. I will be doing it so that, when I finally make sense out of this, it will be in place properly.
Switched user names as you can see ??
Thanks for being clear about user names. While mods can indeed see both are for the same user, it does take a little extra attention to notice. In the mean time the switch can be a bit baffling.
If you are confused, you are in good company. It is confusing. You basically have it right now, if you do that it’ll be fine. However, it’s even more complicated in fact. Like all objects in WP, plugins have both a slug and a name. For most devs these both say the same thing. “My Plugin” name and “my-plugin” slug. There is no technical reason for each to say the same thing. Your plugin name might be “Awesome Plugin” while it’s slug is actually “my-plugin”. Your text domain for gettext purposes should match the plugin’s slug.
Now mind you, I am not part of the plugin review team. They might now be requiring new plugins and slugs say the same thing. There may be a social reason for requiring this even if there is no technical need. I would also advise that the two should say the same thing solely because it is more clear to users. Having a different slug is not too much different than changing accounts in the forums here. You can do it, but it’s a little confusing at first ??
I hope that helps and isn’t just more confusion.
- The topic ‘Text Domain and Translations’ is closed to new replies.