<?php
/**
 * phpaga
 *
 * Language / locale
 *
 * This file takes care of including the right language file for the current
 * user.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: language.php 627 2005-09-06 20:39:59Z worf $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* the following hack has been inserted to allow the user to change
 * her language settings and see the changes immediately. We should
 * find a better way to do this, but it works for the moment.
 */

if (isset ($_REQUEST["action"]) && ($_REQUEST["action"] == ACTION_UPDATE_OK) &&
    (isset($_REQUEST["id"])) &&
    ($_SESSION["auth_user"]["usr_id"] == $_REQUEST["id"]))
{
    if (isset($_REQUEST["lng_id"]) && is_numeric($_REQUEST["lng_id"]))
    {
        $result = phpaga_getLanguageCode($_REQUEST["lng_id"], $lng_code);
        $_SESSION["auth_user"]["lng_code"] = $lng_code;
    }
}

/* normal language detection routine follows */

if (!isset ($_SESSION["auth_user"]) ||
    !is_array ($_SESSION["auth_user"]) ||
    !isset ($_SESSION["auth_user"]["lng_code"]))
{
    $_SESSION["auth_user"]["lng_code"] = "en";
}

if ((basename($_SERVER["PHP_SELF"]) == "install.php")
    && isset($_REQUEST["lang"])
    && strlen($_REQUEST["lang"]))
{
    $phpaga_selected_language = $_REQUEST["lang"];
}
else
{
    $phpaga_selected_language = $_SESSION["auth_user"]["lng_code"];
}

$langname = setlocale(LC_ALL, $phpaga_languages[$phpaga_selected_language]);

if ($langname === false)
    die("Unable to set LC_ALL to $langname");

if (strstr(strtoupper(PHP_OS), 'WIN'))
{
    putenv("LANG=".$langname);
    putenv("LANGUAGE=".$langname);
}

define("PHPAGA_LANGNAME", $langname);
putenv("LC_ALL=$langname");
putenv("LC_NUMERIC=C");
putenv("LC_TIME=C");
putenv("LC_MONETARY=C");

if (setlocale(LC_NUMERIC, "C") === false)
    die("Unable to set LC_NUMERIC to C");

if (setlocale(LC_TIME, "C") === false)
    die("Unable to set LC_TIME to C");

if (setlocale(LC_MONETARY, "C") === false)
    die("Unable to set LC_MONETARY to C");

textdomain("messages");
bindtextdomain("messages", PHPAGA_LNGPATH);

?>
