Completed
Push — master ( c72de8...38327d )
by Stefano
20s queued 13s
created

I18nTrait::getLang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace BEdita\I18n\Core;
14
15
use Cake\Core\Configure;
16
use Cake\I18n\I18n;
17
use Cake\Utility\Hash;
18
19
/**
20
 * Trait that helps with I18n stuff as locale and language configuration.
21
 */
22
trait I18nTrait
23
{
24
    /**
25
     * Proxy to `\Cake\I18n\I18n::getLocale()`.
26
     * Return the currently configure locale as stored in the `intl.default_locale` PHP setting.
27
     *
28
     * @return string The name of the default locale.
29
     *
30
     * @codeCoverageIgnore
31
     */
32
    public function getLocale() : string
33
    {
34
        return I18n::getLocale();
35
    }
36
37
    /**
38
     * Return the configured supported locales
39
     *
40
     * @codeCoverageIgnore
41
     */
42
    public function getLocales() : array
43
    {
44
        return (array)Configure::read('I18n.locales', []);
45
    }
46
47
    /**
48
     * Return an array of available languages.
49
     *
50
     * @return array
51
     *
52
     * @codeCoverageIgnore
53
     */
54
    public function getLanguages() : array
55
    {
56
        return (array)Configure::read('I18n.languages', []);
57
    }
58
59
    /**
60
     * Return the current lang usually set by `\BEdita\I18n\Middleware\I18nMiddleware`
61
     *
62
     * @return string|null
63
     *
64
     * @codeCoverageIgnore
65
     */
66
    public function getLang() : ?string
67
    {
68
        return Configure::read('I18n.lang');
69
    }
70
71
    /**
72
     * Return the language name as configured.
73
     *
74
     * @param string $lang The abbreviated lang
75
     * @return string|null
76
     */
77
    public function getLangName($lang = null) : ?string
78
    {
79
        if (empty($lang)) {
80
            $lang = Configure::read('I18n.default');
81
        }
82
83
        return Hash::get($this->getLanguages(), $lang);
84
    }
85
86
    /**
87
     * Return the default lang as configured.
88
     *
89
     * @return string|null
90
     *
91
     * @codeCoverageIgnore
92
     */
93
    public function getDefaultLang() : ?string
94
    {
95
        return Configure::read('I18n.default');
96
    }
97
}
98