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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#!/usr/bin/perl -w
use strict;
use File::Basename;
die "usage: $0 po/*.po\n" unless @ARGV;
my @locales;
my $func_prefix = "hebcal_lookup_";
foreach my $pofile (@ARGV) {
my $locale = basename($pofile, ".po");
my $outfile = "strings_$locale.gperf";
print STDERR "$pofile => $outfile\n";
open(IN, $pofile) || die "$pofile: $!";
open(OUT, ">$outfile") || die "$outfile: $!";
binmode(IN, ":utf8");
binmode(OUT, ":utf8");
print OUT '%delimiters=|', "\n";
print OUT '%define lookup-function-name ', $func_prefix, $locale, "\n";
print OUT '%struct-type
%{
#include <string.h>
%}
struct event_title { char *name; char *dest; };
%%
';
my $msgid;
while(<IN>) {
if (/^msgid\s+"(.*)"\s*$/) {
$msgid = $1;
} elsif (/^msgstr\s+"(.+)"\s*$/) {
my $msgstr = $1;
if (length($msgid)) {
print OUT qq{$msgid|"$msgstr"\n};
}
}
}
close(IN);
close(OUT);
my $dest = "strings_$locale.c";
my $cmd = "gperf $outfile > $dest";
print STDERR "$cmd\n";
system($cmd);
push(@locales, $locale);
}
my $locale_list = join(", ", @locales);
my $locale_list_len = scalar(@locales) + 1;
my $outfile = "translations.h";
print STDERR "$outfile\n";
open(OUT, ">$outfile") || die "$outfile: $!";
print OUT <<EOF;
/*
* DO NOT EDIT THIS FILE!
* Generated by $0
*/
#ifndef __HEBCAL_TRANSLATIONS__
#define __HEBCAL_TRANSLATIONS__
#define HEBCAL_LANG_LIST "$locale_list"
extern const char *hebcal_langs[$locale_list_len];
struct event_title { char *name; char *dest; };
const char * lookup_translation(const char *s);
typedef enum hebcal_lang_e {
HEBCAL_LANG_DEFAULT,
EOF
;
foreach my $locale (@locales) {
print OUT " HEBCAL_LANG_", uc($locale), ",\n";
}
print OUT <<EOF;
HEBCAL_LANG_UNDEFINED
} hebcal_lang;
void hebcal_set_language(hebcal_lang lang);
hebcal_lang hebcal_get_language(const char *locale);
#endif /* __HEBCAL_TRANSLATIONS__ */
EOF
;
close(OUT);
$outfile = "translations.c";
print STDERR "$outfile\n";
open(OUT, ">$outfile") || die "$outfile: $!";
print OUT <<EOF;
/*
* DO NOT EDIT THIS FILE!
* Generated by $0
*/
#include "translations.h"
#include <string.h>
const char *hebcal_langs[] = {
EOF
;
foreach my $locale (@locales) {
print OUT " \"", $locale, "\",\n";
}
print OUT " NULL\n};\n\n";
foreach my $locale (@locales) {
print OUT "struct event_title * ", $func_prefix, $locale, "(const char *str, unsigned int len);\n";
}
print OUT <<EOF;
hebcal_lang g_lang = HEBCAL_LANG_DEFAULT;
void hebcal_set_language(hebcal_lang lang) {
g_lang = lang;
}
hebcal_lang hebcal_get_language(const char *locale) {
if (!locale || !locale[0]) {
return HEBCAL_LANG_DEFAULT;
EOF
;
foreach my $locale (@locales) {
print OUT " } else if (0 == strcmp(locale, \"", $locale, "\")) {\n";
print OUT " return HEBCAL_LANG_", uc($locale), ";\n";
}
print OUT <<EOF;
} else {
return HEBCAL_LANG_UNDEFINED;
}
}
const char * lookup_translation(const char *src) {
struct event_title *et;
if (!src || !src[0]) {
return src;
}
switch (g_lang) {
case HEBCAL_LANG_DEFAULT:
return src;
EOF
;
foreach my $locale (@locales) {
print OUT " case HEBCAL_LANG_", uc($locale), ":\n";
print OUT " et = ", $func_prefix, $locale, "(src, strlen(src));\n";
print OUT " break;\n";
}
print OUT <<EOF;
case HEBCAL_LANG_UNDEFINED:
default:
return src;
}
if (et && et->dest && et->dest[0]) {
return et->dest;
}
return src;
}
EOF
;
close(OUT);
exit(0);
|