Completed
Push — master ( 7bf2c4...ed6650 )
by Danilo
03:26
created

File::loadSingleLanguage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBotFramework\Localization;
4
5
trait File {
6
7
    /**
8
     * \addtogroup Localization Localization
9
     * \brief Methods to create a localized bot.
10
     * \details Localization files must have this syntax:
11
     * file <code>./localization/en.json</code>:
12
     *
13
     *     {"Hello_Msg": "Hello"}
14
     *
15
     * File <code>./localization/it.json</code>:
16
     *
17
     *     {"Hello_Msg": "Ciao"}
18
     *
19
     * Usage in <code>processMessage()</code>:
20
     *
21
     *     $sendMessage($this->local[$this->language]["Hello_Msg"]);
22
     *
23
     * @{
24
     */
25
26
    /** \brief Store the localizated strings. */
27
    protected $local;
28
29
    /** \brief Directory where there are the localization files. */
30
    protected $localization_dir = './localization';
31
32
    /**
33
     * \brief Load a localization file into the localized strings array.
34
     * @param $lang Language to load.
35
     * @param $dir Directory in which there are the JSON files.
36
     * @return True if loaded. False if already loaded.
37
     */
38
    protected function loadSingleLanguage(string $lang = 'en', $dir = './localization') : bool {
39
40
        // Name of the file
41
        $filename = "$dir/$lang";
42
43
        // If this language isn't already set and the file exists
44
        if (!isset($this->local[$lang]) && file_exists($filename)) {
45
46
            // Load localization in memory
47
            $this->local[$lang] = json_decode(file_get_contents($filename), true);
48
49
            // We loaded it
50
            return true;
51
52
        }
53
54
        return false;
55
56
    }
57
58
    /**
59
     * \brief Load all localization files (JSON-serialized) from a folder and set them in $local variable.
60
     * \details Save all localization files, saved as json format, from a directory and put the contents in $local variable.
61
     * Each file will be saved into $local with the first two letters of the filename as the index.
62
     * @param $dir Directory where the localization files are saved.
63
     */
64
    public function loadLocalization($dir = './localization') {
65
66
        // Open directory
67
        if ($handle = opendir($dir)) {
68
69
            // Iterate over all files
70
            while (false !== ($file = readdir($handle))) {
71
72
                // If the file is a JSON data file
73
                if (strlen($file) > 6 && substr($file, -5) === '.json') {
74
75
                    try {
76
77
                        // Add the contents of the file to the $local variable, after deserializng it from JSON format
78
                        // The contents will be added with the 2 letter of the file as the index
79
                        $this->local[substr($file, 0, 2)] = json_decode(file_get_contents("$dir/$file"), true);
80
81
                    } catch (BotException $e) {
0 ignored issues
show
Bug introduced by
The class PhpBotFramework\Localization\BotException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
82
83
                        echo $e->getMessage();
84
85
                    }
86
87
                }
88
89
            }
90
91
        }
92
93
    }
94
95
    public function setLocalizationDir($dir) {
96
97
        $this->localization_dir = $dir;
98
99
    }
100
101
    /** @} */
102
103
}
104