langtool.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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");
  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. $symbol = "$2";
  29. $value = $3;
  30. }
  31. next if (! defined $symbol or length($symbol) == 0);
  32. # Trim whitespaces from both sides
  33. $value =~ s/^\s+|\s+$//g;
  34. #$string =~ s/" MACHINE_NAME "/Prusa i3/;
  35. $value =~ s/" FIRMWARE_URL "/https:\/\/github.com\/prusa3d\/Prusa-i3-Plus\//;
  36. $value =~ s/" PROTOCOL_VERSION "/1.0/;
  37. $value =~ s/" STRINGIFY\(EXTRUDERS\) "/1/;
  38. $value =~ s/" MACHINE_UUID "/00000000-0000-0000-0000-000000000000/;
  39. ${$out}{$symbol} = { value=>$value, %$modifiers };
  40. }
  41. return $out;
  42. }
  43. sub pgm_is_whitespace
  44. {
  45. my ($c) = @_;
  46. if (! defined($c)) {
  47. print "pgm_is_whitespace: undefined\n";
  48. exit(1);
  49. }
  50. return $c == ord(' ') || $c == ord('\t') || $c == ord('\r') || $c == ord('\n');
  51. }
  52. sub pgm_is_interpunction
  53. {
  54. my ($c) = @_;
  55. return $c == ord('.') || $c == ord(',') || $c == ord(';') || $c == ord('?') || $c == ord('!');
  56. }
  57. sub break_text_fullscreen
  58. {
  59. my $lines = [];
  60. my ($text_str) = @_;
  61. if (! defined($text_str) || length($text_str) < 2) {
  62. return $lines;
  63. }
  64. $text_str =~ s/^"//;
  65. $text_str =~ s/([^\\])"/$1/;
  66. $text_str =~ s/\\"/"/;
  67. my @msg = unpack("W*", $text_str);
  68. #my @msg = split("", $text_str);
  69. my $len = $#msg + 1;
  70. my $i = 0;
  71. LINE:
  72. while ($i < $len) {
  73. while ($i < $len && pgm_is_whitespace($msg[$i])) {
  74. $i += 1;
  75. }
  76. if ($i == $len) {
  77. # End of the message.
  78. last LINE;
  79. }
  80. my $msgend2 = $i + ((20 > $len) ? $len : 20);
  81. my $msgend = $msgend2;
  82. if ($msgend < $len && ! pgm_is_whitespace($msg[$msgend]) && ! pgm_is_interpunction($msg[$msgend])) {
  83. # Splitting a word. Find the start of the current word.
  84. while ($msgend > $i && ! pgm_is_whitespace($msg[$msgend - 1])) {
  85. $msgend -= 1;
  86. }
  87. if ($msgend == $i) {
  88. # Found a single long word, which cannot be split. Just cut it.
  89. $msgend = $msgend2;
  90. }
  91. }
  92. my $outstr = substr($text_str, $i, $msgend - $i);
  93. $i = $msgend;
  94. $outstr =~ s/~/ /g;
  95. #print "Output string: $outstr \n";
  96. push @$lines, $outstr;
  97. }
  98. return $lines;
  99. }
  100. my %texts;
  101. my %attributes;
  102. my $num_languages = 0;
  103. foreach my $lang (@langs) {
  104. my $symbols = parselang("language_$lang.h");
  105. foreach my $key (keys %{$symbols}) {
  106. if (! (exists $texts{$key})) {
  107. $texts{$key} = [];
  108. }
  109. my $symbol_value = ${$symbols}{$key};
  110. my $strings = $texts{$key};
  111. die "Symbol $key defined first in $lang, undefined in the preceding language files."
  112. if (scalar(@$strings) != $num_languages);
  113. push @$strings, ${$symbol_value}{value};
  114. }
  115. $num_languages += 1;
  116. foreach my $key (keys %texts) {
  117. my $strings = $texts{$key};
  118. if (scalar(@$strings) != $num_languages) {
  119. # die "Symbol $key undefined in $lang."
  120. print "Symbol $key undefined in language \"$lang\". Using the english variant.\n";
  121. push @$strings, ${$strings}[0];
  122. }
  123. }
  124. }
  125. my $filename = 'language_all.h';
  126. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  127. # For the programmatic access to the program memory, read
  128. # http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
  129. print $fh <<END
  130. #ifndef LANGUAGE_ALL_H
  131. #define LANGUAGE_ALL_H
  132. // Language indices into their particular symbol tables.
  133. END
  134. ;
  135. # Export symbolic IDs of languages.
  136. for my $i (0 .. $#langs) {
  137. my $lang = uc $langs[$i];
  138. print $fh "#define LANG_ID_$lang $i\n";
  139. }
  140. print $fh <<END
  141. // Language is not defined and it shall be selected from the menu.
  142. #define LANG_ID_FORCE_SELECTION 254
  143. // Language is not defined on a virgin RAMBo board.
  144. #define LANG_ID_UNDEFINED 255
  145. // Default language ID, if no language is selected.
  146. #define LANG_ID_DEFAULT LANG_ID_CZ
  147. // Number of languages available in the language table.
  148. #define LANG_NUM ${num_languages}
  149. // Currectly active language selection.
  150. extern unsigned char lang_selected;
  151. #define LANG_TABLE_SELECT_EXPLICIT(TABLE, LANG) ((const char*)(pgm_read_ptr(TABLE + (LANG))))
  152. #define LANG_TABLE_SELECT(TABLE) LANG_TABLE_SELECT_EXPLICIT(TABLE, lang_selected)
  153. END
  154. ;
  155. foreach my $key (sort(keys %texts)) {
  156. my $strings = $texts{$key};
  157. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  158. # All strings are English.
  159. print $fh "extern const char* const ${key}_LANG_TABLE[1];\n";
  160. print $fh "#define $key LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, 0)\n";
  161. } else {
  162. print $fh "extern const char* const ${key}_LANG_TABLE[LANG_NUM];\n";
  163. print $fh "#define $key LANG_TABLE_SELECT(${key}_LANG_TABLE)\n";
  164. print $fh "#define ${key}_EXPLICIT(LANG) LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, LANG)\n"
  165. if ($key eq "MSG_LANGUAGE_NAME" || $key eq "MSG_LANGUAGE_SELECT");
  166. }
  167. }
  168. print $fh <<END
  169. extern char* CAT2(const char *s1,const char *s2);
  170. extern char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4);
  171. #endif //LANGUAGE_ALL.H
  172. END
  173. ;
  174. close $fh;
  175. print ".h created\n";
  176. $filename = 'language_all.cpp';
  177. open($fh, '>', $filename) or die "Could not open file '$filename' $!";
  178. print $fh <<'END'
  179. #include <avr/pgmspace.h>
  180. #include "configuration_prusa.h"
  181. #include "language_all.h"
  182. #define LCD_WIDTH 20
  183. extern unsigned char lang_selected;
  184. END
  185. ;
  186. my @keys = sort(keys %texts);
  187. foreach my $key (@keys) {
  188. my $strings = $texts{$key};
  189. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  190. # Shrink the array to a single value.
  191. $strings = [${$strings}[0]];
  192. }
  193. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  194. my $suffix = uc($langs[$i]);
  195. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  196. print $fh "const char ${key}_${suffix}[] PROGMEM = ${$strings}[$i];\n";
  197. }
  198. }
  199. my $langnum = $#{$strings}+1;
  200. if ($langnum == $#langs+1) {
  201. $langnum = "LANG_NUM";
  202. }
  203. print $fh "const char * const ${key}_LANG_TABLE[$langnum] PROGMEM = {\n";
  204. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  205. my $suffix = uc($langs[$i]);
  206. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  207. print $fh "\t${key}_${suffix}";
  208. } else {
  209. print $fh "\t${key}_EN";
  210. }
  211. print $fh ',' if $i < $#{$strings};
  212. print $fh "\n";
  213. }
  214. print $fh "};\n\n";
  215. }
  216. print $fh <<'END'
  217. char langbuffer[LCD_WIDTH+1];
  218. char* CAT2(const char *s1,const char *s2) {
  219. unsigned char len=0;
  220. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  221. len+=strlen_P(s1);
  222. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  223. return langbuffer;
  224. }
  225. char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4) {
  226. unsigned char len=0;
  227. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  228. len+=strlen_P(s1);
  229. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  230. len+=strlen_P(s2);
  231. strncpy_P(langbuffer+len,s3,LCD_WIDTH-len);
  232. len+=strlen_P(s3);
  233. strncpy_P(langbuffer+len,s4,LCD_WIDTH-len);
  234. return langbuffer;
  235. }
  236. END
  237. ;
  238. print ".cpp created.\nDone!\n";
  239. for my $lang (0 .. $#langs) {
  240. print "Language: $langs[$lang]\n";
  241. foreach my $key (@keys) {
  242. my $strings = $texts{$key};
  243. my $message = ${$strings}[$lang];
  244. if ($lang == 0 || ${$strings}[0] ne $message) {
  245. # If the language is not English, don't show the non-translated message.
  246. my $lines = break_text_fullscreen($message);
  247. my $nlines = @{$lines};
  248. if ($nlines > 1) {
  249. print "Multi-line message: $message. Breaking to $nlines lines:\n";
  250. print "\t$_\n" foreach (@{$lines});
  251. }
  252. }
  253. }
  254. }
  255. sub break_text_fullscreen