<?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$
 *
 * 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 users to change
 * their 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_DATA['action']) && ($REQUEST_DATA['action'] == ACTION_UPDATE_OK) &&
    (isset($REQUEST_DATA['id'])) &&
    ($_SESSION['auth_user']['usr_id'] == $REQUEST_DATA['id'])) {

    if (isset($REQUEST_DATA['lng_id']) && is_numeric($REQUEST_DATA['lng_id'])) {
        $lng_code = phpaga_getLanguageCode($REQUEST_DATA['lng_id']);
        $_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']) || !strlen($_SESSION['auth_user']['lng_code']))

    $_SESSION['auth_user']['lng_code'] = 'en';

if ((basename($_SERVER['PHP_SELF']) == 'install.php') && isset($REQUEST_DATA['lang']) && strlen($REQUEST_DATA['lang']))
    $phpaga_selected_language = $REQUEST_DATA['lang'];
else
    $phpaga_selected_language = $_SESSION['auth_user']['lng_code'];

if (!strlen($phpaga_selected_language))
    die("Unable to determine the selected language. Please report this error.");

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

if ($langname === false)
    die(sprintf("Unable to set LC_ALL to '%s' make sure the desired locale is installed.", $phpaga_languages[$phpaga_selected_language]));

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);

?>
