Using multi language in Wordpress

3

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

How can you do this without having to create a new Wordpress for each language?

    
asked by anonymous 09.11.2015 / 18:13

2 answers

2

I do not know any native wordpress function that works with multi-language, and when needed I use two ways, depending on what the job asks for.

I recommend that you use the ACF plugin for manageable content that will make your life easier, as well as being well documented. even in free version. For the method I I will use it as a reference, but if I did not want to use it you could use the native functions of wordpress , to create fields arriving at 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 the <form> receives one of the languages not selected, and can be changed in the future.

Inside the functions.php I enter 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 assign the value coming from post to a Cookie .

Method I:

In this way duplicate the content fields within a same page using the same URL at all times.

For example, using the ACF, I create 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 make the following verification:

<?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>

If I assign the variable $ content to the current language field, checking the Cookie .

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

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

Method II:

You can duplicate the pages, where you do 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 stays with the respective content of the language.

In the code you just have to check the current cookie language and switch 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 href by assigning $ item_1 depending on Cookie .

Advantage of this method: The content is easier to manage because the number of fields per page is less.

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

Disadvantage: In sites that have many pages, this template 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, you can check some on the wordpress plugins page a>.

If even though you're experiencing difficulties, I suggest you replicate the content to three subdomains, such as es.site.com, en.site.com, and site.com. Although I do not recommend this method, due to duplicity of content, it is also valid.

    
09.11.2015 / 19:52
1

Try this plugin, it has several languages link

    
09.11.2015 / 19:41