tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / solenv / bin / modules / pre2par / language.pm
blob332bb985f860e62aaeb741e10376bdc13016e6d9
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package pre2par::language;
21 use strict;
23 ##############################################################
24 # Returning a specific language string from the block
25 # of all translations
26 ##############################################################
28 sub get_language_string_from_language_block
30 my ($language_block, $language) = @_;
32 my $newstring = "";
34 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
37 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
39 $newstring = $1;
40 $newstring = "\"" . $newstring . "\"";
41 last;
45 # defaulting to english!
47 if ( $newstring eq "" )
49 $language = "en-US"; # defaulting to english
51 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
53 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ )
55 $newstring = $1;
56 last;
61 return $newstring;
64 ############################################
65 # collecting all replace variables
66 # in a language file
67 ############################################
69 sub get_all_replace_variables
71 my ($langfile) = @_;
73 my %allvars = ();
75 for ( my $i = 0; $i <= $#{$langfile}; $i++ )
77 if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ )
79 my $variable = $1;
80 # print "lang block '$variable'\n";
81 my @lang_block = ();
82 my $counter;
84 # Store the complete block in all languages for a specified variable
85 for ( $counter = $i + 1; $counter <= $#{$langfile}; $counter++ ) {
86 my $line = ${$langfile}[$counter];
87 last if ($line =~ /^s*\[/); # next decl.
88 push @lang_block, $line;
90 # print "$variable = '@lang_block'\n";
91 $allvars{$variable} = \@lang_block;
92 $i = $counter - 1;
96 return \%allvars;
99 ############################################
100 # localizing the par file with the
101 # corresponding language file
102 ############################################
104 sub localize
106 my ($parfile, $langfile) = @_;
108 my $replace_hash = get_all_replace_variables($langfile);
110 # parse lines of the form Name (st) = STR_NAME_MODULE_HELPPACK_OC;
111 # for variable substitution
112 my $langlinere = qr/^\s*\w+\s*\(([\w-]+)\)\s*\=\s*([\w-]+)\s*;/;
113 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
115 my $oneline = ${$parfile}[$i];
117 if ( $oneline =~ $langlinere) {
118 my $language = $1; # can be "01" or "en" or "en-US" or ...
119 my $variable = $2;
121 # print "line '$oneline' split to '$language' '$variable'\n";
123 if (defined $replace_hash->{$variable}) {
124 my $newstring = get_language_string_from_language_block($replace_hash->{$variable}, $language);
125 if ( $newstring eq "" ) { $newstring = "\"" . $variable . "\""; }
127 $oneline =~ s/$variable/$newstring/g;
129 ${$parfile}[$i] = $oneline;