<?php

$detection = function () {
    if (isset($_REQUEST['language'])) {
        switch ($_REQUEST['language']) {
            case 'en':
            case 'de':
                return $_REQUEST['language'];
        }

        return 'en';
    }

    echo '<script>

        var lang = "en";

        if ("language" in navigator) {
            lang = navigator.language;

        } else if ("browserLanguage" in navigator) {
            lang = navigator.browserLanguage;

        } else if ("systemLanguage" in navigator) {
            lang = navigator.systemLanguage;

        } else if ("userLanguage" in navigator) {
            lang = navigator.userLanguage;
        }

        lang = lang.substr(0, 2);

        switch (lang) {
            case "en":
            case "de":
                window.location = "?language="+ lang;
                break;
                
            default:
                window.location = "?language=en";
        }
    </script>';
    exit;
};

return $detection();
