aboutsummaryrefslogtreecommitdiffstats
path: root/localeDetection.class.php
blob: 40e9862e4ac9d8e14c0182d53dc80a5526405c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

/* ChooseLocale
 *
 * Licence: MPL 2/GPL 2.0/LGPL 2.1
 * Author: Pascal Chevrel, Mozilla
 * Date : 2010-07-17
 *
 * Description:
 * Class to choose the locale which locale we will show to the visitor
 * based on http acceptlang headers and our list of supported locales.
 *
 *
*/




class ChooseLocale
{
    public    $HTTPAcceptLang;
    public    $supportedLocales;
    protected $detectedLocale;
    protected $defaultLocale;
    public    $mapLonglocales;


    public function __construct($list=array('en-US'))
    {
        $this -> HTTPAcceptLang   = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $this -> supportedLocales = array_unique($list);
        $this -> setDefaultLocale('en-US');
        $this -> setCompatibleLocale();
        $this -> mapLonglocales = true;

    }

    public function getAcceptLangArray()
    {
        if (empty($this->HTTPAcceptLang)) return null;

        return explode(',', $this->HTTPAcceptLang);
    }

    public function getCompatibleLocale()
    {
        $l       = $this -> defaultLocale;
        $acclang = $this -> getAcceptLangArray();

        if(!is_array($acclang)) {
            return $this -> defaultLocale;
        }

        foreach ($acclang as $var) {
            $locale      = $this -> _cleanHTTPlocaleCode($var);
            $shortLocale = array_shift(explode('-', $locale));

            if (in_array($locale, $this -> supportedLocales)) {
                $l = $locale;
                break;
            }

            if (in_array($shortLocale, $this -> supportedLocales)) {
                $l = $shortLocale;
                break;
            }

            // check if we map visitors short locales to site long locales
            // like en -> en-GB
            if ($this -> mapLonglocales == true) {
                foreach ($this -> supportedLocales as $var) {
                    $shortSupportedLocale = array_shift(explode('-', $var));
                    if ($shortLocale == $shortSupportedLocale) {
                        $l = $var;
                        break;
                    }
                }
            }

            }

        return $l;
    }

    public function getDefaultLocale() {
        return $this -> defaultLocale;
    }

    public function setCompatibleLocale() {
        $this -> detectedLocale  = $this -> getCompatibleLocale();
    }

    public function setDefaultLocale($locale) {

        // the default locale should always be among the site locales
        // if not, the first locale in the supportedLocales array is default
        if (!in_array($locale, $this -> supportedLocales)) {
            $this -> defaultLocale = $this -> supportedLocales[0];

        } else {
            $this -> defaultLocale = $locale;
        }
        return;
    }

    private function _cleanHTTPlocaleCode($str)
    {
        $locale = explode(';', $str);
        $locale = trim($locale[0]);

        return $locale;
    }

}
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 *
 * Copyright 2000 MandrakeSoft
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Portions from Erik Troan (ewt@redhat.com)
 *
 * Copyright 1996 Red Hat Software 
 *
 */


/*
 * Each different frontend must implement all functions defined in frontend.h
 */


#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include "newt/newt.h"

#include <probing.h>

#include "frontend.h"

void init_frontend(char * welcome_msg)
{
	int i;
	for (i=0; i<38; i++) printf("\n");
	newtInit();
	newtCls();
	
	newtDrawRootText(0, 0, welcome_msg);
	
	newtPushHelpLine(" <Alt-F1> for here, <Alt-F3> to see the logs, <Alt-F4> for kernel msg");
	newtRefresh();
}


void finish_frontend(void)
{
	newtFinished();
}


void verror_message(char *msg, va_list ap)
{
	newtWinMessagev("Error", "Ok", msg, ap);
}

void vinfo_message(char *msg, va_list ap)
{
	newtWinMessagev("Notice", "Ok", msg, ap);
}


void vwait_message(char *msg, va_list ap)
{
	int width, height;
	char * title = "Please wait...";
	newtComponent c, f;
	newtGrid grid;
	char * buf = NULL;
	char * flowed;
	int size = 0;
	int i = 0;
	
	do {
		size += 1000;
		if (buf) free(buf);
		buf = malloc(size);
		i = vsnprintf(buf, size, msg, ap);
	} while (i >= size || i == -1);

	flowed = newtReflowText(buf, 60, 5, 5, &width, &height);
	
	c = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
	newtTextboxSetText(c, flowed);

	grid = newtCreateGrid(1, 1);
	newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, c, 0, 0, 0, 0, 0, 0);
	newtGridWrappedWindow(grid, title);

	free(flowed);
	free(buf);

	f = newtForm(NULL, NULL, 0);
	newtFormAddComponent(f, c);

	newtDrawForm(f);
	newtRefresh();
	newtFormDestroy(f);
}

void remove_wait_message(void)
{
	newtPopWindow();
}


static newtComponent form = NULL, scale = NULL;
static int size_progress;
static int actually_drawn;
static char * msg_progress;

void init_progression(char *msg, int size)
{
	size_progress = size;
	if (size) {
		actually_drawn = 0;
		newtCenteredWindow(70, 5, "Please wait...");
		form = newtForm(NULL, NULL, 0);
		newtFormAddComponent(form, newtLabel(1, 1, msg));
		scale = newtScale(1, 3, 68, size);
		newtFormAddComponent(form, scale);
		newtDrawForm(form);
		newtRefresh();
	}
	else {
		wait_message(msg);
		msg_progress = msg;
	}
}

void update_progression(int current_size)
{
	if (size_progress) {
		if (current_size <= size_progress)
			newtScaleSet(scale, current_size);
		newtRefresh();
	}
	else {
		struct timeval t;
		int time;
		static int last_time = -1;
		gettimeofday(&t, NULL);
		time = t.tv_sec*3 + t.tv_usec/300000;
		if (time != last_time) {
			char msg_prog_final[500];
			sprintf(msg_prog_final, "%s (%d bytes read) ", msg_progress, current_size);
			remove_wait_message();
			wait_message(msg_prog_final);
		}
		last_time = time;
	}
}

void end_progression(void)
{
	if (size_progress) {
		newtPopWindow();
		newtFormDestroy(form);
	}
	else
		remove_wait_message();
}


enum return_type ask_from_list_comments(char *msg, char ** elems, char ** elems_comments, char ** choice)
{
	char * items[500];
	int answer = 0, rc;
	char ** sav_elems = elems;
	int i;

	probe_that_type(USB_CONTROLLERS, BUS_USB); // we'd need the keyboard for interactions so...

	i = 0;
	while (elems && *elems) {
		int j = (*elems_comments) ? strlen(*elems_comments) : 0;
		items[i] = malloc(sizeof(char) * (strlen(*elems) + j + 4));
		strcpy(items[i], *elems);
		if (*elems_comments) {
			strcat(items[i], " (");
			strcat(items[i], *elems_comments);
			strcat(items[i], ")");
		}
		elems_comments++;
		i++;
		elems++;
	}
	items[i] = NULL;

	rc = newtWinMenu("Please choose...", msg, 52, 5, 5, 7, items, &answer, "Ok", "Cancel", NULL);

	if (rc == 2)
		return RETURN_BACK;

	*choice = strdup(sav_elems[answer]);

	return RETURN_OK;
}


enum return_type ask_from_list(char *msg, char ** elems, char ** choice)
{
	int answer = 0, rc;

	probe_that_type(USB_CONTROLLERS, BUS_USB); // we'd need the keyboard for interactions so...

	rc = newtWinMenu("Please choose...", msg, 52, 5, 5, 7, elems, &answer, "Ok", "Cancel", NULL);

	if (rc == 2)
		return RETURN_BACK;

	*choice = strdup(elems[answer]);

	return RETURN_OK;
}


enum return_type ask_yes_no(char *msg)
{
	int rc;

	probe_that_type(USB_CONTROLLERS, BUS_USB); // we'd need the keyboard for interactions so...

	rc = newtWinTernary("Please answer...", "Yes", "No", "Back", msg);

	if (rc == 1)
		return RETURN_OK;
	else if (rc == 3)
		return RETURN_BACK;
	else return RETURN_ERROR;
}


static void (*callback_real_function)(char ** strings) = NULL;