Completed
Pull Request — master (#137)
by Philipp
04:15
created

i12n.php ➔ testlocale()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
// Copyright (C) 2014-2016 Universitätsbibliothek Mannheim
4
// See file LICENSE for license details.
5
6
// Test whether the script was called directly (used for unit test).
7
if (!isset($unittest)) {
8
    $unittest = array();
9
}
10
$unittest[__FILE__] = (sizeof(get_included_files()) == 1);
11
12
require_once('php-gettext/gettext.inc');
13
14
    // The default translations are in locale/en_US.UTF-8/LC_MESSAGES/palma.mo.
15
    $locale = '';
16
    if (isset($_REQUEST['lang'])) {
17
        // User requested language by URL parameter.
18
        $locale = $_REQUEST['lang'];
19
        $_SESSION['lang'] = $locale;
20
    } else if (isset($_SESSION['lang'])) {
21
        // Get language from session data.
22
        $locale = $_SESSION['lang'];
23
    } else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
24
        // Get language from browser settings.
25
        $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
26
    }
27
    switch (substr($locale, 0, 2)) {
28
    case 'al':
29
        // Albanian.
30
        $locale = 'al_AL.UTF-8';
31
        break;
32
    case 'ar':
33
        // Arabic.
34
        $locale = 'ar.UTF-8';
35
        break;
36
    case 'de':
37
        // German.
38
        $locale = 'de_DE.UTF-8';
39
        break;
40
    case 'en':
41
        // English.
42
        $locale = 'en_US.UTF-8';
43
        break;
44
    case 'es':
45
        // Spanish.
46
        $locale = 'es_ES.UTF-8';
47
        break;
48
    case 'it':
49
        // Italian.
50
        $locale = 'it_IT.UTF-8';
51
        break;
52
    case 'ru':
53
        // Russian.
54
        $locale = 'ru_RU.UTF-8';
55
        break;
56
    case 'ur':
57
        // Urdu.
58
        $locale = 'ur_PK.UTF-8';
59
        break;
60
    case 'zh':
61
        // Chinese.
62
        $locale = 'zh_CN.UTF-8';
63
        break;
64
    default:
65
        $locale = 'en_US.UTF-8';
66
        break;
67
    }
68
    //~ error_log("setlocale $locale");
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
    putenv("LANG=$locale");
70
    _setlocale(LC_MESSAGES, $locale);
71
    _bindtextdomain('palma', 'locale');
72
    _bind_textdomain_codeset('palma', 'UTF-8');
73
    _textdomain('palma');
74
75
if ($unittest[__FILE__]) {
76
77
    function testlocale($locale = false)
78
    {
79
        if ($locale) {
80
            _setlocale(LC_MESSAGES, $locale);
81
        }
82
        error_log(sprintf('%-12s ', ($locale ? $locale : 'default') . ':') . __('Screen section'));
83
    }
84
85
    // Run unit test.
86
87
    if (locale_emulation()) {
88
        print "locale '$locale' is not supported on your system, using custom gettext implementation.\n";
89
    }
90
    else {
91
        print "locale '$locale' is supported on your system, using native gettext implementation.\n";
92
    }
93
94
    testlocale();
95
    testlocale('al_AL.UTF-8');
0 ignored issues
show
Documentation introduced by
'al_AL.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
    testlocale('ar.UTF-8');
0 ignored issues
show
Documentation introduced by
'ar.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
    testlocale('de_DE.UTF-8');
0 ignored issues
show
Documentation introduced by
'de_DE.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
    testlocale('en_US.UTF-8');
0 ignored issues
show
Documentation introduced by
'en_US.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
    testlocale('es_ES.UTF-8');
0 ignored issues
show
Documentation introduced by
'es_ES.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
    testlocale('it_IT.UTF-8');
0 ignored issues
show
Documentation introduced by
'it_IT.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
    testlocale('ru_RU.UTF-8');
0 ignored issues
show
Documentation introduced by
'ru_RU.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
    testlocale('ur_PK.UTF-8');
0 ignored issues
show
Documentation introduced by
'ur_PK.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
    testlocale('zh_CN.UTF-8');
0 ignored issues
show
Documentation introduced by
'zh_CN.UTF-8' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
}
105