| 1 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # Curses::UI::Language | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (c) 2001-2002 by Maurice Makaay. All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # This file is part of Curses::UI. Curses::UI is free software. | ||||||||||||||||||||||||||||
| 6 | # You can redistribute it and/or modify it under the same terms | ||||||||||||||||||||||||||||
| 7 | # as perl itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # Currently maintained by Marcus Thiesen | ||||||||||||||||||||||||||||
| 10 | # e-mail: marcus@cpan.thiesenweb.de | ||||||||||||||||||||||||||||
| 11 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Curses::UI::Language; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | my $default_lang = 'English'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | my %lang_alias = ( | |||||||||||||||||||||||||||
| 18 | 'en' => 'english', | ||||||||||||||||||||||||||||
| 19 | 'uk' => 'english', | ||||||||||||||||||||||||||||
| 20 | 'us' => 'english', | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 'it' => 'italian', | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 'pl' => 'polish', | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 'ru' => 'russian', | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 'de' => 'german', | ||||||||||||||||||||||||||||
| 29 | 'at' => 'german', | ||||||||||||||||||||||||||||
| 30 | 'ch' => 'german', | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 'du' => 'dutch', | ||||||||||||||||||||||||||||
| 33 | 'nl' => 'dutch', | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 'fr' => 'french', | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 'pt' => 'portuguese', | ||||||||||||||||||||||||||||
| 38 | 'pt_BR' => 'portuguese', | ||||||||||||||||||||||||||||
| 39 | 'br' => 'portuguese', | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 'no' => 'norwegian', | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 'es' => 'spanish', | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 'tr' => 'tukish', | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 'cn' => 'chinese', | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub new() | ||||||||||||||||||||||||||||
| 52 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 54 | 9 | my $lang = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 9 | my $this = { | |||||||||||||||||||||||||||
| 57 | -tags => {}, | ||||||||||||||||||||||||||||
| 58 | -lang => undef, | ||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | 9 | bless $this, $class; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Load english tags so these can be used | ||||||||||||||||||||||||||||
| 63 | # as a fallback for other languages. | ||||||||||||||||||||||||||||
| 64 | 9 | $this->loadlanguage('english'); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Load the wanted language. | ||||||||||||||||||||||||||||
| 67 | 9 | $this->loadlanguage($lang); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | return $this; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub loadlanguage($;) | ||||||||||||||||||||||||||||
| 73 | 18 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
loadlanguage: called CC=14 SCAR=26.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 18 | my $this = shift; | |||||||||||||||||||||||||||
| 75 | 18 | my $lang = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Construct the language module to use. | ||||||||||||||||||||||||||||
| 78 | 18 | ▶ | $lang = $default_lang unless defined $lang; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 18 | $lang =~ s/[^\w\_]//g; | |||||||||||||||||||||||||||
| 80 | 18 | $lang = lc $lang; | |||||||||||||||||||||||||||
| 81 | 18 | ▶ | $lang = $lang_alias{$lang} if defined $lang_alias{$lang}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Loading the same language twice is not very useful. | ||||||||||||||||||||||||||||
| 84 | return $this if defined $this->{-lang} and | ||||||||||||||||||||||||||||
| 85 | 18 | ▶ | $lang eq $this->{-lang}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $this->{"-lang"} and $lang eq $this->{"-lang"}
MC/DC50%1/2
defined $this->{"-lang"} and $lang eq $this->{"-lang"}
defined $this->{"-lang"}
$lang eq $this->{"-lang"}
Truth table66%2/3
A and B
A= defined $this->{"-lang"}B= $lang eq $this->{"-lang"}
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Determine filename for the language package. | ||||||||||||||||||||||||||||
| 88 | 9 | (my $l_file = __FILE__) =~ s/\.pm$/\/$lang\.pm/; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Save the name of the currently loaded language. | ||||||||||||||||||||||||||||
| 91 | 9 | $this->{-lang} = $lang; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Create a filehandle to the __DATA__ section | ||||||||||||||||||||||||||||
| 94 | # of the language package. | ||||||||||||||||||||||||||||
| 95 | 9 | local *LANG_DATA; | |||||||||||||||||||||||||||
| 96 | 9 | ▶ | open(LANG_DATA, "< $l_file") or die "Can't open $l_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 9 | ▶ | while (<LANG_DATA>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 72 | ▶ | last if /^\s*__DATA__$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Read and store tags/blocks. | ||||||||||||||||||||||||||||
| 103 | 9 | my $tag = undef; | |||||||||||||||||||||||||||
| 104 | 9 | my $block = ''; | |||||||||||||||||||||||||||
| 105 | 9 | ▶ | LINE: while (<LANG_DATA>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 639 | ▶ | if (m/^#/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 107 | 198 | next LINE; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | elsif (m/^\s*\[\s*(.*)\s*\]\s*(.*)$/) { | ||||||||||||||||||||||||||||
| 110 | 207 | my $oldtag = $tag; | |||||||||||||||||||||||||||
| 111 | 207 | $tag = $1; | |||||||||||||||||||||||||||
| 112 | 207 | $this->store($oldtag, $block); | |||||||||||||||||||||||||||
| 113 | 207 | $block = $2; | |||||||||||||||||||||||||||
| 114 | 207 | ▶ | $block = '' unless defined $block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | elsif (defined $tag) { | ||||||||||||||||||||||||||||
| 117 | 216 | $block .= "$_"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | elsif (!m/^\s*$/) { | ||||||||||||||||||||||||||||
| 120 | 0 | warn "$l_file, line $.: found data outside tag block\n"; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 9 | $this->store($tag, $block); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 9 | close(LANG_DATA); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub store($$;) | ||||||||||||||||||||||||||||
| 129 | 216 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=6 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 216 | my $this = shift; | |||||||||||||||||||||||||||
| 131 | 216 | my $tag = shift; | |||||||||||||||||||||||||||
| 132 | 216 | my $block = shift; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 216 | ▶ | return $this unless defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Remove empty start- and endlines. | ||||||||||||||||||||||||||||
| 137 | 207 | my @block = split /\n/, $block; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | while (@block and $block[0] =~ /^\s*$/) { shift @block } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@block and $block[0] =~ /^\s*$/
MC/DC0%0/2
@block and $block[0] =~ /^\s*$/
@block
$block[0] =~ /^\s*$/
Truth table33%1/3
A and B
A= @blockB= $block[0] =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | while (@block and $block[-1] =~ /^\s*$/) { pop @block } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@block and $block[-1] =~ /^\s*$/
MC/DC0%0/2
@block and $block[-1] =~ /^\s*$/
@block
$block[-1] =~ /^\s*$/
Truth table33%1/3
A and B
A= @blockB= $block[-1] =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 207 | $this->{-tags}->{lc $tag} = join "\n", @block; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 207 | return $this; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub get($;) | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 150 | 0 | my $tag = shift; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $block = $this->{-tags}->{$tag}; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | unless (defined $block) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | warn "get(): no language block for tag '$tag'"; | |||||||||||||||||||||||||||
| 155 | 0 | $block = ''; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | return $block; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub getarray($;) | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getarray: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 164 | 0 | my $tag = shift; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $block = $this->get($tag); | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | return () unless defined $block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | $block =~ s/\n/ /g; | |||||||||||||||||||||||||||
| 170 | 0 | return split " ", $block; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 8 | 1; | |||||||||||||||||||||||||||