Support for RTL(right to left) setting in the translation pipeline
[openemr.git] / contrib / util / language_translations / combineConstantsSpreadsheet.pl
blob4c54442f7759ffd1c09858365f5a30f1a0cb8228
1 #!/usr/bin/perl
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # author Brady Miller
9 # email brady@sparmy.com
10 # date 04/03/09
12 # This is a perl script that will create a new ranslation spreadsheet
13 # file from a constant file and an old spreadsheet file. Output will
14 # go to newSpreadsheet.tsv and errors and message swill be logged to
15 # log.txt.
17 # Example commands:
19 # -Below will output the new spreadsheet:
20 # ./commonConstantsSpreadsheet.pl spreadsheet.tsv constants.txt
23 use strict;
25 my $filenameOut = "newSpreadsheet.tsv";
26 my $logFile = "log.txt";
27 my $de = "\t"; # delimiter
28 my @constants;
29 my @spreadsheet;
30 my @newSpreadsheet;
31 my $inputSpreadsheet;
32 my $inputConstants;
33 my $constantRow = 6; # from spreadsheet, 0 is lowest
34 my $constantColumn = 1; # from spreadsheet, 0 is lowest
35 my $constantIdColumn = 0; # from spreadsheet, 0 is lowest
37 # open log file
38 open(LOGFILE, ">$logFile") or die "unable to open log file";
40 # collect parameters
41 if (@ARGV > 2) {
42 die "\nERROR: Too many parameters. Follow instructions found in buildLanguageDatabase.pl file.\n\n";
44 elsif (@ARGV < 2) {
45 die "\nERROR: Need a parameter(s). Follow instructions found in buildLanguageDatabase.pl file.\n\n";
47 elsif (@ARGV == 2) {
48 $inputSpreadsheet = $ARGV[0];
49 $inputConstants = $ARGV[1];
51 else {
52 print LOGFILE "ERROR: with parameters\n\n";
53 die "ERROR: with parameters\n\n";
56 # open output files
57 open(OUTPUTFILE, ">$filenameOut") or die "unable to open output file";
59 # place input files into arrays and chomp them
60 open(MYINPUTFILE, "<$inputSpreadsheet") or die "unable to open file";
61 @spreadsheet = <MYINPUTFILE>;
62 close(MYINPUTFILE);
63 for my $var (@spreadsheet) {
64 chomp($var);
66 open(MYINPUTFILE2, "<$inputConstants") or die "unable to open file";
67 @constants = <MYINPUTFILE2>;
68 close(MYINPUTFILE2);
69 for my $var (@constants) {
70 chomp($var);
73 # first create header on new spreadsheet while removing it from spreadsheet array
74 for (my $i =0; $i < $constantRow; $i++) {
75 push (@newSpreadsheet, shift(@spreadsheet));
78 # place common constants into the spreadsheet array
79 my $idCounter = 1; # to assign constant id numbers
80 my $hitFlag; # flag to ensure ad new constants to spreadsheet
81 my @origSpreadConstants; # to keep track of removed constants later
82 my @finalSpreadConstants; # to keep track of removed constants later
83 my $first = 1; # to keep track of removed constants later
84 foreach my $var2 (@constants) {
86 $hitFlag = 0; # reset the hit flag
88 foreach my $var (@spreadsheet) {
90 my @tempArr = split($de,$var);
91 my $tempCons = @tempArr[$constantColumn];
93 # collect the original listing of constants during first loop
94 if ($first) {
95 push(@origSpreadConstants,$tempCons)
98 if ($tempCons eq $var2) {
99 # add to array to keep track of removed constants
100 push(@finalSpreadConstants,$tempCons);
102 # create new id number
103 @tempArr[$constantIdColumn] = $idCounter;
104 my $newLine = join($de,@tempArr);
105 $idCounter +=1;
107 # place into array
108 push (@newSpreadsheet,$newLine);
110 # set the hit flag
111 $hitFlag = 1;
116 $first = 0;
118 if (!($hitFlag)) {
119 # constant is new, so add to spreadsheet
120 push (@newSpreadsheet,$idCounter.$de.$var2);
121 push (@finalSpreadConstants,$var2); # for later error checking
122 print LOGFILE "ADDED: ".$var2."\n";
123 $idCounter +=1;
127 # send the removed constants to a log file
128 foreach my $var (@origSpreadConstants) {
129 if (!(withinArray($var,@finalSpreadConstants))) {
130 print LOGFILE "REMOVED: ".$var."\n";
134 # send the added constants to a log file
135 # this is redundant to hit method above
136 # no need for this method, so comment out
137 # foreach my $var (@finalSpreadConstants) {
138 # if (!(withinArray($var,@origSpreadConstants))) {
139 # print LOGFILE "ADDED: ".$var."\n";
143 # output the common constants
144 foreach my $var (@newSpreadsheet) {
145 print OUTPUTFILE $var."\n";
148 # close output file
149 close(OUTPUTFILE);
153 # FUNCTIONS
156 # function to return whether a variable is in an array
157 # param - $variable @arr
158 # return - 1(true) or 0(false) integer
160 sub withinArray {
161 my($variable,@arr) = @_;
162 my $isMatch = 0;
163 foreach my $tempVar (@arr) {
164 if ($tempVar eq $variable) {
165 $isMatch = 1;
166 last;
169 return $isMatch;