While working on creating a WordPress theme, we often come across such inscriptions as “upload more, author, tags”, etc. For example, as in my blog:
It’s good when the site is in one language. Then you don’t really need to do anything with them. But what to do when the site is multilingual.
If the customer does not need to change their names in the future, then there is no point in displaying them for editing in the admin panel either.
There is a lot of information on the Internet in which the use of the Poedit program appears. But there is a much more elegant way. To do this, we need the Loco Translate plugin. This add-on is extremely useful to use – thanks to it you can translate not only theme entries, but also plugins. But specifically today we will talk about the translation of the topic.
Here I will not go into much theory. Let’s focus on the process.
Stages:
- In the files with the theme, we place the entries we need in one of these functions _e(), __(); The only difference between these functions is that _e() immediately displays the text on the page, while __() only receives it (therefore, you need to use echo to display the translation text).
_e('Author', 'my-site'); echo __('Author', 'my-site');
The functions have two arguments: The first one is the text to be translated (mandatory). Second: Transfer ID. The scope of the translation, denoted by the name, which then becomes an identifier in PHP (optional).
- Uploading the Loco Translate plugin to our site
- In the admin panel in the left menu we find the Loco Translate item, hover over it with the mouse and click on the Themes item.
- We ended up on a page where all the downloaded themes that are available to us for use on the site are displayed. We choose our current theme. In this case, it’s a topic called “My”
- Before proceeding to the next step, I recommend creating a directory called languages in the theme folder. Here we will store all the files that are related to the translation of our theme.
- After point 4, if you create a topic from scratch, then most likely you will be greeted with the following message on the page that opens – No translations found for “name”. Therefore, the next step is to click on the inscription above “Create a template”.
Thus, the plugin scans our theme and all the inscriptions that we declared in the theme using the functions in paragraph 1 will be output to a file in .pot format – this will be a template for translations without the translation itself. - Next, we create the actual translation of the records. Click on the inscription “New language”.
- In this window, we set the language to which we will translate and the place where we will save the translation file (In my case, we translate the topic into Ukrainian and save the file to the languages folder).Having finished with this, click on the “Start translation” button below. As a result of this step, we will have two translation files in .po and .mo format.
- Next we find the inscriptions that we will translate. In the area with a list of inscriptions, find the one you need and click on it. We will have two fields below available: the first is how the inscription is displayed in the topic, the second is how this inscription should be displayed in translation. Actually, in the second field, we enter the translation we need and save the changes. We translate all other inscriptions in the same way.
- The final touch is to download the theme translation (.mo) file. We do this with the load_theme_textdomain() function which will be used in the after_setup_theme hook. This code is placed in the theme’s functions.php file:
function my_theme_setup() { load_theme_textdomain('my-site', get_template_directory() . '/languages'); } add_action('after_setup_theme', 'my_theme_setup');