Passed
Push — main ( e456c4...a9dbf5 )
by Thierry
03:02
created

Translator::trans()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 4
nc 6
nop 3
1
<?php
2
3
/**
4
 * Translator.php - Translator
5
 *
6
 * Provide translation service for strings in the Jaxon library.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Utils\Translation;
16
17
use function trim;
18
use function file_exists;
19
use function is_array;
20
use function str_replace;
21
use function array_map;
22
use function array_keys;
23
use function array_values;
24
25
class Translator
26
{
27
    /**
28
     * The default locale
29
     *
30
     * @var string
31
     */
32
    protected $sDefaultLocale = 'en';
33
34
    /**
35
     * The translations
36
     *
37
     * @var array
38
     */
39
    protected $aTranslations = [];
40
41
    /**
42
     * Set the default locale
43
     *
44
     * @param string $sLocale
45
     *
46
     * @return void
47
     */
48
    public function setLocale(string $sLocale)
49
    {
50
        if(($sLocale = trim($sLocale)))
51
        {
52
            $this->sDefaultLocale = $sLocale;
53
        }
54
    }
55
56
    /**
57
     * Recursively load translated strings from an array
58
     *
59
     * @param string $sLanguage The language of the translations
60
     * @param string $sPrefix The prefix for names
61
     * @param array $aTranslations The translated strings
62
     *
63
     * @return void
64
     */
65
    private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations)
66
    {
67
        foreach($aTranslations as $sKey => $xTranslation)
68
        {
69
            $sKey = trim($sKey);
70
            $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey;
71
            if(is_array($xTranslation))
72
            {
73
                // Recursively read the translations in the array
74
                $this->_loadTranslations($sLanguage, $sKey, $xTranslation);
75
            }
76
            else
77
            {
78
                // Save this translation
79
                $this->aTranslations[$sLanguage][$sKey] = $xTranslation;
80
            }
81
        }
82
    }
83
84
    /**
85
     * Load translated strings from a file
86
     *
87
     * @param string $sFilePath The file full path
88
     * @param string $sLanguage The language of the strings in this file
89
     *
90
     * @return void
91
     */
92
    public function loadTranslations(string $sFilePath, string $sLanguage)
93
    {
94
        if(!file_exists($sFilePath))
95
        {
96
            return;
97
        }
98
        $aTranslations = require($sFilePath);
99
        if(!is_array($aTranslations))
100
        {
101
            return;
102
        }
103
        // Load the translations
104
        if(!isset($this->aTranslations[$sLanguage]))
105
        {
106
            $this->aTranslations[$sLanguage] = [];
107
        }
108
        $this->_loadTranslations($sLanguage, '', $aTranslations);
109
    }
110
111
    /**
112
     * Get a translated string
113
     *
114
     * @param string $sText The key of the translated string
115
     * @param array $aPlaceHolders The placeholders of the translated string
116
     * @param string $sLanguage The language of the translated string
117
     *
118
     * @return string
119
     */
120
    public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string
121
    {
122
        $sText = trim($sText);
123
        if(empty($sLanguage))
124
        {
125
            $sLanguage = $this->sDefaultLocale;
126
        }
127
        if(!isset($this->aTranslations[$sLanguage][$sText]))
128
        {
129
            return $sText;
130
        }
131
        $sMessage = $this->aTranslations[$sLanguage][$sText];
132
        if(!empty($aPlaceHolders))
133
        {
134
            $aVars = array_map(function($sVar) {
135
                return ':' . $sVar;
136
            }, array_keys($aPlaceHolders));
137
            $sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage);
138
        }
139
        return $sMessage;
140
    }
141
}
142