imgtools/imaker/buildrom_plugins/localise.pm
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Symbian Foundation License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 # Adds a LOCALISE macro that enables configuration of localised files.
       
    16 # The localised language selection is done with a ADD_LANGUAGE macro.
       
    17 #
       
    18 
       
    19 
       
    20 
       
    21 ###############################################################################
       
    22 #
       
    23 # Syntax: LOCALISE
       
    24 #   type=LOCALISE(source, target[, languages])
       
    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 ??.
       
    27 #   languages => a space delimited list of language codes
       
    28 #
       
    29 # Syntax: ADD_LANGUAGE
       
    30 #   ADD_LANGUAGE lang
       
    31 #
       
    32 # Example:
       
    33 # Add languages
       
    34 # ADD_LANGUAGE 01
       
    35 # ADD_LANGUAGE 02
       
    36 # ADD_LANGUAGE 03
       
    37 #
       
    38 # Use Case 1:
       
    39 # Localises a App resoure file.
       
    40 #   data=LOCALISE(APP_RESOURCE_DIR\App.r??, RESOURCE_DIR\app.r??)
       
    41 # Output:
       
    42 #   data=APP_RESOURCE_DIR\App.r01 RESOURCE_DIR\app.r01
       
    43 #   data=APP_RESOURCE_DIR\App.r02 RESOURCE_DIR\app.r02
       
    44 #   data=APP_RESOURCE_DIR\App.r03 RESOURCE_DIR\app.r03
       
    45 #
       
    46 # Use Case 2:
       
    47 # Localise all resource files under a section
       
    48 # ADD_LANGUAGE 01
       
    49 # ADD_LANGUAGE 02
       
    50 # ADD_LANGUAGE 03
       
    51 #
       
    52 # LOCALISE_ALL_RESOURCES_BEGIN
       
    53 # // All resource files will be localised
       
    54 # data=APP_RESOURCE_DIR\App.rsc RESOURCE_DIR\app.rsc
       
    55 # data=APP_RESOURCE_DIR\App2.rsc RESOURCE_DIR\app2.rsc
       
    56 # LOCALISE_ALL_RESOURCES_END
       
    57 # Output:
       
    58 #   data=APP_RESOURCE_DIR\App.r01 RESOURCE_DIR\app.r01
       
    59 #   data=APP_RESOURCE_DIR\App.r02 RESOURCE_DIR\app.r02
       
    60 #   data=APP_RESOURCE_DIR\App.r03 RESOURCE_DIR\app.r03
       
    61 #   data=APP_RESOURCE_DIR\App.r01 RESOURCE_DIR\app.r01
       
    62 #   data=APP_RESOURCE_DIR\App.r02 RESOURCE_DIR\app.r02
       
    63 #   data=APP_RESOURCE_DIR\App.r03 RESOURCE_DIR\app.r03
       
    64 #
       
    65 ###############################################################################
       
    66 
       
    67 
       
    68 package localise;
       
    69 use strict;
       
    70 use localise_all_resources;
       
    71 
       
    72 BEGIN
       
    73   {
       
    74   use Exporter ();
       
    75   our ( $VERSION, @ISA, @EXPORT );
       
    76   # set the version for version checking
       
    77   $VERSION     = 1.00;
       
    78 
       
    79   @ISA         = qw( Exporter );
       
    80   @EXPORT      = qw( &localise_info
       
    81                      &do_localise_extension
       
    82                      &convert_lang
       
    83                      &trim
       
    84                      &is_addlanguage_entry
       
    85                      &get_lang_from_addlanguage_entry
       
    86                      &is_localise_entry
       
    87                      &get_type_from_localise_entry
       
    88                      &get_source_from_localise_entry
       
    89                      &get_target_from_localise_entry
       
    90                      &get_langs_from_localise_entry
       
    91                      &parse_component_langs
       
    92                      &expand_localise_macro
       
    93                      &is_language_in_component_langs
       
    94                       );
       
    95   }
       
    96 
       
    97 my %localise_infostruct =
       
    98   (
       
    99   name => "localise",
       
   100   invocation => "InvocationPoint1",
       
   101   single => "localise::do_localise_extension"
       
   102   );
       
   103 
       
   104 my $line;
       
   105 my @newobydata;
       
   106 my %languages;
       
   107 my $verbose=0;
       
   108 my $errors=0;
       
   109 my $localise_all_resource=0;
       
   110 
       
   111 sub localise_info
       
   112   {
       
   113   return \%localise_infostruct;
       
   114   }
       
   115 
       
   116 # Entry point for the plugi
       
   117 sub do_localise_extension
       
   118 {
       
   119   print "========================== Begin do_localise_extension =======================\n" if $verbose;
       
   120   my $obydata = shift;
       
   121   do_localise_all_resources_extension(\@{$obydata});
       
   122 
       
   123 
       
   124   undef @newobydata;
       
   125   foreach $line (@{$obydata})
       
   126   {
       
   127     # Ignore REM statements, to avoid processing "REM __SCALABLE_IMAGE( ... )"
       
   128     if ($line =~ /^\s*REM/i)
       
   129     {
       
   130       push @newobydata, $line;
       
   131       next;
       
   132     }
       
   133     # ADD_LANGUAGE xx
       
   134     if (is_addlanguage_entry($line))
       
   135     {
       
   136       my $code = get_lang_from_addlanguage_entry($line);
       
   137       if ($code !~ /^\w\w+$/)
       
   138       {
       
   139         print "ERROR: bad default language code $code";
       
   140         #$errors++;
       
   141         next;
       
   142       }
       
   143       else
       
   144       {
       
   145         print "adding language $code\n" if $verbose;
       
   146         $languages{$code} = 1;
       
   147         push @newobydata, "REM handled $line";
       
   148         next;
       
   149       }
       
   150     }
       
   151     # LOCALISE macro
       
   152     if (is_localise_entry($line))
       
   153     {
       
   154       my @newdata = expand_localise_macro($line,\%languages);
       
   155       push @newobydata, @newdata;
       
   156       next;
       
   157     }
       
   158     # Default case
       
   159     push @newobydata, $line;
       
   160   }
       
   161   @{$obydata} = @newobydata;
       
   162   print "========================== End do_localise_extension =======================\n" if $verbose;
       
   163   #Stop image creation in error case
       
   164   #exit(1) if ($errors);
       
   165 }
       
   166 
       
   167 sub expand_localise_macro
       
   168 {
       
   169   my $data         = $_[0];
       
   170   my %theLanguages = %{ $_[1] };
       
   171   my @localised = ();
       
   172   print "LOCALISE $data\n" if $verbose;
       
   173 
       
   174   my $type   = get_type_from_localise_entry($data);
       
   175   my $source = get_source_from_localise_entry($data);
       
   176   my $target = get_target_from_localise_entry($data);
       
   177   my %componentLangs = get_langs_from_localise_entry($data);
       
   178 
       
   179   my @languages = sort keys %theLanguages;
       
   180   foreach my $lang (@languages)
       
   181   {
       
   182     print "Language ".$lang."\n" if $verbose;
       
   183     my $sourcedata = convert_lang($source,$lang);
       
   184     my $targetdata = convert_lang($target,$lang);
       
   185 
       
   186     # Check does the component have overriding configurations
       
   187     # The component specific setting can define the component to ignore localisation
       
   188     if ( !is_language_in_component_langs($lang,\%componentLangs) )
       
   189     {
       
   190       #Component specific configuration overrides the global lang definitions
       
   191       print "WARNING: Component specific configuration removes this resource $source\n"  if $verbose;
       
   192       next;
       
   193     }
       
   194 
       
   195     my $data = "$type=$sourcedata $targetdata\n";
       
   196     print "lang data $data\n" if $verbose;
       
   197     #push the data to the new structure
       
   198     push @localised, $data;
       
   199   }
       
   200   return @localised;
       
   201 }
       
   202 
       
   203 sub is_language_in_component_langs($$)
       
   204 {
       
   205   my $lang           = $_[0];
       
   206   my %componentLangs = %{ $_[1] };
       
   207   #Check whether the component langs is empty
       
   208   if ( (keys %componentLangs) > 0)
       
   209   {
       
   210     if (exists $componentLangs{ $lang })
       
   211     {
       
   212       return $componentLangs{ $lang };
       
   213     }
       
   214     else
       
   215     {
       
   216       return 0;
       
   217     }
       
   218   }
       
   219   else
       
   220   {
       
   221     return 1;
       
   222   }
       
   223 
       
   224 }
       
   225 # trim(string)
       
   226 # Removes spaces from both ends of the string.
       
   227 # Returns a trimmed string.
       
   228 sub trim($)
       
   229 {
       
   230   my $string = shift;
       
   231   $string =~ s/^\s+//;
       
   232   $string =~ s/\s+$//;
       
   233   return $string;
       
   234 }
       
   235 
       
   236 # convert_lang(string)
       
   237 # convert the string ?? part to the lang specific
       
   238 sub convert_lang($$)
       
   239 {
       
   240   my $res = shift;
       
   241   my $lang= shift;
       
   242   my $count = ($res =~ tr/%//);
       
   243   #create array with count amount of time of lang
       
   244   my @data = ();
       
   245   for (my $i=0; $i<$count; $i++) {
       
   246     push(@data, $lang);
       
   247   }
       
   248   my $output = sprintf($res,@data);
       
   249   return $output;
       
   250 }
       
   251 # match ADD_LANGUAGE 01
       
   252 sub is_addlanguage_entry($)
       
   253 {
       
   254   my $entry = shift;
       
   255   if ( $entry =~ m/^\s*ADD_LANGUAGE\s+(\S+)\s*/i )
       
   256   {
       
   257     return 1;
       
   258   }
       
   259   return 0;
       
   260 }
       
   261 #
       
   262 sub get_lang_from_addlanguage_entry($)
       
   263 {
       
   264   my $entry = shift;
       
   265   if ( $entry =~ m/^\s*ADD_LANGUAGE\s+(\S+)/i )
       
   266   {
       
   267     return $1;
       
   268   }
       
   269   return "";
       
   270 }
       
   271 
       
   272 # match data=LOCALISE(foobar.rsc, resource/foobar.rsc)
       
   273 sub is_localise_entry($)
       
   274 {
       
   275   my $entry = shift;
       
   276   if ( $entry =~ m/^\s*\S+\s*=\s*LOCALISE(\s*(\S+),\s*(\S+))/i )
       
   277   {
       
   278     return 1;
       
   279   }
       
   280   return 0;
       
   281 }
       
   282 # get the type from an iby entry
       
   283 sub get_type_from_localise_entry($)
       
   284 {
       
   285   my $entry = shift;
       
   286   if ( $entry =~ m/^\s*(\S+)\s*=/i )
       
   287   {
       
   288     return $1;
       
   289   }
       
   290   return "";
       
   291 }
       
   292 # get the source file from an iby entry
       
   293 sub get_source_from_localise_entry($)
       
   294 {
       
   295   my $entry = shift;
       
   296   if ( $entry =~ m/^\s*(\S+)\s*=\s*LOCALISE\(\s*([^, ]+)\s*,/i )
       
   297   {
       
   298     return $2;
       
   299   }
       
   300   return "";
       
   301 }
       
   302 # get the target file from an iby entry
       
   303 sub get_target_from_localise_entry($)
       
   304 {
       
   305   my $entry = shift;
       
   306   if ( $entry =~ m/^\s*(\S+)\s*=\s*LOCALISE\(\s*([^, ]+)\s*,\s*([^, ]+)(,|\))/i )
       
   307   {
       
   308     return $3;
       
   309   }
       
   310   return "";
       
   311 }
       
   312 # get the target file from an iby entry
       
   313 sub get_langs_from_localise_entry($)
       
   314 {
       
   315   my $entry = shift;
       
   316   my %emptyhash;
       
   317   if ( $entry =~ m/^\s*(\S+)\s*=\s*LOCALISE\(\s*([^, ]+)\s*,\s*([^, ]+)\s*,\s*(.*?)\)/i )
       
   318   {
       
   319     if ($4)
       
   320     {
       
   321       return parse_component_langs($4);
       
   322     }
       
   323   }
       
   324   return %emptyhash;
       
   325 }
       
   326 sub parse_component_langs($)
       
   327 {
       
   328   my $langs = shift;
       
   329   my %cLangs;
       
   330   foreach my $item (split(/ /,$langs))
       
   331   {
       
   332   print "lang item $item\n" if $verbose;
       
   333   if ($item =~ /^(\w\w+)$/)
       
   334     {
       
   335     print "include component specific language $1\n" if $verbose;
       
   336     $cLangs{$1} = 1;
       
   337     }
       
   338   elsif ($item =~ /^!(\w\w+)$/)
       
   339     {
       
   340     print "exclude component specific language $1\n" if $verbose;
       
   341     $cLangs{$1} = 0;
       
   342     }
       
   343   else
       
   344     {
       
   345     print "ERROR: bad default language code in $item localise macro $langs\n";
       
   346     $errors++;
       
   347     next;
       
   348     }
       
   349   }
       
   350   return %cLangs;
       
   351 }
       
   352 
       
   353 
       
   354 1;  # Return a true value from the file