summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/i18n/src/Locale.php
blob: 48e345f338d091b9277d06e878eb7a0e9eb39320 (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
<?php

namespace ipl\I18n;

use ipl\Stdlib\Str;
use stdClass;

class Locale
{
    /** @var string Default locale code */
    protected $defaultLocale = 'en_US';

    /**
     * Get the default locale
     *
     * @return string
     */
    public function getDefaultLocale()
    {
        return $this->defaultLocale;
    }

    /**
     * Set the default locale
     *
     * @param string $defaultLocale
     *
     * @return $this
     */
    public function setDefaultLocale($defaultLocale)
    {
        $this->defaultLocale = $defaultLocale;

        return $this;
    }

    /**
     * Return the preferred locale based on the given HTTP header and the available translations
     *
     * @param string $header    The HTTP "Accept-Language" header
     * @param array<string> $available Available translations
     *
     * @return string The browser's preferred locale code
     */
    public function getPreferred($header, array $available)
    {
        $headerValues = Str::trimSplit($header, ',');
        for ($i = 0; $i < count($headerValues); $i++) {
            // In order to accomplish a stable sort we need to take the original
            // index into account as well during element comparison
            $headerValues[$i] = [$headerValues[$i], $i];
        }
        usort( // Sort DESC but keep equal elements ASC
            $headerValues,
            function ($a, $b) {
                $tagA = Str::trimSplit($a[0], ';', 2);
                $tagB = Str::trimSplit($b[0], ';', 2);
                $qValA = (float) (strpos($a[0], ';') > 0 ? substr(array_pop($tagA), 2) : 1);
                $qValB = (float) (strpos($b[0], ';') > 0 ? substr(array_pop($tagB), 2) : 1);

                return $qValA < $qValB ? 1 : ($qValA > $qValB ? -1 : ($a[1] > $b[1] ? 1 : ($a[1] < $b[1] ? -1 : 0)));
            }
        );
        for ($i = 0; $i < count($headerValues); $i++) {
            // We need to reset the array to its original structure once it's sorted
            $headerValues[$i] = $headerValues[$i][0];
        }
        $requestedLocales = [];
        foreach ($headerValues as $headerValue) {
            if (strpos($headerValue, ';') > 0) {
                $parts = Str::trimSplit($headerValue, ';', 2);
                $headerValue = $parts[0];
            }
            $requestedLocales[] = str_replace('-', '_', $headerValue);
        }
        $requestedLocales = array_combine(
            array_map('strtolower', array_values($requestedLocales)),
            array_values($requestedLocales)
        );

        $available[] = $this->defaultLocale;
        $availableLocales = array_combine(
            array_map('strtolower', array_values($available)),
            array_values($available)
        );

        $similarMatch = null;

        foreach ($requestedLocales as $requestedLocaleLowered => $requestedLocale) {
            $localeObj = $this->parseLocale($requestedLocaleLowered);

            if (
                isset($availableLocales[$requestedLocaleLowered])
                && (! $similarMatch || $this->parseLocale($similarMatch)->language === $localeObj->language)
            ) {
                // Prefer perfect match only if no similar match has been found yet or the perfect match is more precise
                // than the similar match
                return $availableLocales[$requestedLocaleLowered];
            }

            if (! $similarMatch) {
                foreach ($availableLocales as $availableLocaleLowered => $availableLocale) {
                    if ($this->parseLocale($availableLocaleLowered)->language === $localeObj->language) {
                        $similarMatch = $availableLocaleLowered;
                        break;
                    }
                }
            }
        }

        return $similarMatch ? $availableLocales[$similarMatch] : $this->defaultLocale;
    }

    /**
     * Parse a locale into its subtags
     *
     * Converts to output of {@link \Locale::parseLocale()} to an object and returns it.
     *
     * @param string $locale
     *
     * @return stdClass Output of {@link \Locale::parseLocale()} converted to an object
     */
    public function parseLocale($locale)
    {
        return (object) \Locale::parseLocale($locale);
    }
}