langtool.pl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #!/usr/bin/perl
  2. # Processes language_xx.h files into language.cpp and language.h
  3. use strict;
  4. use warnings;
  5. my @langs = ("en","cz","it","es","pl","de");
  6. sub parselang
  7. {
  8. my ($filename) = @_;
  9. open(my $fh, '<:encoding(UTF-8)', $filename)
  10. # open(my $fh, '<', $filename)
  11. or die "Could not open file '$filename' $!";
  12. # Create a new hash reference.
  13. my $out = {};
  14. while (my $line = <$fh>) {
  15. chomp $line;
  16. next if (index($line, 'define') == -1 || index($line, 'MSG') == -1);
  17. my $modifiers = {};
  18. my $symbol = '';
  19. my $value = '';
  20. if (index($line, 'define(') == -1) {
  21. # Extended definition, which specifies the string formatting.
  22. $line =~ /(?is)define\s*(\S*)\s*(.*)/;
  23. $symbol = "$1";
  24. $value = $2;
  25. } else {
  26. $line =~ /(?is)define\((.*)\)\s*(\S*)\s*(.*)/;
  27. my $options = $1;
  28. foreach my $key_value (split /,/, $options) {
  29. if ($key_value =~ /\s*(\S*)\s*=\s*(\S*)\s*/) {
  30. ${$modifiers}{$1} = $2;
  31. }
  32. }
  33. $symbol = "$2";
  34. $value = $3;
  35. }
  36. next if (! defined $symbol or length($symbol) == 0);
  37. # Trim whitespaces from both sides
  38. $value =~ s/^\s+|\s+$//g;
  39. #$string =~ s/" MACHINE_NAME "/Prusa i3/;
  40. $value =~ s/" FIRMWARE_URL "/https:\/\/github.com\/prusa3d\/Prusa-i3-Plus\//;
  41. $value =~ s/" PROTOCOL_VERSION "/1.0/;
  42. $value =~ s/" STRINGIFY\(EXTRUDERS\) "/1/;
  43. $value =~ s/" MACHINE_UUID "/00000000-0000-0000-0000-000000000000/;
  44. ${$out}{$symbol} = { value=>$value, %$modifiers };
  45. }
  46. return $out;
  47. }
  48. sub pgm_is_whitespace
  49. {
  50. my ($c) = @_;
  51. if (! defined($c)) {
  52. print "pgm_is_whitespace: undefined\n";
  53. exit(1);
  54. }
  55. return $c == ord(' ') || $c == ord('\t') || $c == ord('\r') || $c == ord('\n');
  56. }
  57. sub pgm_is_interpunction
  58. {
  59. my ($c) = @_;
  60. return $c == ord('.') || $c == ord(',') || $c == ord(':') || $c == ord(';') || $c == ord('?') || $c == ord('!') || $c == ord('/');
  61. }
  62. sub break_text_fullscreen
  63. {
  64. my $lines = [];
  65. my ($text_str, $max_linelen) = @_;
  66. if (! defined($text_str) || length($text_str) < 2) {
  67. return $lines;
  68. }
  69. $text_str =~ s/^"//;
  70. $text_str =~ s/([^\\])"/$1/;
  71. $text_str =~ s/\\"/"/;
  72. my @msg = unpack("W*", $text_str);
  73. #my @msg = split("", $text_str);
  74. my $len = $#msg + 1;
  75. my $i = 0;
  76. LINE:
  77. while ($i < $len) {
  78. while ($i < $len && pgm_is_whitespace($msg[$i])) {
  79. $i += 1;
  80. }
  81. if ($i == $len) {
  82. # End of the message.
  83. last LINE;
  84. }
  85. my $msgend2 = $i + (($max_linelen > $len) ? $len : $max_linelen);
  86. my $msgend = $msgend2;
  87. if ($msgend < $len && ! pgm_is_whitespace($msg[$msgend]) && ! pgm_is_interpunction($msg[$msgend])) {
  88. # Splitting a word. Find the start of the current word.
  89. while ($msgend > $i && ! pgm_is_whitespace($msg[$msgend - 1])) {
  90. $msgend -= 1;
  91. }
  92. if ($msgend == $i) {
  93. # Found a single long word, which cannot be split. Just cut it.
  94. $msgend = $msgend2;
  95. }
  96. }
  97. my $outstr = substr($text_str, $i, $msgend - $i);
  98. $i = $msgend;
  99. $outstr =~ s/~/ /g;
  100. #print "Output string: $outstr \n";
  101. push @$lines, $outstr;
  102. }
  103. return $lines;
  104. }
  105. my %texts;
  106. my %attributes;
  107. my $num_languages = 0;
  108. if (1)
  109. {
  110. # First load the common strings.
  111. my $symbols = parselang("language_common.h");
  112. foreach my $key (keys %{$symbols}) {
  113. if (! (exists $texts{$key})) {
  114. my $symbol_value = ${$symbols}{$key};
  115. # Store the symbol value for each language.
  116. $texts{$key} = [ (${$symbol_value}{value}) x ($#langs+1) ];
  117. # Store the possible attributes.
  118. delete ${$symbol_value}{value};
  119. # Store an "is common" attribute.
  120. ${$symbol_value}{common} = 1;
  121. # 4x 80 characters, 4 lines sent over serial line.
  122. ${$symbol_value}{length} = 320;
  123. ${$symbol_value}{lines} = 1;
  124. $attributes{$key} = $symbol_value;
  125. } else {
  126. print "Duplicate key in language_common.h: $key\n";
  127. }
  128. }
  129. }
  130. foreach my $lang (@langs) {
  131. my $symbols = parselang("language_$lang.h");
  132. foreach my $key (keys %{$symbols}) {
  133. if (! (exists $texts{$key})) {
  134. $texts{$key} = [];
  135. }
  136. my $symbol_value = ${$symbols}{$key};
  137. my $strings = $texts{$key};
  138. if (defined $attributes{$key} && defined ${$attributes{$key}}{common} && ${$attributes{$key}}{common} == 1) {
  139. # Common overrides the possible definintions in the language specific files.
  140. } else {
  141. die "Symbol $key defined first in $lang, undefined in the preceding language files."
  142. if (scalar(@$strings) != $num_languages);
  143. push @$strings, ${$symbol_value}{value};
  144. if ($lang eq 'en') {
  145. # The english texts may contain attributes. Store them into %attributes.
  146. delete ${$symbol_value}{value};
  147. $attributes{$key} = $symbol_value;
  148. }
  149. }
  150. }
  151. $num_languages += 1;
  152. foreach my $key (keys %texts) {
  153. my $strings = $texts{$key};
  154. if (scalar(@$strings) < $num_languages) {
  155. # die "Symbol $key undefined in $lang."
  156. print "Symbol $key undefined in language \"$lang\". Using the english variant:\n";
  157. print "\t", ${$strings}[0], "\n";
  158. push @$strings, ${$strings}[0];
  159. }
  160. }
  161. }
  162. my $filename = 'language_all.h';
  163. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  164. # For the programmatic access to the program memory, read
  165. # http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
  166. print $fh <<END
  167. #ifndef LANGUAGE_ALL_H
  168. #define LANGUAGE_ALL_H
  169. // Language indices into their particular symbol tables.
  170. END
  171. ;
  172. # Export symbolic IDs of languages.
  173. for my $i (0 .. $#langs) {
  174. my $lang = uc $langs[$i];
  175. print $fh "#define LANG_ID_$lang $i\n";
  176. }
  177. print $fh <<END
  178. // Language is not defined and it shall be selected from the menu.
  179. #define LANG_ID_FORCE_SELECTION 254
  180. // Language is not defined on a virgin RAMBo board.
  181. #define LANG_ID_UNDEFINED 255
  182. // Default language ID, if no language is selected.
  183. #define LANG_ID_DEFAULT LANG_ID_CZ
  184. // Number of languages available in the language table.
  185. #define LANG_NUM ${num_languages}
  186. // Currectly active language selection.
  187. extern unsigned char lang_selected;
  188. #define LANG_TABLE_SELECT_EXPLICIT(TABLE, LANG) ((const char*)(pgm_read_ptr(TABLE + (LANG))))
  189. #define LANG_TABLE_SELECT(TABLE) LANG_TABLE_SELECT_EXPLICIT(TABLE, lang_selected)
  190. END
  191. ;
  192. foreach my $key (sort(keys %texts)) {
  193. my $strings = $texts{$key};
  194. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  195. # All strings are English.
  196. print $fh "extern const char* const ${key}_LANG_TABLE[1];\n";
  197. print $fh "#define $key LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, 0)\n";
  198. } else {
  199. print $fh "extern const char* const ${key}_LANG_TABLE[LANG_NUM];\n";
  200. print $fh "#define $key LANG_TABLE_SELECT(${key}_LANG_TABLE)\n";
  201. print $fh "#define ${key}_EXPLICIT(LANG) LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, LANG)\n"
  202. if ($key eq "MSG_LANGUAGE_NAME" || $key eq "MSG_LANGUAGE_SELECT");
  203. }
  204. }
  205. print $fh <<END
  206. extern char* CAT2(const char *s1,const char *s2);
  207. extern char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4);
  208. #endif //LANGUAGE_ALL.H
  209. END
  210. ;
  211. close $fh;
  212. print ".h created\n";
  213. $filename = 'language_all.cpp';
  214. open($fh, '>', $filename) or die "Could not open file '$filename' $!";
  215. print $fh <<'END'
  216. #include <avr/pgmspace.h>
  217. #include "Configuration_prusa.h"
  218. #include "language_all.h"
  219. #define LCD_WIDTH 20
  220. extern unsigned char lang_selected;
  221. END
  222. ;
  223. my @keys = sort(keys %texts);
  224. foreach my $key (@keys) {
  225. my $strings = $texts{$key};
  226. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  227. # Shrink the array to a single value.
  228. $strings = [${$strings}[0]];
  229. }
  230. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  231. my $suffix = uc($langs[$i]);
  232. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  233. print $fh "const char ${key}_${suffix}[] PROGMEM = ${$strings}[$i];\n";
  234. }
  235. }
  236. my $langnum = $#{$strings}+1;
  237. if ($langnum == $#langs+1) {
  238. $langnum = "LANG_NUM";
  239. }
  240. print $fh "const char * const ${key}_LANG_TABLE[$langnum] PROGMEM = {\n";
  241. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  242. my $suffix = uc($langs[$i]);
  243. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  244. print $fh "\t${key}_${suffix}";
  245. } else {
  246. print $fh "\t${key}_EN";
  247. }
  248. print $fh ',' if $i < $#{$strings};
  249. print $fh "\n";
  250. }
  251. print $fh "};\n\n";
  252. }
  253. print $fh <<'END'
  254. char langbuffer[LCD_WIDTH+1];
  255. char* CAT2(const char *s1,const char *s2) {
  256. unsigned char len=0;
  257. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  258. len+=strlen_P(s1);
  259. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  260. return langbuffer;
  261. }
  262. char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4) {
  263. unsigned char len=0;
  264. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  265. len+=strlen_P(s1);
  266. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  267. len+=strlen_P(s2);
  268. strncpy_P(langbuffer+len,s3,LCD_WIDTH-len);
  269. len+=strlen_P(s3);
  270. strncpy_P(langbuffer+len,s4,LCD_WIDTH-len);
  271. return langbuffer;
  272. }
  273. END
  274. ;
  275. print ".cpp created.\nDone!\n";
  276. my $verify_only = 1;
  277. for my $lang (0 .. $#langs) {
  278. print "Language: $langs[$lang]\n";
  279. foreach my $key (@keys) {
  280. my $strings = $texts{$key};
  281. my %attrib = %{$attributes{$key}};
  282. my $message = ${$strings}[$lang];
  283. $message =~ /\S*"(.*)"\S*/;
  284. $message = $1;
  285. if ($lang == 0 || ${$strings}[0] ne $message) {
  286. # If the language is not English, don't show the non-translated message.
  287. my $max_nlines = $attrib{lines} // 1;
  288. my $max_linelen = $attrib{length} // (($max_nlines > 1) ? 20 : 17);
  289. # if (! $verify_only) {
  290. # if ($nlines > 1) {
  291. # print "Multi-line message: $message. Breaking to $nlines lines:\n";
  292. # print "\t$_\n" foreach (@{$lines});
  293. # }
  294. # }
  295. if ($max_nlines <= 1) {
  296. my $linelen = length($message);
  297. if ($linelen > $max_linelen) {
  298. print "Key $key, language $langs[$lang], line length: $linelen, max length: $max_linelen\n";
  299. print "\t$message\n";
  300. }
  301. } else {
  302. my $lines = break_text_fullscreen($message, $max_linelen);
  303. my $nlines = @{$lines};
  304. if ($nlines > $max_nlines) {
  305. print "Key $key, language $langs[$lang], lines: $nlines, max lines: $max_nlines\n";
  306. print "\t$_\n" foreach (@{$lines});
  307. }
  308. }
  309. }
  310. }
  311. }