1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
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.
14#
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).
20#
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.
25#
26#*************************************************************************
27
28package pre2par::language;
29
30use pre2par::existence;
31
32##############################################################
33# Returning a specific language string from the block
34# of all translations
35##############################################################
36
37sub get_language_string_from_language_block
38{
39	my ($language_block, $language) = @_;
40
41	my $newstring = "";
42
43	for ( my $i = 0; $i <= $#{$language_block}; $i++ )
44	{
45
46		if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
47		{
48			$newstring = $1;
49			$newstring =~ s/\"/\\\"/g;	# masquerading all '"' in the string
50			$newstring = "\"" . $newstring . "\"";
51			last;
52		}
53	}
54
55	# defaulting to english!
56
57	if ( $newstring eq "" )
58	{
59		$language = "en-US"; 	# defaulting to english
60
61		for ( my $i = 0; $i <= $#{$language_block}; $i++ )
62		{
63			if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ )
64			{
65				$newstring = $1;
66				last;
67			}
68		}
69	}
70
71	return $newstring;
72}
73
74##############################################################
75# Returning the complete block in all languages
76# for a specified string
77##############################################################
78
79sub get_language_block_from_language_file
80{
81	my ($searchstring, $langfile) = @_;
82
83	my @language_block = ();
84
85	for ( my $i = 0; $i <= $#{$langfile}; $i++ )
86	{
87		if ( ${$langfile}[$i] =~ /^\s*\[\s*$searchstring\s*\]\s*$/ )
88		{
89			my $counter = $i;
90
91			push(@language_block, ${$langfile}[$counter]);
92			$counter++;
93
94			while (( $counter <= $#{$langfile} ) && (!( ${$langfile}[$counter] =~ /^\s*\[/ )))
95			{
96				push(@language_block, ${$langfile}[$counter]);
97				$counter++;
98			}
99
100			last;
101		}
102	}
103
104	return \@language_block;
105}
106
107############################################
108# collecting all replace strings
109# in a language file
110############################################
111
112sub get_all_replace_strings
113{
114	my ($langfile) = @_;
115
116	my @allstrings = ();
117
118	for ( my $i = 0; $i <= $#{$langfile}; $i++ )
119	{
120		if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ )
121		{
122			my $replacestring = $1;
123			if (! pre2par::existence::exists_in_array($replacestring, \@allstrings))
124			{
125				push(@allstrings, $replacestring);
126			}
127		}
128	}
129
130	return \@allstrings;
131}
132
133############################################
134# localizing the par file with the
135# corresponding language file
136############################################
137
138sub localize
139{
140	my ($parfile, $langfile) = @_;
141
142	my $allreplacestrings = get_all_replace_strings($langfile);
143
144	for ( my $i = 0; $i <= $#{$parfile}; $i++ )
145	{
146		my $oneline = ${$parfile}[$i];
147
148		for ( my $j = 0; $j <= $#{$allreplacestrings}; $j++ )
149		{
150			if ( $oneline =~ /\b${$allreplacestrings}[$j]\b/ ) # Not for basic scripts
151			{
152				my $oldstring = ${$allreplacestrings}[$j];
153
154				if ( $oneline =~ /^\s*\w+\s*\(([\w-]+)\)\s*\=/ )
155				{
156					my $language = $1; 	 # can be "01" or "en" or "en-US" or ...
157
158					my $languageblock = get_language_block_from_language_file($oldstring, $langfile);
159					my $newstring = get_language_string_from_language_block($languageblock, $language);
160
161					if ( $newstring eq "" )	{ $newstring = "\"" . $oldstring . "\""; }
162
163					$oneline =~ s/$oldstring/$newstring/g;
164
165					${$parfile}[$i] = $oneline;
166				}
167			}
168		}
169	}
170}
171
1721;
173