File: addTrEntry

package info (click to toggle)
lemonldap-ng 2.22.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,516 kB
  • sloc: perl: 82,515; javascript: 25,474; xml: 6,473; makefile: 1,327; sh: 492; sql: 159; python: 55; php: 26
file content (101 lines) | stat: -rwxr-xr-x 2,429 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

use strict;
use JSON;
use Getopt::Long;
use Encode::Locale qw/decode_argv/;
my ( $portal, $modify, $help, $delete, $reorder );
my $json =
  JSON->new->utf8->pretty()->canonical()->space_before(0)->space_after(0);

decode_argv();
GetOptions(
    "portal|p"  => \$portal,
    "modify|m"  => \$modify,
    "delete|d"  => \$delete,
    "reorder|r" => \$reorder,
    "help|h"    => \$help,
);
usage() if $help or ( !@ARGV and !$reorder );
my $key    = shift @ARGV;
my $enText = shift @ARGV;
my $frText = shift(@ARGV) || $enText;

# Check args
usage() if $delete and $modify;
if ($key) {
    usage() if $reorder;
    if ($delete) {
        usage() if $enText;
    }
    else {
        usage() unless $enText;
    }
}
else {
    usage() unless $reorder;
}

# Main
my $wdir =
    'lemonldap-ng-'
  . ( $portal ? "portal" : "manager" )
  . '/site/htdocs/static/languages';
opendir D, $wdir or die "unable to open $wdir";
my @langs = grep { /\.json$/ } readdir D;
closedir D;
for my $lang (@langs) {
    my ( $file, $content );
    {
        local $/ = undef;
        open $file, "$wdir/$lang" or die $!;
        binmode $file;
        $content = <$file>;
        close $file;
    }
    my $jsonObj = $json->decode($content);
    if ($key) {
        if ( $jsonObj->{$key} xor( $delete or $modify ) ) {
            print STDERR ( $jsonObj->{$key}
                ? "key already exists\n"
                : "key doesn't exit\n" );
            usage();
        }
        if ($delete) {
            delete $jsonObj->{$key};
        }
        else {
            my $text = ($lang eq 'fr.json' ? $frText : $enText);
            if ($text =~ /^\$(.*)/) {
                die "\$$1 not found" unless $jsonObj->{$1};
                $jsonObj->{$key} = $jsonObj->{$1};
            } else {
                $jsonObj->{$key} = $text;
            }
        }
    }
    $content = $json->encode($jsonObj);
    $content =~ s/\n\s+/\n/sg;
    $content =~ s/\n*$//s;
    open $file, '>', "$wdir/$lang" or die $!;
    binmode $file;
    print $file $content;
    close $file;
}

## usage
sub usage {
    print STDERR <<EOT;
Usage:
$0 <option> key enText <frText>
$0 <option> key '\$otherkey'

Options:
  --portal  -p: add entry in portal translation instead of manager
  --modify  -m: modify an existing entry
  --delete  -d: delete an existing key
  --reorder -r: reorder files
  --help   -h: display this
EOT
    exit( $help ? 0 : 1 );
}