1*9780544fSAndrew Rist#**************************************************************
2*9780544fSAndrew Rist#
3*9780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*9780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*9780544fSAndrew Rist#  distributed with this work for additional information
6*9780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*9780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*9780544fSAndrew Rist#  "License"); you may not use this file except in compliance
9*9780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*9780544fSAndrew Rist#
11*9780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*9780544fSAndrew Rist#
13*9780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*9780544fSAndrew Rist#  software distributed under the License is distributed on an
15*9780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
17*9780544fSAndrew Rist#  specific language governing permissions and limitations
18*9780544fSAndrew Rist#  under the License.
19*9780544fSAndrew Rist#
20*9780544fSAndrew Rist#**************************************************************
21*9780544fSAndrew Rist
22*9780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage pre2par::language;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse pre2par::existence;
27cdf0e10cSrcweir
28cdf0e10cSrcweir##############################################################
29cdf0e10cSrcweir# Returning a specific language string from the block
30cdf0e10cSrcweir# of all translations
31cdf0e10cSrcweir##############################################################
32cdf0e10cSrcweir
33cdf0e10cSrcweirsub get_language_string_from_language_block
34cdf0e10cSrcweir{
35cdf0e10cSrcweir	my ($language_block, $language) = @_;
36cdf0e10cSrcweir
37cdf0e10cSrcweir	my $newstring = "";
38cdf0e10cSrcweir
39cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$language_block}; $i++ )
40cdf0e10cSrcweir	{
41cdf0e10cSrcweir
42cdf0e10cSrcweir		if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
43cdf0e10cSrcweir		{
44cdf0e10cSrcweir			$newstring = $1;
45cdf0e10cSrcweir			$newstring =~ s/\"/\\\"/g;	# masquerading all '"' in the string
46cdf0e10cSrcweir			$newstring = "\"" . $newstring . "\"";
47cdf0e10cSrcweir			last;
48cdf0e10cSrcweir		}
49cdf0e10cSrcweir	}
50cdf0e10cSrcweir
51cdf0e10cSrcweir	# defaulting to english!
52cdf0e10cSrcweir
53cdf0e10cSrcweir	if ( $newstring eq "" )
54cdf0e10cSrcweir	{
55cdf0e10cSrcweir		$language = "en-US"; 	# defaulting to english
56cdf0e10cSrcweir
57cdf0e10cSrcweir		for ( my $i = 0; $i <= $#{$language_block}; $i++ )
58cdf0e10cSrcweir		{
59cdf0e10cSrcweir			if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ )
60cdf0e10cSrcweir			{
61cdf0e10cSrcweir				$newstring = $1;
62cdf0e10cSrcweir				last;
63cdf0e10cSrcweir			}
64cdf0e10cSrcweir		}
65cdf0e10cSrcweir	}
66cdf0e10cSrcweir
67cdf0e10cSrcweir	return $newstring;
68cdf0e10cSrcweir}
69cdf0e10cSrcweir
70cdf0e10cSrcweir##############################################################
71cdf0e10cSrcweir# Returning the complete block in all languages
72cdf0e10cSrcweir# for a specified string
73cdf0e10cSrcweir##############################################################
74cdf0e10cSrcweir
75cdf0e10cSrcweirsub get_language_block_from_language_file
76cdf0e10cSrcweir{
77cdf0e10cSrcweir	my ($searchstring, $langfile) = @_;
78cdf0e10cSrcweir
79cdf0e10cSrcweir	my @language_block = ();
80cdf0e10cSrcweir
81cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$langfile}; $i++ )
82cdf0e10cSrcweir	{
83cdf0e10cSrcweir		if ( ${$langfile}[$i] =~ /^\s*\[\s*$searchstring\s*\]\s*$/ )
84cdf0e10cSrcweir		{
85cdf0e10cSrcweir			my $counter = $i;
86cdf0e10cSrcweir
87cdf0e10cSrcweir			push(@language_block, ${$langfile}[$counter]);
88cdf0e10cSrcweir			$counter++;
89cdf0e10cSrcweir
90cdf0e10cSrcweir			while (( $counter <= $#{$langfile} ) && (!( ${$langfile}[$counter] =~ /^\s*\[/ )))
91cdf0e10cSrcweir			{
92cdf0e10cSrcweir				push(@language_block, ${$langfile}[$counter]);
93cdf0e10cSrcweir				$counter++;
94cdf0e10cSrcweir			}
95cdf0e10cSrcweir
96cdf0e10cSrcweir			last;
97cdf0e10cSrcweir		}
98cdf0e10cSrcweir	}
99cdf0e10cSrcweir
100cdf0e10cSrcweir	return \@language_block;
101cdf0e10cSrcweir}
102cdf0e10cSrcweir
103cdf0e10cSrcweir############################################
104cdf0e10cSrcweir# collecting all replace strings
105cdf0e10cSrcweir# in a language file
106cdf0e10cSrcweir############################################
107cdf0e10cSrcweir
108cdf0e10cSrcweirsub get_all_replace_strings
109cdf0e10cSrcweir{
110cdf0e10cSrcweir	my ($langfile) = @_;
111cdf0e10cSrcweir
112cdf0e10cSrcweir	my @allstrings = ();
113cdf0e10cSrcweir
114cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$langfile}; $i++ )
115cdf0e10cSrcweir	{
116cdf0e10cSrcweir		if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ )
117cdf0e10cSrcweir		{
118cdf0e10cSrcweir			my $replacestring = $1;
119cdf0e10cSrcweir			if (! pre2par::existence::exists_in_array($replacestring, \@allstrings))
120cdf0e10cSrcweir			{
121cdf0e10cSrcweir				push(@allstrings, $replacestring);
122cdf0e10cSrcweir			}
123cdf0e10cSrcweir		}
124cdf0e10cSrcweir	}
125cdf0e10cSrcweir
126cdf0e10cSrcweir	return \@allstrings;
127cdf0e10cSrcweir}
128cdf0e10cSrcweir
129cdf0e10cSrcweir############################################
130cdf0e10cSrcweir# localizing the par file with the
131cdf0e10cSrcweir# corresponding language file
132cdf0e10cSrcweir############################################
133cdf0e10cSrcweir
134cdf0e10cSrcweirsub localize
135cdf0e10cSrcweir{
136cdf0e10cSrcweir	my ($parfile, $langfile) = @_;
137cdf0e10cSrcweir
138cdf0e10cSrcweir	my $allreplacestrings = get_all_replace_strings($langfile);
139cdf0e10cSrcweir
140cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$parfile}; $i++ )
141cdf0e10cSrcweir	{
142cdf0e10cSrcweir		my $oneline = ${$parfile}[$i];
143cdf0e10cSrcweir
144cdf0e10cSrcweir		for ( my $j = 0; $j <= $#{$allreplacestrings}; $j++ )
145cdf0e10cSrcweir		{
146cdf0e10cSrcweir			if ( $oneline =~ /\b${$allreplacestrings}[$j]\b/ ) # Not for basic scripts
147cdf0e10cSrcweir			{
148cdf0e10cSrcweir				my $oldstring = ${$allreplacestrings}[$j];
149cdf0e10cSrcweir
150cdf0e10cSrcweir				if ( $oneline =~ /^\s*\w+\s*\(([\w-]+)\)\s*\=/ )
151cdf0e10cSrcweir				{
152cdf0e10cSrcweir					my $language = $1; 	 # can be "01" or "en" or "en-US" or ...
153cdf0e10cSrcweir
154cdf0e10cSrcweir					my $languageblock = get_language_block_from_language_file($oldstring, $langfile);
155cdf0e10cSrcweir					my $newstring = get_language_string_from_language_block($languageblock, $language);
156cdf0e10cSrcweir
157cdf0e10cSrcweir					if ( $newstring eq "" )	{ $newstring = "\"" . $oldstring . "\""; }
158cdf0e10cSrcweir
159cdf0e10cSrcweir					$oneline =~ s/$oldstring/$newstring/g;
160cdf0e10cSrcweir
161cdf0e10cSrcweir					${$parfile}[$i] = $oneline;
162cdf0e10cSrcweir				}
163cdf0e10cSrcweir			}
164cdf0e10cSrcweir		}
165cdf0e10cSrcweir	}
166cdf0e10cSrcweir}
167cdf0e10cSrcweir
168cdf0e10cSrcweir1;
169