LocaleHelperTrait::getPrimaryLanguage()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 4
nc 3
nop 1
crap 4
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-translated-behavior
4
 * @copyright Copyright (c) 2015 LAV45!
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\translate;
10
11
use Locale;
12
13
trait LocaleHelperTrait
14
{
15
    /**
16
     * @var \Closure
17
     */
18
    public $primaryLanguage;
19
20
    /**
21
     * @param string $locale `en-EN`, `ru-RU`
22
     * @return string en or ru
23
     */
24 15
    public function getPrimaryLanguage($locale)
25
    {
26 15
        if ($this->primaryLanguage !== null && is_callable($this->primaryLanguage)) {
27 1
            return call_user_func($this->primaryLanguage, $locale);
28
        }
29 14
        return extension_loaded('intl') ?
30 14
            Locale::getPrimaryLanguage($locale) : substr($locale, 0, 2);
31
    }
32
}