.renest_json.pl 815 Bytes
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/perl
use strict;
use warnings;

use JSON;

my $json = JSON->new->utf8->space_before(0)->space_after(1)->indent(4)->canonical(1);

9 10 11 12 13 14 15 16 17 18 19
my $en_file = 'po/default.json';
my $en;
{
    open my $fh, '<', $en_file or die;
    local $/ = undef;
    $en = <$fh>;
    close $fh;
}

$en = $json->decode($en);

20 21 22 23 24 25 26
my $new_json = {};
my $old_json = '';

while (defined(my $line = <STDIN>)) {
    $old_json .= $line;
}

27
$old_json = $json->decode($old_json);
28
for my $key (keys %{$old_json}) {
29 30 31
    my $index    = index($key, '.');
    my $real_key = substr($key, 0, $index++);
    my $trad_key = substr($key, $index);
32

33 34 35 36 37
    if ($old_json->{$key}) {
        $new_json->{$real_key}->{$trad_key} = $old_json->{$key};
    } else {
        $new_json->{$real_key}->{$trad_key} = $en->{$key};
    }
38 39 40
}

print $json->encode($new_json);