I will really appreciate your help for a detailed guide about how can I achieve the following:

  • Please don't refer to external Plugins since I don't want to use them.
  • I'm developing my own WP theme so no worries there about theme updates.

I have my website currently in Hebrew (theme's strings are in English translated into Hebrew with loco-translate plugin). The website's admin language is in Hebrew so the language-attribute in the html is <html dir="rtl" lang="he-IL"> as default.

All the pages are written in separated .php files. Not in the built-in WP's editor. So I have a full control over their layout (if it's relevant for your future answers).

I would like to enable a translation for the website to English and Russian.

So, here are the actual questions:

1) How can I create virtual pages like example.com/en/ and example.com/ru/ without actually creating the en and ru pages (in WP Admin->New Page), so when you click on the en or ru links it will take you to that URL without showing the code 404 page not found?

2) How to load correctly and automatically the relevant .mo file for that translated page from myTheme/languages/ folder? I was able to achieve this by creating a function in functions.php file. But i'm not sure it is the right way especially when there are sub pages like example.com/en/about:


function change_wordpress_locale( $locale ) {

    if (is_page('en')) :
        load_textdomain( 'myTextDomain', get_template_directory() . '/languages/en_US.mo' );

    elseif (is_page('ru')) : 
        load_textdomain( 'myTextDomain', get_template_directory() . '/languages/ru_RU.mo' );

        load_textdomain( 'myTextDomain', get_template_directory() . '/languages/he_IL.mo' );
add_filter( 'wp', 'change_wordpress_locale' );


3) How to switch the WP function language_attributes(); to display the correct attributes on the html itself for those pages and sub-pages?

4) How can I achieve for example if you are currently on the en page and you click on a link to a page-slug "about" it will be example.com/en/about instead of example.com/about. I mean the links to be relative to the current language used by the frontend user?

5) How to target those languages to change some <head> <meta> for example?

For example <?php if ( is_page ('en') ) : ... endif; ?> or there some better way to do it?

6) How to translate categories and tags so they will be shown in the correct language?

Thanks a lot ahead for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.