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

TranslationWriter.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a103799d33b089496754d5eea9855e0c83280306 (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
<?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
 *
 */
use Piwik\Common;

/**
 * Write translations to file
 *
 * @package Piwik
 */
class Piwik_TranslationWriter
{
    static private $baseTranslation = null;

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

    /**
     * Quote a "C" string
     *
     * @param string $s
     * @return string
     */
    static public function quote($s)
    {
        return "'" . addcslashes($s, "'") . "'";
    }

    /**
     * Get translation file path
     *
     * @param string $lang ISO 639-1 alpha-2 language code
     * @param string $base Optional base directory (either 'lang' or 'tmp')
     * @throws Exception
     * @return string path
     */
    static public function getTranslationPath($lang, $base = 'lang')
    {
        if (!Common::isValidFilename($lang) ||
            ($base !== 'lang' && $base !== 'tmp')
        ) {
            throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($lang)));
        }

        return PIWIK_INCLUDE_PATH . '/' . $base . '/' . $lang . '.php';
    }

    /**
     * Load translations from file
     *
     * @param string $lang ISO 639-1 alpha-2 language code
     * @throws Exception
     * @return array $translations Array of translations ( key => translated string )
     */
    static public function loadTranslation($lang)
    {
        $translations = array();
        $path = self::getTranslationPath($lang);
        if (!is_readable($path)) {
            throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($lang)));
        }

        require $path;
        return $translations;
    }

    /**
     * Output translations to string
     *
     * @param array $translations Array of translations ( key => translated string )
     * @return string
     */
    static public function outputTranslation($translations)
    {
        if (!self::$baseTranslation) {
            self::$baseTranslation = self::loadTranslation('en');
        }
        $en = self::$baseTranslation;

        $output = array('<?php', '$translations = array(');
        $deferoutput = array();

        /* write all the translations that exist in en.php */
        foreach ($en as $key => $en_translation) {
            if (isset($translations[$key]) && !empty($translations[$key])) {
                $tmp = self::quote($translations[$key]);
                $output[] = "\t'$key' => $tmp,";
            }
        }

        /* write the remaining translations (that don't exist in en.php) */
        foreach ($translations as $key => $translation) {
            if (!isset($en[$key]) && !empty($translation)) {
                $tmp = self::quote($translation);
                $deferoutput[] = "\t'$key' => $tmp,";
            }
        }

        if (count($deferoutput) > 0) {
            $output[] = "\n\t// FOR REVIEW";
            $output = array_merge($output, $deferoutput);
        }

        $output[] = ');';

        return implode($output, "\n") . "\n";
    }

    /**
     * Save translations to file; translations should already be cleansed.
     *
     * @param array $translations Array of translations ( key => translated string )
     * @param string $destinationPath Path of file to save translations to
     * @return bool|int False if failure, or number of bytes written
     */
    static public function saveTranslation($translations, $destinationPath)
    {
        return file_put_contents($destinationPath, self::outputTranslation($translations));
    }
}