Reputation: 879
N.B: I am 15 years old and still learning coding so if you answer me answer me thoroughly please....
Hello everybody, I am making my own website with WordPress I am also making my own custom theme and I am still in it's development, the site is already online but still not complete, and I want to complete it as fast as possible because when school arrive I won't have time to coding anymore.
One of key features of my website that I want it to be a multi language site, at least Arabic and English languages, so I installed the Polylang WordPress plugin and every thing is ok except that there is some texts for example in the footer were hardcoded, written in the footer.php file it self so Polylang can't touch it at all.I want to use Polylang string translations option to translate any text in the header or footer or anywhere I specify it.and this is my problem.
I want a solution to this problem, thanks in advance.
Sorry if there is any language mistakes,and bad layout I am texting from a phone.
Upvotes: 3
Views: 7778
Reputation: 321
Localization or l10n for short has very well developed methods. Since you are designing your own theme you should read about WordPress l10n to learn about PO and MO files.
Typically in Wordpress you use _e('','') and __('','') to echo or return localized strings respectively. These functions use gettext to match the first string with the MO file's localized entry with the second argument which defines your text-domain.
_e('I will echo text', 'my-theme');
In order to get these functions to return the proper results from the MO file you need to populate the PO file. PO file editors can scan your theme folder looking for any instances of these _e() and __() functions and list them in an easy to edit UI. There are a variety of PO editors available.
Once you populate the strings you use the PO editor to create the MO file for that language. This is typically done in the 'languages' folder of your theme. If you did it elsewhere you would now copy the PO/ MO files there.
Now the final piece is to register your text-domain in your theme so the gettext function can locate the MO you just made.
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup(){
load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );
}
Upvotes: 2
Reputation: 659
I'm not known with Polylang but have used other similar plugins in the past.
I've viewed the developer documentation here and think you need to use the function pll_register_string
to register the string and then use pll_e
to output that string.
So for example you got the text 'Hello World!' in your template, then use the function pll_e
to output that text like so:
<?php pll_e('Hello World!'); ?>
Then to get this string in Polylang string translations you need to register it in your themes functions.php
using the pll_register_string
function like so:
<?php
add_action('init', function() {
pll_register_string('shams-theme', 'Hello World!');
});
?>
Hope this helps out, if you need anymore help just let me know.
Upvotes: 7