-2

I am making a html for showing a "Problem Statement" of a contest. The problem has translation in 2 language English and Bengali. I can make this using 2 htmls. But how can I have like this -
desc.html?lang=en or desc.html?lang=bn or some other kind so that I can tell the page to show a specific language?

I am a newbie in web development.

Fabio
  • 23,183
  • 12
  • 55
  • 64
Rezwan Arefin
  • 177
  • 1
  • 11
  • Do you currently have multiple pages for those languages? – rrr Dec 31 '16 at 14:53
  • @rpi Yes. But I want to have only one for 2 – Rezwan Arefin Dec 31 '16 at 14:54
  • That would require some work with PHP. If you're using just URL to detect language, then with `$_GET` find which one (and have default, just in case) it is and include file that contains all constants. Use those constants in HTML file. – Gynteniuxas Dec 31 '16 at 14:56
  • Possible duplicate of [Best way to internationalize simple PHP website](http://stackoverflow.com/questions/6953528/best-way-to-internationalize-simple-php-website) – cb0 Dec 31 '16 at 14:57

1 Answers1

2

Say, you have 2 files currently, index-english.html and index-german.html

You now create a file called index-global.php. Whether the first two pages are fully HTML or contain PHP, doesn't matter, but you should keep the current extension.Thanks to fred-ii for mentioning that you should change the extension to .php for the HTML-pages

In the index-global.php you'll place the following code

if (isset($_GET['lang']) AND $_GET['lang'] == "en") require("index-english.html");
elseif (isset($_GET['lang']) AND $_GET['lang'] == "de") require("index-german.html");
else {
    header("Location: /?lang=en"); //redirect if no language specified
    exit();
}

A better way would be to check the language automatically based on the Accept-Language-header, for example

$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
    if($language == "en") {
        require("index-english.html");
        exit();
    }
    elseif($language == "de") {
        require("index-german.html");
        exit();
    }
}
require("index-english.html");
exit();

In this case the asker wanted to include all HTML in one file

$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
    if($language == "en") {
        ?>English HTML content here<?php
        exit();
    }
    elseif($language == "de") {
        ?>German HTML content here<?php
        exit();
    }
}
?>English HTML content here<?php
exit();
Community
  • 1
  • 1
rrr
  • 412
  • 4
  • 10