aboutsummaryrefslogtreecommitdiffstats
path: root/dist-tools/git-commit-po-updates.sh
blob: 8f633d88413221b02fe6b44e763467933dafc64b (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
#!/bin/sh
# dist-tools/git-commit-po-updates.sh
# A helper script to commit translation updates into Git.
# Assumes translation updates are visible to git,
# and one directory above.
#
# © Copyright 2019 by Matthias Andree.
# Licensed under the GNU General Public License V2 or,
# at your choice, any later version.
#
# Supported options:
# -n:   dry-run, only print commands, but do not run them.
set -eu


cd "$(realpath $(dirname $0))/.."

# see if Perl has Carp::Always available,
# and implicitly fail (set -e) if it hasn't.
perl -MCarp::Always -e ''

dryrun_pfx=
while getopts 'n' opt ; do
  case $opt in
  n) dryrun_pfx=: ;;
  ?) printf 'Usage: %s [-n]\n' "$0" ;
     exit 2 ;
  esac
done

git diff -G ^.Project-Id-Version --name-only po/*.po \
| while read pofile ; do 
	if ! cmd="$(perl -WT - "$pofile" <<'_EOF'
use Encode::Locale;
use Encode;
use strict;
use Carp::Always ();
use warnings FATAL => 'uninitialized';
my ($ver, $dat, $trl, $lang, $lcod, $cset, $found);

while(<>)
{
	if (/^"Project-Id-Version: (.+)\\n"/)	{ $ver=$1; };
	if (/^"PO-Revision-Date: (.+)\\n"/)	{ $dat=$1; };
	if (/^"Last-Translator: (.+)\\n"/)	{ $trl=$1; };
	if (/^"Language-Team: ([^<]+?)\s+<.*>\\n"/)
						{ $lang=$1; };
	if (/^"Language: (.+)\\n"/)		{ $lcod=$1; };
	if (/^"Content-Type: text\/plain; charset=(.+)\\n"/)
						{ $cset = $1; };
	if ($ver and $dat and $trl and $lang and $lcod and $cset) {
		$found = 1;
		last; 
	}
}

$trl = Encode::decode($cset, $trl);

if ($found) {
	print Encode::encode(locale => "git commit --author '$trl' --date '$dat' -m 'Update <$lcod> $lang translation to $ver'", Encode::FB_CROAK);
} else {
	exit(1);
}
_EOF
)"
	then
		echo >&2 "Parsing $pofile failed, skipping."
		continue
	fi
	cmd="$cmd '$pofile'"
	printf '+ %s\n' "$cmd"
	$dryrun_pfx eval "$cmd"
done