update dev300-m58
[ooovba.git] / solenv / bin / modules / pre2par / language.pm
blobb55fb2930fa6903de4b1bf372e7730c23912fc96
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: language.pm,v $
11 # $Revision: 1.8 $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
32 package pre2par::language;
34 use pre2par::existence;
36 ##############################################################
37 # Returning a specific language string from the block
38 # of all translations
39 ##############################################################
41 sub get_language_string_from_language_block
43 my ($language_block, $language) = @_;
45 my $newstring = "";
47 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
50 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
52 $newstring = $1;
53 $newstring =~ s/\"/\\\"/g; # masquerading all '"' in the string
54 $newstring = "\"" . $newstring . "\"";
55 last;
59 # defaulting to english!
61 if ( $newstring eq "" )
63 $language = "en-US"; # defaulting to english
65 for ( my $i = 0; $i <= $#{$language_block}; $i++ )
67 if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ )
69 $newstring = $1;
70 last;
75 return $newstring;
78 ##############################################################
79 # Returning the complete block in all languages
80 # for a specified string
81 ##############################################################
83 sub get_language_block_from_language_file
85 my ($searchstring, $langfile) = @_;
87 my @language_block = ();
89 for ( my $i = 0; $i <= $#{$langfile}; $i++ )
91 if ( ${$langfile}[$i] =~ /^\s*\[\s*$searchstring\s*\]\s*$/ )
93 my $counter = $i;
95 push(@language_block, ${$langfile}[$counter]);
96 $counter++;
98 while (( $counter <= $#{$langfile} ) && (!( ${$langfile}[$counter] =~ /^\s*\[/ )))
100 push(@language_block, ${$langfile}[$counter]);
101 $counter++;
104 last;
108 return \@language_block;
111 ############################################
112 # collecting all replace strings
113 # in a language file
114 ############################################
116 sub get_all_replace_strings
118 my ($langfile) = @_;
120 my @allstrings = ();
122 for ( my $i = 0; $i <= $#{$langfile}; $i++ )
124 if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ )
126 my $replacestring = $1;
127 if (! pre2par::existence::exists_in_array($replacestring, \@allstrings))
129 push(@allstrings, $replacestring);
134 return \@allstrings;
137 ############################################
138 # localizing the par file with the
139 # corresponding language file
140 ############################################
142 sub localize
144 my ($parfile, $langfile) = @_;
146 my $allreplacestrings = get_all_replace_strings($langfile);
148 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
150 my $oneline = ${$parfile}[$i];
152 for ( my $j = 0; $j <= $#{$allreplacestrings}; $j++ )
154 if ( $oneline =~ /\b${$allreplacestrings}[$j]\b/ ) # Not for basic scripts
156 my $oldstring = ${$allreplacestrings}[$j];
158 if ( $oneline =~ /^\s*\w+\s*\(([\w-]+)\)\s*\=/ )
160 my $language = $1; # can be "01" or "en" or "en-US" or ...
162 my $languageblock = get_language_block_from_language_file($oldstring, $langfile);
163 my $newstring = get_language_string_from_language_block($languageblock, $language);
165 if ( $newstring eq "" ) { $newstring = "\"" . $oldstring . "\""; }
167 $oneline =~ s/$oldstring/$newstring/g;
169 ${$parfile}[$i] = $oneline;