RPeraltaJr
7/25/2019 - 2:30 PM

Setting Up Translations

Format

https://example.com?lang=[LANGUAGE_ABBR]

Example

Translating to French
https://example.com?lang=fr

Code

$language = $_GET['lang'];

include "translations/{$language}/homepage.php";

<div class="hero">
    <div class="container">
        <h1 class="title"><?php echo $content->hero['title']; ?></h1>
    </div>
</div>

Note: You'll also need to look into creating a function to set the default as English if a language is defined in the URL (see example below).

function current_language() {
  
    // array of translations available on webpage
    $lang_array = array('de','en','es','fr','it','pt','nl');  
    
    // default language
    $language = 'en'; 

    // get language from parameter
    if ( isset($_GET['lang']) && !empty($_GET['lang']) ) { 
        $language = $_GET['lang'];
    }
    // set to default if language not found in $lang_array
    if ( isset($_GET['lang']) && !in_array($_GET['lang'], $lang_array)) {
        $language = 'en';
    }
    return $language;

}