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