Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Translate.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23b1e2637155c7eadb571b4cfa47e9c7d82b8990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik;
use Exception;
use Piwik\Config;
use Piwik\Common;

/**
 * @package Piwik
 */
class Translate
{
    static private $instance = null;
    static private $languageToLoad = null;
    private $loadedLanguage = false;

    /**
     * @return \Piwik\Translate
     */
    static public function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function loadEnglishTranslation()
    {
        $this->loadCoreTranslationFile('en');
    }

    public function unloadEnglishTranslation()
    {
        $GLOBALS['Piwik_translations'] = array();
    }

    public function reloadLanguage($language = false)
    {
        if (empty($language)) {
            $language = $this->getLanguageToLoad();
        }
        $this->unloadEnglishTranslation();
        $this->loadEnglishTranslation();
        $this->loadCoreTranslation($language);
        \Piwik\PluginsManager::getInstance()->loadPluginTranslations($language);
    }

    /**
     * Reads the specified code translation file in memory.
     *
     * @param bool|string $language 2 letter language code. If not specified, will detect current user translation, or load default translation.
     * @return void
     */
    public function loadCoreTranslation($language = false)
    {
        if (empty($language)) {
            $language = $this->getLanguageToLoad();
        }
        if ($this->loadedLanguage == $language) {
            return;
        }
        $this->loadCoreTranslationFile($language);
    }

    private function loadCoreTranslationFile($language)
    {
        $translations = array();
        $path = PIWIK_INCLUDE_PATH . '/lang/' . $language . '.php';
        if (!Common::isValidFilename($language) || !is_readable($path)) {
            throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($language)));
        }
        require $path;
        $this->mergeTranslationArray($translations);
        $this->setLocale();
        $this->loadedLanguage = $language;
    }

    public function mergeTranslationArray($translation)
    {
        if (!isset($GLOBALS['Piwik_translations'])) {
            $GLOBALS['Piwik_translations'] = array();
        }
        // we could check that no string overlap here
        $GLOBALS['Piwik_translations'] = array_merge($GLOBALS['Piwik_translations'], array_filter($translation, 'strlen'));
    }

    /**
     * @return string the language filename prefix, eg 'en' for english
     * @throws exception if the language set is not a valid filename
     */
    public function getLanguageToLoad()
    {
        if (is_null(self::$languageToLoad)) {
            $lang = Common::getRequestVar('language', '', 'string');

            Piwik_PostEvent('Translate.getLanguageToLoad', array(&$lang));

            self::$languageToLoad = $lang;
        }

        return self::$languageToLoad;
    }

    /** Reset the cached language to load. Used in tests. */
    static public function reset()
    {
        self::$languageToLoad = null;
    }

    public function getLanguageLoaded()
    {
        return $this->loadedLanguage;
    }

    public function getLanguageDefault()
    {
        return Config::getInstance()->General['default_language'];
    }

    /**
     * Generate javascript translations array
     *
     * @param array $moduleList
     * @return string containing javascript code with translations array (including <script> tag)
     */
    public function getJavascriptTranslations(array $moduleList)
    {
        if (!in_array('General', $moduleList)) {
            $moduleList[] = 'General';
        }

        $js = 'var translations = {';

        $moduleRegex = '#^(';
        foreach ($moduleList as $module) {
            $moduleRegex .= $module . '|';
        }
        $moduleRegex = substr($moduleRegex, 0, -1);
        $moduleRegex .= ')_.*_js$#i';

        // Hack: common translations used in JS but not only, force as them to be defined in JS
        $translations = $GLOBALS['Piwik_translations'];
        $toSetInJs = array('General_Save', 'General_OrCancel');
        foreach ($toSetInJs as $toSetId) {
            $translations[$toSetId . '_js'] = $translations[$toSetId];
        }
        foreach ($translations as $key => $value) {
            if (preg_match($moduleRegex, $key)) {
                $js .= '"' . $key . '": "' . str_replace('"', '\"', $value) . '",';
            }
        }
        $js = substr($js, 0, -1);
        $js .= '};';
        $js .= "\n" . 'if(typeof(piwik_translations) == \'undefined\') { var piwik_translations = new Object; }' .
            'for(var i in translations) { piwik_translations[i] = translations[i];} ';
        $js .= 'function _pk_translate(translationStringId) { ' .
            'if( typeof(piwik_translations[translationStringId]) != \'undefined\' ){  return piwik_translations[translationStringId]; }' .
            'return "The string "+translationStringId+" was not loaded in javascript. Make sure it is suffixed with _js and that you called  %7BloadJavascriptTranslations plugins=\'\$YOUR_PLUGIN_NAME\'%7D before your javascript code.";}';
        return $js;
    }

    /**
     * Set locale
     *
     * @see http://php.net/setlocale
     */
    private function setLocale()
    {
        $locale = $GLOBALS['Piwik_translations']['General_Locale'];
        $locale_variant = str_replace('UTF-8', 'UTF8', $locale);
        setlocale(LC_ALL, $locale, $locale_variant);
        setlocale(LC_CTYPE, '');
    }
}