source: git/src/gettexttomsg.pl @ e073702

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warning
Last change on this file since e073702 was a580dc1, checked in by Olly Betts <olly@…>, 23 years ago

Extract messages from cmdline.c and getopt.c for translation.

git-svn-id: file:///home/survex-svn/survex/trunk@958 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100755
File size: 2.9 KB
Line 
1#!/usr/bin/perl -w
2require 5.004;
3use strict;
4
5use integer;
6
7# Magic identifier (12 bytes)
8my $magic = "Svx\nMsg\r\n\xfe\xff\0";
9# Designed to be corrupted by ASCII ftp, top bit stripping (or
10# being used for parity).  Contains a zero byte so more likely
11# to be flagged as data (e.g. by perl's "-B" test).
12
13my $major = 0;
14my $minor = 8;
15
16# File format (multi-byte numbers in network order (bigendian)):
17# 12 bytes: Magic identifier
18# 1 byte:   File format major version number (0)
19# 1 byte:   File format minor version number (8)
20# 2 bytes:  Number of messages (N)
21# 4 bytes:  Offset from XXXX to end of file
22# XXXX:
23# N*:
24# <message> NUL
25
26my %ent = ();
27
28open ENT, "../lib/named-entities.txt" or die $!;
29while (<ENT>) {
30   my ($e, $v) = /^(\w+),(\d+)/;
31   $ent{$e} = $v;
32}
33close ENT;
34
35my %revmsgs = ();
36
37open MSG, "../lib/messages.txt" or die $!;
38while (<MSG>) {
39   next if /^\s*#/; # skip comments
40   
41   # en:  0 0.81 the message
42   # en-us: 0 0.81 " the message "
43   my ($langs, $msgno, $dummy, $msg) = /^([-\w,]+):\s*(\d+)\s+("?)(.*)\3/;
44
45   unless (defined $langs) {
46      chomp;
47      print STDERR "Warning: Bad line: \"$_\"\n";
48      next;
49   }
50
51   $langs =~ tr/-/_/;
52
53   if ($msg =~ /[\0-\x1f\x7f-\xff]/) {
54      print STDERR "Warning: literal character in message $msgno\n";
55   }
56
57   my $utf8 = string_to_utf8($msg);
58   for (split /,/, $langs) {
59      if (lc $_ eq 'en') {
60          $revmsgs{$utf8} = $msgno;
61      }
62   }
63}
64
65my $die = 0;
66
67while (<>) {
68    if (!/^\s*#/) {
69        while (/\\\n$/) {
70            $_ .= <>;
71        }
72        # very crude - doesn't know about comments, etc
73        s!\b_\("(.*?)"\)!"msg(/*$1*/".findmsg($1).")"!gse;
74    } elsif (/\s*#\s*define\s+_\(/) {
75        $_ = "#include \"message.h\"\n";
76    }
77    print;
78}
79
80if ($die) {
81    die "Not all messages found!\n";
82}
83
84sub findmsg {
85    my $msg = shift;
86    $msg =~ s/\\\n//g;
87    $msg =~ s/\\n/\n/g;
88    $msg =~ s/\\t/\t/g;
89    unless (exists $revmsgs{$msg}) {
90        if (!$die) {
91            print STDERR "Message(s) not found in message file:\n";
92            $die = 1;
93        }
94        print STDERR "'$msg'\n";
95        return "";
96    }
97    return $revmsgs{$msg};
98}
99
100sub string_to_utf8 {
101   my $s = shift;
102   $s =~ s/([\x80-\xff])/char_to_utf8(ord($1))/eg;
103   $s =~ s/\&(#\d+|#x[a-f0-9]+|[a-z0-9]+);?/decode_entity($1)/eig;
104   return $s;
105}
106
107sub decode_entity {
108   my $ent = shift;
109   return char_to_utf8($1) if $ent =~ /^#(\d+)$/;
110   return char_to_utf8(hex($1)) if $ent =~ /^#x([a-f0-9]+)$/;
111   return char_to_utf8($ent{$ent}) if exists $ent{$ent};
112   $ent = "\&$ent;";
113   print STDERR "Warning: entity \"$ent\" unknown\n";
114   return $ent;
115}
116
117sub char_to_utf8 {
118   my $unicode = shift;
119   # ASCII is easy, and the most common case
120   return chr($unicode) if $unicode < 0x80;
121
122   my $result = '';
123   my $n = 0x20;
124   while (1) {
125      $result = chr(0x80 | ($unicode & 0x3f)) . $result;
126      $unicode >>= 6;
127      last if $unicode < $n;
128      $n >>= 1;
129   }
130   $result = chr((0x100 - $n*2) | $unicode) . $result;
131   return $result;
132}
Note: See TracBrowser for help on using the repository browser.