Completed
Push — master ( f10304...059713 )
by Thierry
02:01
created

Translator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 21
rs 9.3142
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 2016 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
class Translator
18
{
19
    protected $xConfig;
20
    protected $sDefaultLocale = 'en';
21
    protected $sResourceDir;
22
    // Translations
23
    protected $aTranslations;
24
25
    public function __construct($sResourceDir, $xConfig)
26
    {
27
        // Translations
28
        $this->aTranslations = array();
29
        // Set the translation resource directory
30
        $this->sResourceDir = trim($sResourceDir);
31
        // Set the config manager
32
        $this->xConfig = $xConfig;
33
        // Load the Jaxon package translations
34
        $this->loadTranslations($this->sResourceDir . '/en/errors.php', 'en');
35
        $this->loadTranslations($this->sResourceDir . '/fr/errors.php', 'fr');
36
        $this->loadTranslations($this->sResourceDir . '/es/errors.php', 'es');
37
        // Load the config translations
38
        $this->loadTranslations($this->sResourceDir . '/en/config.php', 'en');
39
        $this->loadTranslations($this->sResourceDir . '/fr/config.php', 'fr');
40
        $this->loadTranslations($this->sResourceDir . '/es/config.php', 'es');
41
        // Load the upload translations
42
        $this->loadTranslations($this->sResourceDir . '/en/upload.php', 'en');
43
        $this->loadTranslations($this->sResourceDir . '/fr/upload.php', 'fr');
44
        $this->loadTranslations($this->sResourceDir . '/es/upload.php', 'es');
45
    }
46
47
    /**
48
     * Recursively load translated strings from a array
49
     *
50
     * @param string            $sLanguage            The language of the translations
51
     * @param string            $sPrefix              The prefix for names
52
     * @param array             $aTranslations        The translated strings
53
     *
54
     * @return void
55
     */
56
    private function _loadTranslations($sLanguage, $sPrefix, array $aTranslations)
57
    {
58
        foreach($aTranslations as $sName => $xTranslation)
59
        {
60
            $sName = trim($sName);
61
            $sName = ($sPrefix) ? $sPrefix . '.' . $sName : $sName;
62
            if(!is_array($xTranslation))
63
            {
64
                // Save this translation
65
                $this->aTranslations[$sLanguage][$sName] = $xTranslation;
66
            }
67
            else
68
            {
69
                // Recursively read the translations in the array
70
                $this->_loadTranslations($sLanguage, $sName, $xTranslation);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Load translated strings from a file
77
     *
78
     * @param string        $sFilePath            The file full path
79
     * @param string        $sLanguage            The language of the strings in this file
80
     *
81
     * @return void
82
     */
83
    public function loadTranslations($sFilePath, $sLanguage)
84
    {
85
        if(!file_exists($sFilePath))
86
        {
87
            return;
88
        }
89
        $aTranslations = require($sFilePath);
90
        if(!is_array($aTranslations))
91
        {
92
            return;
93
        }
94
        // Load the translations
95
        if(!array_key_exists($sLanguage, $this->aTranslations))
96
        {
97
            $this->aTranslations[$sLanguage] = [];
98
        }
99
        $this->_loadTranslations($sLanguage, '', $aTranslations);
100
    }
101
102
    /**
103
     * Get a translated string
104
     *
105
     * @param string        $sText                The key of the translated string
106
     * @param string        $aPlaceHolders        The placeholders of the translated string
107
     * @param string        $sLanguage            The language of the translated string
108
     *
109
     * @return string        The translated string
110
     */
111
    public function trans($sText, array $aPlaceHolders = array(), $sLanguage = null)
112
    {
113
        $sText = trim((string)$sText);
114
        if(!$sLanguage)
115
        {
116
            $sLanguage = $this->xConfig->getOption('language');
117
        }
118
        if(!$sLanguage)
119
        {
120
            $sLanguage = $this->sDefaultLocale;
121
        }
122
        if(!array_key_exists($sLanguage, $this->aTranslations) || !array_key_exists($sText, $this->aTranslations[$sLanguage]))
123
        {
124
           return $sText;
125
        }
126
        $message = $this->aTranslations[$sLanguage][$sText];
127
        foreach($aPlaceHolders as $name => $value)
128
        {
129
            $message = str_replace(':' . $name, $value, $message);
130
        }
131
        return $message;
132
    }
133
}
134