summaryrefslogtreecommitdiff
path: root/app/Console/LocaleComparator.php
blob: fcc3a995f1af6679e7daf17142c31306b26a362d (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
<?php

namespace Kanboard\Console;

use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class LocaleComparator extends Base
{
    const REF_LOCALE = 'fr_FR';

    protected function configure()
    {
        $this
            ->setName('locale:compare')
            ->setDescription('Compare application translations with the '.self::REF_LOCALE.' locale');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $strings = array();
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('app'));
        $it->rewind();

        while ($it->valid()) {

            if (! $it->isDot() && substr($it->key(), -4) === '.php') {
                $strings = array_merge($strings, $this->search($it->key()));
            }

            $it->next();
        }

        $this->compare(array_unique($strings));
    }

    public function show(array $strings)
    {
        foreach ($strings as $string) {
            echo "    '".str_replace("'", "\'", $string)."' => '',".PHP_EOL;
        }
    }

    public function compare(array $strings)
    {
        $reference_file = 'app/Locale/'.self::REF_LOCALE.'/translations.php';
        $reference = include $reference_file;

        echo str_repeat('#', 70).PHP_EOL;
        echo 'MISSING STRINGS'.PHP_EOL;
        echo str_repeat('#', 70).PHP_EOL;
        $this->show(array_diff($strings, array_keys($reference)));

        echo str_repeat('#', 70).PHP_EOL;
        echo 'USELESS STRINGS'.PHP_EOL;
        echo str_repeat('#', 70).PHP_EOL;
        $this->show(array_diff(array_keys($reference), $strings));
    }

    public function search($filename)
    {
        $content = file_get_contents($filename);
        $strings = array();

        if (preg_match_all('/\b[et]\((\'\K.*?\') *[\)\,]/', $content, $matches) && isset($matches[1])) {
            $strings = $matches[1];
        }

        if (preg_match_all('/\bdt\((\'\K.*?\') *[\)\,]/', $content, $matches) && isset($matches[1])) {
            $strings = array_merge($strings, $matches[1]);
        }

        array_walk($strings, function(&$value) {
            $value = trim($value, "'");
            $value = str_replace("\'", "'", $value);
        });

        return $strings;
    }
}