langtool.pl 7.5 KB

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