imgtools/imaker/buildrom_plugins/localise.pm
changeset 596 9f25be3da657
parent 584 56dd7656a965
equal deleted inserted replaced
595:997c19261166 596:9f25be3da657
    11 #
    11 #
    12 # Contributors:
    12 # Contributors:
    13 #
    13 #
    14 # Description:
    14 # Description:
    15 # Adds a LOCALISE macro that enables configuration of localised files.
    15 # Adds a LOCALISE macro that enables configuration of localised files.
    16 # The localised language selection is done with a ADD_LANGUAGE macro.
    16 # The localised language selection is done with a LANGUAGE_CODE macro.
    17 #
    17 #
    18 
    18 
    19 
    19 
    20 
    20 
    21 ###############################################################################
    21 ###############################################################################
    24 #   type=LOCALISE(source, target[, languages])
    24 #   type=LOCALISE(source, target[, languages])
    25 #   source => the source file. The section that needs to be localised should be marked with ??.
    25 #   source => the source file. The section that needs to be localised should be marked with ??.
    26 #   target => the target file. The section that needs to be localised should be marked with ??.
    26 #   target => the target file. The section that needs to be localised should be marked with ??.
    27 #   languages => a space delimited list of language codes
    27 #   languages => a space delimited list of language codes
    28 #
    28 #
    29 # Syntax: ADD_LANGUAGE
    29 # Syntax: LANGUAGE_CODE
    30 #   ADD_LANGUAGE lang
    30 #   LANGUAGE_CODE lang
    31 #
    31 #
    32 # Example:
    32 # Example:
    33 # Add languages
    33 # Add languages
    34 # ADD_LANGUAGE 01
    34 # LANGUAGE_CODE 01
    35 # ADD_LANGUAGE 02
    35 # LANGUAGE_CODE 02
    36 # ADD_LANGUAGE 03
    36 # LANGUAGE_CODE 03
    37 #
    37 #
    38 # Use Case 1:
    38 # Use Case 1:
    39 # Localises a App resoure file.
    39 # Localises a App resoure file.
    40 #   data=LOCALISE(APP_RESOURCE_DIR\App.r??, RESOURCE_DIR\app.r??)
    40 #   data=LOCALISE(APP_RESOURCE_DIR\App.r??, RESOURCE_DIR\app.r??)
    41 # Output:
    41 # Output:
    43 #   data=APP_RESOURCE_DIR\App.r02 RESOURCE_DIR\app.r02
    43 #   data=APP_RESOURCE_DIR\App.r02 RESOURCE_DIR\app.r02
    44 #   data=APP_RESOURCE_DIR\App.r03 RESOURCE_DIR\app.r03
    44 #   data=APP_RESOURCE_DIR\App.r03 RESOURCE_DIR\app.r03
    45 #
    45 #
    46 # Use Case 2:
    46 # Use Case 2:
    47 # Localise all resource files under a section
    47 # Localise all resource files under a section
    48 # ADD_LANGUAGE 01
    48 # LANGUAGE_CODE 01
    49 # ADD_LANGUAGE 02
    49 # LANGUAGE_CODE 02
    50 # ADD_LANGUAGE 03
    50 # LANGUAGE_CODE 03
    51 #
    51 #
    52 # LOCALISE_ALL_RESOURCES_BEGIN
    52 # LOCALISE_ALL_RESOURCES_BEGIN
    53 # // All resource files will be localised
    53 # // All resource files will be localised
    54 # data=APP_RESOURCE_DIR\App.rsc RESOURCE_DIR\app.rsc
    54 # data=APP_RESOURCE_DIR\App.rsc RESOURCE_DIR\app.rsc
    55 # data=APP_RESOURCE_DIR\App2.rsc RESOURCE_DIR\app2.rsc
    55 # data=APP_RESOURCE_DIR\App2.rsc RESOURCE_DIR\app2.rsc
   105 my @newobydata;
   105 my @newobydata;
   106 my %languages;
   106 my %languages;
   107 my $verbose=0;
   107 my $verbose=0;
   108 my $errors=0;
   108 my $errors=0;
   109 my $localise_all_resource=0;
   109 my $localise_all_resource=0;
       
   110 my %qt_langs = ();
   110 
   111 
   111 sub localise_info
   112 sub localise_info
   112   {
   113   {
   113   return \%localise_infostruct;
   114   return \%localise_infostruct;
   114   }
   115   }
   118 {
   119 {
   119   print "========================== Begin do_localise_extension =======================\n" if $verbose;
   120   print "========================== Begin do_localise_extension =======================\n" if $verbose;
   120   my $obydata = shift;
   121   my $obydata = shift;
   121   do_localise_all_resources_extension(\@{$obydata});
   122   do_localise_all_resources_extension(\@{$obydata});
   122 
   123 
   123 
       
   124   undef @newobydata;
   124   undef @newobydata;
   125   foreach $line (@{$obydata})
   125   foreach $line (@{$obydata})
   126   {
   126   {
   127     # Ignore REM statements, to avoid processing "REM __SCALABLE_IMAGE( ... )"
   127     # LANGUAGE_CODE xx
   128     if ($line =~ /^\s*REM/i)
       
   129     {
       
   130       push @newobydata, $line;
       
   131       next;
       
   132     }
       
   133     # ADD_LANGUAGE xx
       
   134     if (is_addlanguage_entry($line))
   128     if (is_addlanguage_entry($line))
   135     {
   129     {
   136       my $code = get_lang_from_addlanguage_entry($line);
   130       my $code = get_lang_from_addlanguage_entry($line);
   137       if ($code !~ /^\w\w+$/)
   131       if ($code !~ /^\w\w+$/)
   138       {
   132       {
   142       }
   136       }
   143       else
   137       else
   144       {
   138       {
   145         print "adding language $code\n" if $verbose;
   139         print "adding language $code\n" if $verbose;
   146         $languages{$code} = 1;
   140         $languages{$code} = 1;
   147         push @newobydata, "REM handled $line";
   141         push @newobydata, $line;
   148         next;
   142         next;
   149       }
   143       }
       
   144     }
       
   145     # Ignore REM statements, to avoid processing "REM __SCALABLE_IMAGE( ... )"
       
   146     if ($line =~ /^\s*REM/i)
       
   147     {
       
   148       push @newobydata, $line;
       
   149       next;
       
   150     }
       
   151     # Read QT to Symbian language id mappings
       
   152     if ($line =~ /^\s*QT_TO_SYMBIAN_LANGID\s+(.+?)\s*$/i)
       
   153     {
       
   154       push(@newobydata, "REM handled $line");
       
   155       open(FILE, $1) or
       
   156           die("ERROR: localise.pm can't open QT to Symbian language id mapping file `$1'\n");
       
   157       map {
       
   158           $qt_langs{$2} = $1 if (!/^\s*#/ && /^\s*(\S+?)\s*=\s*(\d+)\s*$/);
       
   159       } <FILE>;
       
   160       close(FILE);
       
   161       next;
   150     }
   162     }
   151     # LOCALISE macro
   163     # LOCALISE macro
   152     if (is_localise_entry($line))
   164     if (is_localise_entry($line))
   153     {
   165     {
   154       my @newdata = expand_localise_macro($line,\%languages);
   166       my @newdata = expand_localise_macro($line,\%languages);
   243   #create array with count amount of time of lang
   255   #create array with count amount of time of lang
   244   my @data = ();
   256   my @data = ();
   245   for (my $i=0; $i<$count; $i++) {
   257   for (my $i=0; $i<$count; $i++) {
   246     push(@data, $lang);
   258     push(@data, $lang);
   247   }
   259   }
   248   my $output = sprintf($res,@data);
   260 
       
   261   my $output;
       
   262   if (($res =~ m/\.qm["']?$/i) && %qt_langs)
       
   263         {
       
   264                 $output = sprintf($res, $qt_langs{sprintf("%s", @data)});
       
   265         }
       
   266   else
       
   267   {
       
   268         $output = sprintf($res,@data);
       
   269         }
   249   return $output;
   270   return $output;
   250 }
   271 }
   251 # match ADD_LANGUAGE 01
   272 # match LANGUAGE_CODE 01
   252 sub is_addlanguage_entry($)
   273 sub is_addlanguage_entry($)
   253 {
   274 {
   254   my $entry = shift;
   275   my $entry = shift;
   255   if ( $entry =~ m/^\s*ADD_LANGUAGE\s+(\S+)\s*/i )
   276   if ( $entry =~ m/^\s*REM\s+handled\s+LANGUAGE_CODE\s+(\S+)\s*$/i )
   256   {
   277   {
   257     return 1;
   278     return 1;
   258   }
   279   }
   259   return 0;
   280   return 0;
   260 }
   281 }
   261 #
   282 #
   262 sub get_lang_from_addlanguage_entry($)
   283 sub get_lang_from_addlanguage_entry($)
   263 {
   284 {
   264   my $entry = shift;
   285   my $entry = shift;
   265   if ( $entry =~ m/^\s*ADD_LANGUAGE\s+(\S+)/i )
   286   if ( $entry =~ m/^\s*REM\s+handled\s+LANGUAGE_CODE\s+(\S+)\s*$/i )
   266   {
   287   {
   267     return $1;
   288     return $1;
   268   }
   289   }
   269   return "";
   290   return "";
   270 }
   291 }