summaryrefslogtreecommitdiffstats
path: root/modules/translation/application/clicommands/TestCommand.php
blob: 347c2c9f808631570bc1473db0bd67205a0a9363 (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
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Translation\Clicommands;

use Icinga\Date\DateFormatter;
use Icinga\Module\Translation\Cli\ArrayToTextTableHelper;
use Icinga\Module\Translation\Cli\TranslationCommand;
use ipl\I18n\GettextTranslator;
use ipl\I18n\StaticTranslator;

/**
 * Timestamp test helper
 *
 *
 */
class TestCommand extends TranslationCommand
{
    protected $locales = array();

    /**
     * Get translation examples for DateFormatter
     *
     * To help you check if the values got translated correctly
     *
     * USAGE:
     *
     *   icingacli translation test dateformatter <locale>
     *
     * EXAMPLES:
     *
     *   icingacli translation test dateformatter de_DE
     *   icingacli translation test dateformatter fr_FR
     */
    public function dateformatterAction()
    {
        $time = time();

        /** @uses DateFormatter::timeAgo */
        $this->printTable($this->getMultiTranslated(
            'Time Ago',
            array('Icinga\Date\DateFormatter', 'timeAgo'),
            array(
                "15 sec" => $time - 15,
                "62 sec" => $time - 62,
                "10 min" => $time - 600,
                "1h"     => $time - 1 * 3600,
                "3h"     => $time - 3 * 3600,
                "25h"    => $time - 25 * 3600,
                "31d"    => $time - 31 * 24 * 3600,
            )
        ));

        $this->printTable($this->getMultiTranslated(
            'Time Since',
            array('Icinga\Date\DateFormatter', 'timeSince'),
            array(
                "15 sec" => $time - 15,
                "62 sec" => $time - 62,
                "10 min" => $time - 600,
                "1h"     => $time - 1 * 3600,
                "3h"     => $time - 3 * 3600,
                "25h"    => $time - 25 * 3600,
                "31d"    => $time - 31 * 24 * 3600,
            )
        ));

        $this->printTable($this->getMultiTranslated(
            'Time Until',
            array('Icinga\Date\DateFormatter', 'timeUntil'),
            array(
                "15 sec" => $time + 15,
                "62 sec" => $time + 62,
                "10 min" => $time + 600,
                "1h"     => $time + 1 * 3600,
                "3h"     => $time + 3 * 3600,
                "25h"    => $time + 25 * 3600,
                "31d"    => $time + 31 * 24 * 3600,
            )
        ));
    }

    public function defaultAction()
    {
        $this->dateformatterAction();
    }

    public function init()
    {
        foreach ($this->params->getAllStandalone() as $l) {
            $this->locales[] = $l;
        }

        if (empty($this->locales)) {
            /** @var GettextTranslator $translator */
            $translator = StaticTranslator::$instance;
            $this->locales = $translator->listLocales();
        }
    }

    protected function callTranslated($callback, $arguments, $locale = 'en_US')
    {
        /** @var GettextTranslator $translator */
        $translator = StaticTranslator::$instance;
        $translator->setLocale($locale);
        return call_user_func_array($callback, $arguments);
    }

    protected function getMultiTranslated($name, $callback, $arguments, $locales = null)
    {
        if ($locales === null) {
            $locales = $this->locales;
        }
        array_unshift($locales, 'C');

        $rows = array();

        foreach ($arguments as $k => $args) {
            $row = array($name => $k);

            if (! is_array($args)) {
                $args = array($args);
            }
            foreach ($locales as $locale) {
                $row[$locale] = $this->callTranslated($callback, $args, $locale);
            }
            $rows[] = $row;
        }

        return $rows;
    }

    protected function printTable($rows)
    {
        $tt = new ArrayToTextTableHelper($rows);
        $tt->showHeaders(true);
        $tt->render();
        echo "\n\n";
    }
}