Using Multi Language in Wordpress

Asked

Viewed 215 times

3

I’m developing a website in Wordpress, and need some content on the site (menus, photos and etc) to be in 3 different languages.

How is it possible to do this without creating a new Wordpress for each language?

  • You are looking for internationalization. I gave a detailed answer on this here

  • @Caiofelipepereira I don’t only want to translate, for that I would edit the . po of the template. I want to have different pages for different languages.

2 answers

2


I don’t know any native function of wordpress that works with multi-language, and when I need to use two ways, depending on what the job asks for.

I recommend using the plugin ACF for manageable content it will facilitate your life, besides being well documented its scope is also good even in free version. For method I I will use it as a reference, but if you did not want to use it you can use the native functions of wordpress, to create fields reaching the same result.


To capture the current language, selected by the user I use the following code in both methods:

<ul>
    <?php
        $lang = $_COOKIE['lang'];
        if ($lang == 'en'):
            $flag_1 = 'es';
            $flag_2 = 'pt-br';
        elseif ($lang == 'es'):
            $flag_1 = 'pt-br';
            $flag_2 = 'en';
        else:
            $flag_1 = 'en';
            $flag_2 = 'es';
        endif;
    ?>
    <li>
        <form method="post" action="">
            <input name="lang" type="hidden" value="<?php echo $flag_1; ?>" aria-hidden="true">
            <input name="redirect" type="hidden" value="<?php echo $_SERVER['REQUEST_URI']; ?>" aria-hidden="true">
            <input type="submit" role="button" value="<?php echo $flag_1; ?>">
        </form>
    </li>
    <li>
        <form method="post" action="">
            <input name="lang" type="hidden" value="<?php echo $flag_2; ?>" aria-hidden="true">
            <input name="redirect" type="hidden" value="<?php echo $_SERVER['REQUEST_URI']; ?>" aria-hidden="true">
            <input type="submit" role="button" value="<?php echo $flag_2; ?>">
        </form>
    </li>
</ul>

Yes, I use two <form>! In this example, there are 3 languages: Portuguese, English and Spanish, where after capturing the selected language each of <form> receives one of the languages not selected and can be exchanged in the future.

Inside functions.php insert the following code:

if (!isset($_COOKIE['lang'])) {
    setcookie('lang', 'pt-br', (time() + (3 * 24 * 3600)), '/');
    wp_redirect(site_url());
    die;
}
if (isset($_POST['lang'])) {
    setcookie('lang', $_POST['lang'], (time() + (3 * 24 * 3600)), '/');
    /*Rede
    wp_redirect(site_url($_POST['redirect']));*/
    //local
    wp_redirect(site_url());
    die;

}

Nothing much, I just attribute the value coming from post for a Cookie.


Method I:

This form duplicates the content fields inside the same page always using the same URL.

For example, using the ACF crio inside the "example" page three fields, "conteudo_br", "conteudo_es" and "conteudo_en", which refer to the same field, but each one is responsible for the content in each language.

In the code I do the following check:

<?php
    if ( $_COOKIE['lang'] == "en"):
        $conteudo = get_field('conteudo_en');
    elseif ($_COOKIE['lang'] == "es"):
        $conteudo = get_field('conteudo_es');
    else:
        $conteudo = get_field('conteudo_br');
    endif;
?>
<article>
    <h2>Título do conteúdo</h2>
    <?php echo $conteudo; ?>
</article>

Through the if attribute the variable $contents the field of the current language, verifying the Cookie.

Advantage of this method: I create only a menu, because the page will always be the same.

Disadvantage: For SEO, the page will always have the same name, for example the contact page, will always be "www.meudominio.com/contact" and will not have a "/contact" for example for English.

Method II:

You can duplicate the pages, where you would not need to create "conteudo_br", "conteudo_es" and "conteudo_en", just create a field, for example "content" and assign the same to the three pages (English, Spanish and Portuguese), where each one gets the respective content of the language.

In the code you would just have to check the current language of cookie and change the menu link instead of the content itself as the method I suggests.

Example:

<?php
    if ($_COOKIE['lang'] == 'en'):
        $item_1 = 'contact';
    elseif ($_COOKIE['lang'] == 'es'):
        $item_1 = 'contacto';
    else:
        $item_1 = 'contato';
    endif;
?>
<a href="<?php bloginfo('url'); echo '/'.$item_1; ?>"><?php echo $item_1; ?></a>

Above the code selects the href attributing to the $item_1 depending on the Cookie.

Advantage of this method: Content is easier to manage because the number of fields per page is lower.

Regarding SEO, each page will have its proper URL in each language.

Disadvantage: On sites that have many pages, this model can generate an excessive accumulation of pages, because for each page will be created 3, one for each language.


Finally, analyze what your work needs and use the form that best suits you.

Also if you prefer you can use some plugin that does all this work for you, can check some on the page of wordpress plugins.

If even after step by step you are experiencing difficulties, I suggest you replicate the content to three subdomains like es.site.com, en.site.com and site.com ... As much as I don’t recommend this method, due to content duplicity, it is also valid.

  • I understood, but how would I make a menu exist only in one language and in the others it does not exist? It would be a display: none or something like? The first code I didn’t understand very well, where would you put it? No index?

  • You mentioned that there are plugin’s that do this process, but would know if there are any that follows your same logic?

  • @Giovannibernini, in relation to the language you can simply insert the HTML code between the "if" of php, and then it will be showing only in that particular language, because with display None would be somewhat gambiarra. Regarding the first code goes from common sense, I usually put inside the header or Nav as is something that the user can change at any time.

  • Plugins are somewhat limited, so I wrote this giant text so you can do in "arm" since it is something very specific and I do not know any plugin that does it satisfactorily.

  • I understood, but I will have to hit my head a little to do, because I use WP a little while ago, I decided to migrate from Joomla! in the wrong project, rsrsrs. I get doubtful on that part of calling the pages. The page conteudo, that will load the code to call the pages in the correct language? But what would a "field" be another page? Would I create a "contacting" page plus 3 pages per language, and on the "contacting" page will be the code of your last example? This "contact" page would be a redirector?

  • Do 3 different Wordpress in subdominios, would not be a good solution right? (site.com, es.site.com, br.site.com). I wanted to have this difference between languages, in the url. But I believe that a "site.com/en/" would be easier.

  • A field would be a field, for example the text editor is a field, the title is a field which you can access via the_algumacoisa(), with ACF you can create custom fields more easily than the native form of wordpress, of a minimal read on and you were understanding it’s quite easy!

  • Maybe it’s @giovanniBernini, if you’re having trouble understanding step by step the best way would be to actually do a Sub-domain for each language, but I’ve had redirect problems because of subdominios, but you can try, is also a valid alternative!

  • I think I ended up understanding myself here, this piece of code that you gave as an example in method 2, it would have to exist in the 3 pages? What I’m getting into is, where would that last code be? I currently already have the pages for each language assembled, so I believe that this method 2 serious better and easier. I don’t know if I could explain my doubt this time more clearly, but thank you for your help!!

Show 5 more comments

1

  • But can I edit some things like the examples I mentioned? Or is it just to translate the posts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.