Change WordPress texts without modifying the translation

There are many occasions in which it can be interesting to modify elements of the WordPress administration to adapt them to the taste of a client or of oneself and, like everything in WordPress, you can get it in several ways. [19659002] The mode " to the beast " would be to create your own translation adapted to your tastes or the project, but if you only want to modify a few texts it is not necessary to make that nonsense which would also be ruined in every WordPress update.

A simple way to modify WordPress texts and not get lost in each update is create a function using gettext and ngettext that you modify those texts that you want to customize and it would be something like this:

load_theme_textdomain ( 'My theme name' TEMPLATEPATH . '/ languages' [19659009]) ;

$ locale = get_locale () ;

$ locale_file = TEMPLATEPATH . "/ languages ​​/ $ locale.php" ;

if ([19659020] is_readable ( $ locale_file ) )

require_once ( $ locale_file ) ;

On line 1 you will see the function load_theme_textdomain () . This function allows you to load a Text Domain . Choose any name, but remember that it has to be unique. Ideally, you should use the name of your theme
.

2 – Internationalize your theme

To translate our WordPress theme we will use the PHP gettext functions.
GetText has two functions: _e and __ (both with underscores).
The "_e" function is used to print "simple" text, and the function __ is used when the text to be displayed already is wrapped in PHP tags.

EXAMPLES:

Realize again that the name of the text domain ( My theme name ). Remember that it must be the same as in the file functions.php .

The boring part is that you have to replace each simple string with the necessary function. Depending on how many strings your theme has, this can be a really tedious task (that's why so few themes are internationalized). It seems that there are some GNU tools that facilitate the task of extracting strings from files in a simple way. If you want you can search Google by xgettext .

3 – Create your .po file

From now on, your WordPress theme can be easily translated into any language. But to show the texts in a language different from yours you have to add a file .po .

The files .po mean Portable Object (Portable Object). Basically, these files contain a string, and its translation into another language. For example, if you translate the Spanish version of WordPress, you will have a file es_ES.po inside the folder / wp-content / languages ​​/ . This file contains all the translations so that your theme "speaks" Spanish, so your theme says Welcome instead of Welcome .

But there is good news, you do not have to search in the files of your theme each line to be translated. There is an online tool called icanlocalize.com that can scan PHP files and create files .po for you. ICanLocalize will extract all strings wrapped in calls __ ("txt", "domain") and _e ("txt", "domain") . Chains can be closed with double quotes (") or single quotes (') and with any encoding characters.

 icanlocalize" title = "icanlocalize" width = "500" height = "345" class = "aligncenter size-medium wp-image-5846" srcset = "http://bootstrapx.com/wp-content/uploads/2018/06/icanlocalize-500x345.png 500w, https://ayudawp.com/wp- content / uploads / 2009/08 / icanlocalize.png 913w "sizes =" (max-width: 500px) 100vw, 500px "/> </a></p>
<p> Then, the files <em> Po </em> can be edited with <a href= PoEdit a free software specially designed for this task:

 poedit "title =" poedit "width =" 500 "height =" 343 "class =" aligncenter size-medium wp-image- 5847 "srcset =" http://bootstrapx.com/wp-content/uploads/2018/06/poedit-500x343.png 500w, https://ayudawp.com/wp-content/uploads/2009/08/poedit- 1024x704.png 1024w, https://ayudawp.com/wp-content/uploads/2009/08/poedit.png 1248w "sizes =" (max-width: 500px) 100vw, 500p x "/> </a></p>
<p> As you may have guessed, you have to translate each string. Once you have translated everything save the file <em> .po </em>. PoEdit will also create a file <em> .mo </em>which is a compiled version of the file <em> .po </em>. </p>
<h2><span id= 4 – Start-up

Now that you have done the most part " difficult "of the work the only thing that remains is to define your" locale "in WordPress.

To get it, the first thing is to get your language and country code. For example, if your language is Spanish and you live in Argentina, your code will be es_AR . The GNU gettext manual has pages to help you find the codes for your country and language .

Once you have your codes, open the file wp-config. php and look for the constant WPLANG. If it exists, simply replace the existing code with yours. If it does not exist, then paste the following line (with your own country code and language, of course)

Sources

In the following articles you will find the documentation used to elaborate this post. [19659091] NOTICE : this publication is from two years ago or more. If it's a code or a plugin it might not work in the latest versions of WordPress, and if it's a news story it might be obsolete. Then do not say we have not warned you.