| 1 |
# |
| 2 |
# This file is part of the LibreOffice project. |
| 3 |
# |
| 4 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
# |
| 8 |
# This file incorporates work covered by the following license notice: |
| 9 |
# |
| 10 |
# Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
# contributor license agreements. See the NOTICE file distributed |
| 12 |
# with this work for additional information regarding copyright |
| 13 |
# ownership. The ASF licenses this file to you under the Apache |
| 14 |
# License, Version 2.0 (the "License"); you may not use this file |
| 15 |
# except in compliance with the License. You may obtain a copy of |
| 16 |
# the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
# |
| 18 |
|
| 19 |
package pre2par::language; |
| 20 |
|
| 21 |
use strict; |
| 22 |
|
| 23 |
############################################################## |
| 24 |
# Returning a specific language string from the block |
| 25 |
# of all translations |
| 26 |
############################################################## |
| 27 |
|
| 28 |
sub get_language_string_from_language_block |
| 29 |
{ |
| 30 |
my ($language_block, $language) = @_; |
| 31 |
|
| 32 |
my $newstring = ""; |
| 33 |
|
| 34 |
for ( my $i = 0; $i <= $#{$language_block}; $i++ ) |
| 35 |
{ |
| 36 |
|
| 37 |
if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ ) |
| 38 |
{ |
| 39 |
$newstring = $1; |
| 40 |
$newstring = "\"" . $newstring . "\""; |
| 41 |
last; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
# defaulting to english! |
| 46 |
|
| 47 |
if ( $newstring eq "" ) |
| 48 |
{ |
| 49 |
$language = "en-US"; # defaulting to english |
| 50 |
|
| 51 |
for ( my $i = 0; $i <= $#{$language_block}; $i++ ) |
| 52 |
{ |
| 53 |
if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*(\".*\")\s*$/ ) |
| 54 |
{ |
| 55 |
$newstring = $1; |
| 56 |
last; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return $newstring; |
| 62 |
} |
| 63 |
|
| 64 |
############################################ |
| 65 |
# collecting all replace variables |
| 66 |
# in a language file |
| 67 |
############################################ |
| 68 |
|
| 69 |
sub get_all_replace_variables |
| 70 |
{ |
| 71 |
my ($langfile) = @_; |
| 72 |
|
| 73 |
my %allvars = (); |
| 74 |
|
| 75 |
for ( my $i = 0; $i <= $#{$langfile}; $i++ ) |
| 76 |
{ |
| 77 |
if ( ${$langfile}[$i] =~ /^\s*\[\s*(.*?)\s*\]\s*$/ ) |
| 78 |
{ |
| 79 |
my $variable = $1; |
| 80 |
# print "lang block '$variable'\n"; |
| 81 |
my @lang_block = (); |
| 82 |
my $counter; |
| 83 |
|
| 84 |
# Store the complete block in all languages for a specified variable |
| 85 |
for ( $counter = $i + 1; $counter <= $#{$langfile}; $counter++ ) { |
| 86 |
my $line = ${$langfile}[$counter]; |
| 87 |
last if ($line =~ /^s*\[/); # next decl. |
| 88 |
push @lang_block, $line; |
| 89 |
} |
| 90 |
# print "$variable = '@lang_block'\n"; |
| 91 |
$allvars{$variable} = \@lang_block; |
| 92 |
$i = $counter - 1; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return \%allvars; |
| 97 |
} |
| 98 |
|
| 99 |
############################################ |
| 100 |
# localizing the par file with the |
| 101 |
# corresponding language file |
| 102 |
############################################ |
| 103 |
|
| 104 |
sub localize |
| 105 |
{ |
| 106 |
my ($parfile, $langfile) = @_; |
| 107 |
|
| 108 |
my $replace_hash = get_all_replace_variables($langfile); |
| 109 |
|
| 110 |
# parse lines of the form Name (st) = STR_NAME_MODULE_HELPPACK_OC; |
| 111 |
# for variable substitution |
| 112 |
my $langlinere = qr/^\s*\w+\s*\(([\w-]+)\)\s*\=\s*([\w-]+)\s*;/; |
| 113 |
for ( my $i = 0; $i <= $#{$parfile}; $i++ ) |
| 114 |
{ |
| 115 |
my $oneline = ${$parfile}[$i]; |
| 116 |
|
| 117 |
if ( $oneline =~ $langlinere) { |
| 118 |
my $language = $1; # can be "01" or "en" or "en-US" or ... |
| 119 |
my $variable = $2; |
| 120 |
|
| 121 |
# print "line '$oneline' split to '$language' '$variable'\n"; |
| 122 |
|
| 123 |
if (defined $replace_hash->{$variable}) { |
| 124 |
my $newstring = get_language_string_from_language_block($replace_hash->{$variable}, $language); |
| 125 |
if ( $newstring eq "" ) { $newstring = "\"" . $variable . "\""; } |
| 126 |
|
| 127 |
$oneline =~ s/$variable/$newstring/g; |
| 128 |
|
| 129 |
${$parfile}[$i] = $oneline; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
1; |