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: 65381c0a2da811ec4d1144c1c275dfd3484b5a36 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik;

use Exception;

/**
 */
class Translate
{
    private static $languageToLoad = null;
    private static $loadedLanguage = false;

    /**
     * Clean a string that may contain HTML special chars, single/double quotes, HTML entities, leading/trailing whitespace
     *
     * @param string $s
     * @return string
     */
    public static function clean($s)
    {
        return html_entity_decode(trim($s), ENT_QUOTES, 'UTF-8');
    }

    public static function loadEnglishTranslation()
    {
        self::loadCoreTranslationFile('en');
    }

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

    public static function reloadLanguage($language = false)
    {
        if (empty($language)) {
            $language = self::getLanguageToLoad();
        }
        self::unloadEnglishTranslation();
        self::loadEnglishTranslation();
        self::loadCoreTranslation($language);
        \Piwik\Plugin\Manager::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 static function loadCoreTranslation($language = false)
    {
        if (empty($language)) {
            $language = self::getLanguageToLoad();
        }
        if (self::$loadedLanguage == $language) {
            return;
        }
        self::loadCoreTranslationFile($language);
    }

    private static function loadCoreTranslationFile($language)
    {
        if (empty($language)) {
            return;
        }
        $path = PIWIK_INCLUDE_PATH . '/lang/' . $language . '.json';
        if (!Filesystem::isValidFilename($language) || !is_readable($path)) {
            throw new Exception(Piwik::translate('General_ExceptionLanguageFileNotFound', array($language)));
        }
        $data = file_get_contents($path);
        $translations = json_decode($data, true);
        self::mergeTranslationArray($translations);
        self::setLocale();
        self::$loadedLanguage = $language;
    }

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

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

            /**
             * Triggered when the current user's language is requested.
             *
             * By default the current language is determined by the **language** query
             * parameter. Plugins can override this logic by subscribing to this event.
             *
             * **Example**
             *
             *     public function getLanguage(&$lang)
             *     {
             *         $client = new My3rdPartyAPIClient();
             *         $thirdPartyLang = $client->getLanguageForUser(Piwik::getCurrentUserLogin());
             *
             *         if (!empty($thirdPartyLang)) {
             *             $lang = $thirdPartyLang;
             *         }
             *     }
             *
             * @param string &$lang The language that should be used for the current user. Will be
             *                      initialized to the value of the **language** query parameter.
             */
            Piwik::postEvent('User.getLanguage', array(&$lang));

            self::$languageToLoad = $lang;
        }

        return self::$languageToLoad;
    }

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

    private static function isALanguageLoaded() {
        return !empty($GLOBALS['Piwik_translations']);
    }

    /**
     * Either the name of the currently loaded language such as 'en' or 'de' or null if no language is loaded at all.
     * @return bool|string
     */
    public static function getLanguageLoaded()
    {
        if (!self::isALanguageLoaded()) {
            return null;
        }

        return self::$loadedLanguage;
    }

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

    /**
     * Generate javascript translations array
     */
    public static function getJavascriptTranslations()
    {
        $translations = & $GLOBALS['Piwik_translations'];

        $clientSideTranslations = array();
        foreach (self::getClientSideTranslationKeys() as $key) {
            list($plugin, $stringName) = explode("_", $key, 2);
            $clientSideTranslations[$key] = $translations[$plugin][$stringName];
        }

        $js = 'var translations = ' . Common::json_encode($clientSideTranslations) . ';';
        $js .= "\n" . 'if (typeof(piwik_translations) == \'undefined\') { var piwik_translations = new Object; }' .
            'for(var i in translations) { piwik_translations[i] = translations[i];} ';
        return $js;
    }

    /**
     * Returns the list of client side translations by key. These translations will be outputted
     * to the translation JavaScript.
     */
    private static function getClientSideTranslationKeys()
    {
        $result = array();

        /**
         * Triggered before generating the JavaScript code that allows i18n strings to be used
         * in the browser.
         *
         * Plugins should subscribe to this event to specify which translations
         * should be available to JavaScript.
         *
         * Event handlers should add whole translation keys, ie, keys that include the plugin name.
         *
         * **Example**
         *
         *     public function getClientSideTranslationKeys(&$result)
         *     {
         *         $result[] = "MyPlugin_MyTranslation";
         *     }
         *
         * @param array &$result The whole list of client side translation keys.
         */
        Piwik::postEvent('Translate.getClientSideTranslationKeys', array(&$result));

        $result = array_unique($result);

        return $result;
    }

    /**
     * Set locale
     *
     * @see http://php.net/setlocale
     */
    private static 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, '');
    }

    public static function findTranslationKeyForTranslation($translation)
    {
        if (empty($GLOBALS['Piwik_translations'])) {
            return;
        }

        foreach ($GLOBALS['Piwik_translations'] as $key => $translations) {
            $possibleKey = array_search($translation, $translations);
            if (!empty($possibleKey)) {
                return $key . '_' . $possibleKey;
            }
        }
    }
}