Completed
Push — master ( b7fd28...33de19 )
by Aitor Riba
06:14 queued 03:57
created

Builder::transArray()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 6.9811
cc 7
eloc 14
nc 8
nop 2
1
<?php
2
3
namespace Aitor24\Laralang;
4
5
use Aitor24\Laralang\Builder\ApertiumTrans;
6
use Aitor24\Laralang\Builder\GoogleTrans;
7
use Aitor24\Laralang\Builder\MymemoryTrans;
8
use Aitor24\Laralang\Models\DB_Translation;
9
10
class Builder
11
{
12
    /**
13
     * Get the translation.
14
     *
15
     * @param string $string
16
     *
17
     * @return object
18
     */
19
    public static function trans($string, $vars = [])
20
    {
21
        $translator = config('laralang.default.translator');
22
        if (!in_array(config('laralang.default.translator'), ['apertium', 'mymemory', 'google'])) {
23
            return "<font style='color:red;'>Laralang doesn't support $translator translator. Check config</font>";
24
        } else {
25
            if (config('laralang.default.translator') == 'mymemory') {
26
                return new MymemoryTrans($string, $vars);
27
            } elseif (config('laralang.default.translator') == 'apertium') {
28
                return new ApertiumTrans($string, $vars);
29
            } elseif (config('laralang.default.translator') == 'google') {
30
                return new GoogleTrans($string, $vars);
31
            }
32
        }
33
    }
34
35
    /**
36
     * Generate files json for a specify package.
37
     *
38
     * @param string $string
0 ignored issues
show
Bug introduced by
There is no parameter named $string. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     *
40
     * @return object
41
     */
42
    public static function generateTranslations($is_package, $package, $translationsPath, $toLangs = 'es')
43
    {
44
        if ($is_package) {
45
            $path = realpath(__DIR__.'./../../../'.$package.$translationsPath);
46
        } else {
47
            $path = resource_path().'/'.$translationsPath;
48
        }
49
        $toLangs = explode('|', $toLangs);
50
        $files = glob($path.'/*.php');
51
        foreach ($files as $file) {
52
            foreach ($toLangs as $lang) {
53
                $lines = include $file;
54
                $array = self::transArray($lines, $lang);
55
                $data = '<?php return '.str_replace(')', ']', str_replace('array (', '[', str_replace(")'", ']', str_replace("'array (", '[', $array)))).";\n";
56
57
                $savePath = $path.'./../'.$lang.'/';
58
                if (!file_exists($savePath)) {
59
                    mkdir($savePath, 0777, true);
60
                }
61
                file_put_contents($savePath.basename($file), $data);
62
            }
63
        }
64
    }
65
66
    private static function transArray($array, $lang)
67
    {
68
        foreach ($array as $var => $text) {
69
            $vars = [];
70
            if (is_array($text)) {
71
                $array[$var] = self::transArray($text, $lang);
72
            } else {
73
                foreach (explode(' ', $text) as $word) {
74
                    if (substr($word, 0, 1) == ':' && !in_array($word, $vars)) {
75
                        array_push($vars, $word);
76
                    }
77
                }
78
                $entry = $text;
79
                foreach ($vars as $key => $varreplace) {
80
                    $entry = str_replace($varreplace, 'VAR_'.$key, $entry);
81
                }
82
                $array[$var] = self::trans($entry, $vars)->from('en')->to($lang)->Save(false)->__toString();
83
            }
84
        }
85
86
        return stripslashes(var_export($array, true));
87
    }
88
89
    /**
90
     * Get the languages that translations has been translated.
91
     *
92
     * @return array
93
     */
94 View Code Duplication
    public static function toLanguages()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $locales = [];
97
        $translations = DB_Translation::distinct()->select('to_lang')->get();
98
        foreach ($translations as $object) {
99
            array_push($locales, $object->to_lang);
100
        }
101
102
        return $locales;
103
    }
104
105
    /**
106
     * Get the languages from translations has been translated.
107
     *
108
     * @return array
109
     */
110 View Code Duplication
    public static function fromLanguages()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $locales = [];
113
        $translations = DB_Translation::distinct()->select('from_lang')->get();
114
        foreach ($translations as $object) {
115
            array_push($locales, $object->from_lang);
116
        }
117
118
        return $locales;
119
    }
120
121
    /**
122
     * Get all the available languages.
123
     *
124
     * @return array
125
     */
126
    public static function allLanguages()
127
    {
128
        return [
129
            'en' => 'English',
130
            'es' => 'Spanish',
131
            'ca' => 'Catalan',
132
            'pt' => 'Portuguese',
133
            'zh' => 'Chinese',
134
            'ja' => 'Japanese',
135
            'de' => 'German',
136
            'fr' => 'French',
137
            'eu' => 'Basque',
138
            'ru' => 'Russian',
139
        ];
140
    }
141
}
142