aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/intl/Symfony/Component/Intl/ResourceBundle/LanguageBundle.php
blob: c449f9c1996cd9a3e8c8d6f31d2d9ad61b914f98 (plain) (blame)
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
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Intl\ResourceBundle;

/**
 * Default implementation of {@link LanguageBundleInterface}.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
{
    /**
     * {@inheritdoc}
     */
    public function getLanguageName($lang, $region = null, $locale = null)
    {
        if (null === $locale) {
            $locale = \Locale::getDefault();
        }

        if (null === ($languages = $this->readEntry($locale, array('Languages')))) {
            return null;
        }

        // Some languages are translated together with their region,
        // i.e. "en_GB" is translated as "British English"
        if (null !== $region && isset($languages[$lang.'_'.$region])) {
            return $languages[$lang.'_'.$region];
        }

        return $languages[$lang];
    }

    /**
     * {@inheritdoc}
     */
    public function getLanguageNames($locale = null)
    {
        if (null === $locale) {
            $locale = \Locale::getDefault();
        }

        if (null === ($languages = $this->readEntry($locale, array('Languages')))) {
            return array();
        }

        if ($languages instanceof \Traversable) {
            $languages = iterator_to_array($languages);
        }

        return $languages;
    }

    /**
     * {@inheritdoc}
     */
    public function getScriptName($script, $lang = null, $locale = null)
    {
        if (null === $locale) {
            $locale = \Locale::getDefault();
        }

        $data = $this->read($locale);

        // Some languages are translated together with their script,
        // e.g. "zh_Hans" is translated as "Simplified Chinese"
        if (null !== $lang && isset($data['Languages'][$lang.'_'.$script])) {
            $langName = $data['Languages'][$lang.'_'.$script];

            // If the script is appended in braces, extract it, e.g. "zh_Hans"
            // is translated as "Chinesisch (vereinfacht)" in locale "de"
            if (strpos($langName, '(') !== false) {
                list($langName, $scriptName) = preg_split('/[\s()]/', $langName, null, PREG_SPLIT_NO_EMPTY);

                return $scriptName;
            }
        }

        // "af" (Afrikaans) has no "Scripts" block
        if (!isset($data['Scripts'][$script])) {
            return null;
        }

        return $data['Scripts'][$script];
    }

    /**
     * {@inheritdoc}
     */
    public function getScriptNames($locale = null)
    {
        if (null === $locale) {
            $locale = \Locale::getDefault();
        }

        if (null === ($scripts = $this->readEntry($locale, array('Scripts')))) {
            return array();
        }

        if ($scripts instanceof \Traversable) {
            $scripts = iterator_to_array($scripts);
        }

        return $scripts;
    }
}