aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs/includes/i18n.php
blob: 37dee90436c320d2e25e003eedb761a7014071a3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * Functions to translate demo.armandphilippot.com
 *
 * @package   DemoArmandphilippotCom
 * @author    Armand Philippot <contact@armandphilippot.com>
 * @copyright 2021 Armand Philippot
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
 * @link      https://demo.armandphilippot.com/
 * @since     1.0.0
 */

/**
 * Get a list of available translations.
 *
 * @return array The available languages.
 */
function dap_get_available_languages()
{
    $languages_dir = glob('languages' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
    $languages     = str_replace('languages' . DIRECTORY_SEPARATOR, '', $languages_dir);

    return $languages;
}

/**
 * Get accepted languages based on HTTP header.
 *
 * @return array The accepted languages.
 */
function dap_get_languages_from_http()
{
    $languages = '';

    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        $languages = preg_split('/(,|;)/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], -1, PREG_SPLIT_NO_EMPTY);
        $languages = array_filter(
            $languages,
            function ($value) {
                return substr($value, 0, 2) !== 'q=';
            }
        );
    }

    return $languages;
}

/**
 * Check if a locale is a valid locale.
 *
 * @param string $locale The locale to test.
 * @param array  $valid_locales The valid locales.
 * @return boolean True if the locale exists.
 */
function dap_is_valid_locale(string $locale, array $valid_locales) : bool
{
    if (empty($locale)) {
        return false;
    }

    return in_array($locale, $valid_locales, true);
}

/**
 * Filter the accepted locales to determine the locale to use.
 *
 * @param array  $locales The locales to test.
 * @param string $default_locale The default locale.
 * @return string The first valid locale.
 */
function dap_get_valid_locale(array $locales, string $default_locale) : string
{
    $valid_locales = dap_get_available_languages();

    if (isset($default_locale) && dap_is_valid_locale($default_locale, $valid_locales)) {
        $default_locale = $default_locale;
    } else {
        $default_locale = 'en_US';
    }

    $possible_locales = array_intersect($locales, $valid_locales);
    $possible_locales = array_values($possible_locales);
    $locale           = '';

    if (count($possible_locales) > 0) {
        $locale = $possible_locales[0];
    } else {
        $locale = $default_locale;
    }

    return $locale;
}

/**
 * Define the locale to use.
 *
 * @param string $default_locale The default locale.
 * @return string The locale to use.
 */
function dap_define_locale(string $default_locale)
{
    $http_language = dap_get_languages_from_http();
    $http_locale   = str_replace('-', '_', $http_language);
    $http_locale = is_array($http_locale) ? $http_locale : array($http_locale);
    $locale        = dap_get_valid_locale($http_locale, $default_locale);

    return $locale;
}

/**
 * Get a list of possible format based on the locale to use.
 *
 * @param string $locale The locale to use.
 * @return array The locale in different formats to use.
 */
function dap_get_formatted_locale(string $locale)
{
    $formatted_locale   = array( $locale );
    $formatted_locale[] = $locale . '.utf8';
    $formatted_locale[] = $locale . '.UTF-8';
    $formatted_locale[] = strtok($locale, '_');

    return $formatted_locale;
}

/**
 * Transform a locale format (ex: `en_US`) to a language format (ex: `en-US`).
 *
 * @param string $locale The used locale.
 * @return string The language to use.
 */
function dap_get_language(string $locale)
{
    $language = str_replace('_', '-', $locale);

    return $language;
}

/**
 * Set the locale.
 *
 * @param array $formatted_locale The locale with its different formats.
 */
function dap_set_locale(array $formatted_locale)
{
    setlocale(LC_ALL, $formatted_locale);
    bindtextdomain('DAP', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'languages');
    bind_textdomain_codeset('DAP', 'UTF-8');
    textdomain('DAP');
}