#!/usr/bin/perl -w
#use utf8;

# open input files for reading
open(STRINGS_DTD, "localizable_strings.dtd") or die "Can't open translated localizable_strings.dtd: $!\n";
open(EN_STRINGS_DTD, "localizable_strings_en.dtd") or die "Can't open original localizable_strings.dtd: $!\n";
# open output file for writing
open(LOCALIZABLE_STRINGS, ">Localizable.strings.uft8") or die "Can't open Localizable.strings.utf8: $!\n";


@entities = ();
%en_strings = ();
%trans_strings = ();

#create array of entities and hashtable of original strings
while ($line = <EN_STRINGS_DTD>) {
  if($line=~m/(\w+)\.label\s*\"(.*)\"/)
  {
    push @entities, $1;
    $en_strings{$1} = $2;
  }
}

# create hashtable of translated strings
while ($line = <STRINGS_DTD>) {
  if($line=~m/(\w+)\.label\s*\"(.*)\"/)
  {
    $trans_strings{$1} = $2;
  }
}

# create translated Localizable.strings for all valid entities
# use original string if no translation is provided
foreach $current_entity (@entities){
  $line="";
  $en_string = "";
  $trans_string = "";
  if ($en_strings{$current_entity}){
    $en_string = $en_strings{$current_entity};
    $trans_string = $en_string;
    
    if ($trans_strings{$current_entity}){
      $trans_string = $trans_strings{$current_entity};
    }
    $line = "\"$en_string\" = \"$trans_string\"\n";
    print LOCALIZABLE_STRINGS $line;
  }
}