langtool.pl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. 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('!');
  61. }
  62. sub break_text_fullscreen
  63. {
  64. my $lines = [];
  65. my ($text_str) = @_;
  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 + ((20 > $len) ? $len : 20);
  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. $attributes{$key} = $symbol_value;
  122. } else {
  123. print "Duplicate key in language_common.h: $key\n";
  124. }
  125. }
  126. }
  127. foreach my $lang (@langs) {
  128. my $symbols = parselang("language_$lang.h");
  129. foreach my $key (keys %{$symbols}) {
  130. if (! (exists $texts{$key})) {
  131. $texts{$key} = [];
  132. }
  133. my $symbol_value = ${$symbols}{$key};
  134. my $strings = $texts{$key};
  135. if (defined $attributes{$key} && defined ${$attributes{$key}}{common} && ${$attributes{$key}}{common} == 1) {
  136. # Common overrides the possible definintions in the language specific files.
  137. } else {
  138. die "Symbol $key defined first in $lang, undefined in the preceding language files."
  139. if (scalar(@$strings) != $num_languages);
  140. push @$strings, ${$symbol_value}{value};
  141. if ($lang eq 'en') {
  142. # The english texts may contain attributes. Store them into %attributes.
  143. delete ${$symbol_value}{value};
  144. $attributes{$key} = $symbol_value;
  145. }
  146. }
  147. }
  148. $num_languages += 1;
  149. foreach my $key (keys %texts) {
  150. my $strings = $texts{$key};
  151. if (scalar(@$strings) < $num_languages) {
  152. # die "Symbol $key undefined in $lang."
  153. print "Symbol $key undefined in language \"$lang\". Using the english variant.\n";
  154. push @$strings, ${$strings}[0];
  155. }
  156. }
  157. }
  158. my $filename = 'language_all.h';
  159. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  160. # For the programmatic access to the program memory, read
  161. # http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
  162. print $fh <<END
  163. #ifndef LANGUAGE_ALL_H
  164. #define LANGUAGE_ALL_H
  165. // Language indices into their particular symbol tables.
  166. END
  167. ;
  168. # Export symbolic IDs of languages.
  169. for my $i (0 .. $#langs) {
  170. my $lang = uc $langs[$i];
  171. print $fh "#define LANG_ID_$lang $i\n";
  172. }
  173. print $fh <<END
  174. // Language is not defined and it shall be selected from the menu.
  175. #define LANG_ID_FORCE_SELECTION 254
  176. // Language is not defined on a virgin RAMBo board.
  177. #define LANG_ID_UNDEFINED 255
  178. // Default language ID, if no language is selected.
  179. #define LANG_ID_DEFAULT LANG_ID_CZ
  180. // Number of languages available in the language table.
  181. #define LANG_NUM ${num_languages}
  182. // Currectly active language selection.
  183. extern unsigned char lang_selected;
  184. #define LANG_TABLE_SELECT_EXPLICIT(TABLE, LANG) ((const char*)(pgm_read_ptr(TABLE + (LANG))))
  185. #define LANG_TABLE_SELECT(TABLE) LANG_TABLE_SELECT_EXPLICIT(TABLE, lang_selected)
  186. END
  187. ;
  188. foreach my $key (sort(keys %texts)) {
  189. my $strings = $texts{$key};
  190. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  191. # All strings are English.
  192. print $fh "extern const char* const ${key}_LANG_TABLE[1];\n";
  193. print $fh "#define $key LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, 0)\n";
  194. } else {
  195. print $fh "extern const char* const ${key}_LANG_TABLE[LANG_NUM];\n";
  196. print $fh "#define $key LANG_TABLE_SELECT(${key}_LANG_TABLE)\n";
  197. print $fh "#define ${key}_EXPLICIT(LANG) LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, LANG)\n"
  198. if ($key eq "MSG_LANGUAGE_NAME" || $key eq "MSG_LANGUAGE_SELECT");
  199. }
  200. }
  201. print $fh <<END
  202. extern char* CAT2(const char *s1,const char *s2);
  203. extern char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4);
  204. #endif //LANGUAGE_ALL.H
  205. END
  206. ;
  207. close $fh;
  208. print ".h created\n";
  209. $filename = 'language_all.cpp';
  210. open($fh, '>', $filename) or die "Could not open file '$filename' $!";
  211. print $fh <<'END'
  212. #include <avr/pgmspace.h>
  213. #include "configuration_prusa.h"
  214. #include "language_all.h"
  215. #define LCD_WIDTH 20
  216. extern unsigned char lang_selected;
  217. END
  218. ;
  219. my @keys = sort(keys %texts);
  220. foreach my $key (@keys) {
  221. my $strings = $texts{$key};
  222. if (@{$strings} == grep { $_ eq ${$strings}[0] } @{$strings}) {
  223. # Shrink the array to a single value.
  224. $strings = [${$strings}[0]];
  225. }
  226. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  227. my $suffix = uc($langs[$i]);
  228. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  229. print $fh "const char ${key}_${suffix}[] PROGMEM = ${$strings}[$i];\n";
  230. }
  231. }
  232. my $langnum = $#{$strings}+1;
  233. if ($langnum == $#langs+1) {
  234. $langnum = "LANG_NUM";
  235. }
  236. print $fh "const char * const ${key}_LANG_TABLE[$langnum] PROGMEM = {\n";
  237. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  238. my $suffix = uc($langs[$i]);
  239. if ($i == 0 || ${$strings}[$i] ne ${$strings}[0]) {
  240. print $fh "\t${key}_${suffix}";
  241. } else {
  242. print $fh "\t${key}_EN";
  243. }
  244. print $fh ',' if $i < $#{$strings};
  245. print $fh "\n";
  246. }
  247. print $fh "};\n\n";
  248. }
  249. print $fh <<'END'
  250. char langbuffer[LCD_WIDTH+1];
  251. char* CAT2(const char *s1,const char *s2) {
  252. unsigned char len=0;
  253. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  254. len+=strlen_P(s1);
  255. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  256. return langbuffer;
  257. }
  258. char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4) {
  259. unsigned char len=0;
  260. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  261. len+=strlen_P(s1);
  262. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  263. len+=strlen_P(s2);
  264. strncpy_P(langbuffer+len,s3,LCD_WIDTH-len);
  265. len+=strlen_P(s3);
  266. strncpy_P(langbuffer+len,s4,LCD_WIDTH-len);
  267. return langbuffer;
  268. }
  269. END
  270. ;
  271. print ".cpp created.\nDone!\n";
  272. my $verify_only = 1;
  273. for my $lang (0 .. $#langs) {
  274. print "Language: $langs[$lang]\n";
  275. foreach my $key (@keys) {
  276. my $strings = $texts{$key};
  277. my %attrib = %{$attributes{$key}};
  278. my $message = ${$strings}[$lang];
  279. if ($lang == 0 || ${$strings}[0] ne $message) {
  280. # If the language is not English, don't show the non-translated message.
  281. my $lines = break_text_fullscreen($message);
  282. my $nlines = @{$lines};
  283. if (! $verify_only) {
  284. if ($nlines > 1) {
  285. print "Multi-line message: $message. Breaking to $nlines lines:\n";
  286. print "\t$_\n" foreach (@{$lines});
  287. }
  288. }
  289. if (defined $attrib{lines} && $nlines > $attrib{lines}) {
  290. print "Key $key, language $langs[$lang], lines: $nlines, max lines: $attrib{lines}\n";
  291. }
  292. }
  293. }
  294. }