Bump for 3.6-28
[LibreOffice.git] / solenv / bin / modules / pre2par / language.pm
blob13718af695a5d53b4a218f093ac5b45034e4dd58
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2000, 2010 Oracle and/or its affiliates.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # This file is part of OpenOffice.org.
11 # OpenOffice.org is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License version 3
13 # only, as published by the Free Software Foundation.
15 # OpenOffice.org is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License version 3 for more details
19 # (a copy is included in the LICENSE file that accompanied this code).
21 # You should have received a copy of the GNU Lesser General Public License
22 # version 3 along with OpenOffice.org. If not, see
23 # <http://www.openoffice.org/license.html>
24 # for a copy of the LGPLv3 License.
26 #*************************************************************************
28 package pre2par::language;
30 use strict;
32 ##############################################################
33 # Returning a specific language string from the block
34 # of all translations
35 ##############################################################
37 sub get_language_string_from_language_block
39 my ($language_block, $language) = @_;
41 my $newstring = "";
43 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
46 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
48 $newstring = $1;
49 $newstring =~ s/\"/\\\"/g; # masquerading all '"' in the string
50 $newstring = "\"" . $newstring . "\"";
51 last;
55 # defaulting to english!
57 if ( $newstring eq "" )
59 $language = "en-US"; # defaulting to english
61 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
63 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ )
65 $newstring = $1;
66 last;
71 return $newstring;
74 ############################################
75 # collecting all replace variables
76 # in a language file
77 ############################################
79 sub get_all_replace_variables
81 my ($langfile) = @_;
83 my %allvars = ();
85 for ( my $i = 0; $i <= $#{$langfile}; $i++ )
87 if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ )
89 my $variable = $1;
90 # print "lang block '$variable'\n";
91 my @lang_block = ();
92 my $counter;
94 # Store the complete block in all languages for a specified variable
95 for ( $counter = $i + 1; $counter <= $#{$langfile}; $counter++ ) {
96 my $line = ${$langfile}[$counter];
97 last if ($line =~ /^s*\[/); # next decl.
98 push @lang_block, $line;
100 # print "$variable = '@lang_block'\n";
101 $allvars{$variable} = \@lang_block;
102 $i = $counter - 1;
106 return \%allvars;
109 ############################################
110 # localizing the par file with the
111 # corresponding language file
112 ############################################
114 sub localize
116 my ($parfile, $langfile) = @_;
118 my $replace_hash = get_all_replace_variables($langfile);
120 # parse lines of the form Name (st) = STR_NAME_MODULE_HELPPACK_OC;
121 # for variable substitution
122 my $langlinere = qr/^\s*\w+\s*\(([\w-]+)\)\s*\=\s*([\w-]+)\s*;/;
123 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
125 my $oneline = ${$parfile}[$i];
127 if ( $oneline =~ $langlinere) {
128 my $language = $1; # can be "01" or "en" or "en-US" or ...
129 my $variable = $2;
131 # print "line '$oneline' split to '$language' '$variable'\n";
133 if (defined $replace_hash->{$variable}) {
134 my $languageblock = $replace_hash->{$variable};
135 my $newstring = get_language_string_from_language_block($replace_hash->{$variable}, $language);
136 if ( $newstring eq "" ) { $newstring = "\"" . $variable . "\""; }
138 $oneline =~ s/$variable/$newstring/g;
140 ${$parfile}[$i] = $oneline;