| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::Element::Select - Base select input element |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::Element::Select; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Log ':all'; |
| 13 |
use Debconf::Gettext; |
| 14 |
use base qw(Debconf::Element); |
| 15 |
use Debconf::Encoding qw(to_Unicode); |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
This is a base Select input element. |
| 20 |
|
| 21 |
=head1 METHODS |
| 22 |
|
| 23 |
=over 4 |
| 24 |
|
| 25 |
=item visible |
| 26 |
|
| 27 |
Select elements are not really visible if there are less than two choices |
| 28 |
for them. |
| 29 |
|
| 30 |
=cut |
| 31 |
|
| 32 |
sub visible { |
| 33 |
my $this=shift; |
| 34 |
|
| 35 |
my @choices=$this->question->choices_split; |
| 36 |
|
| 37 |
if (@choices > 1) { |
| 38 |
return 1; |
| 39 |
} |
| 40 |
else { |
| 41 |
debug 'developer' => 'Not displaying select list '. |
| 42 |
$this->question->name.' with '. |
| 43 |
(@choices+0).' choice'.((@choices == 0) ? 's' : ''); |
| 44 |
return 0; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
=item translate_default |
| 49 |
|
| 50 |
This method returns a default value, in the user's language, suitable for |
| 51 |
displaying to the user. Defaults are stored internally in the C locale; |
| 52 |
this method does any necessary translation to the current locale. |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
sub translate_default { |
| 57 |
my $this=shift; |
| 58 |
|
| 59 |
# I need both the translated and the non-translated choices. |
| 60 |
my @choices=$this->question->choices_split; |
| 61 |
$this->question->template->i18n(''); |
| 62 |
my @choices_c=$this->question->choices_split; |
| 63 |
$this->question->template->i18n(1); |
| 64 |
|
| 65 |
# Get the C default. |
| 66 |
my $c_default=''; |
| 67 |
$c_default=$this->question->value if defined $this->question->value; |
| 68 |
# Translate it. |
| 69 |
foreach (my $x=0; $x <= $#choices; $x++) { |
| 70 |
return $choices[$x] if $choices_c[$x] eq $c_default; |
| 71 |
} |
| 72 |
# If it's not in the list of choices, just ignore it. |
| 73 |
return ''; |
| 74 |
} |
| 75 |
|
| 76 |
=item translate_to_C |
| 77 |
|
| 78 |
Pass a value in the current locale in to this function, and it will look it |
| 79 |
up in the list of choices, convert it back to the C locale, and return it. |
| 80 |
|
| 81 |
=cut |
| 82 |
|
| 83 |
sub translate_to_C { |
| 84 |
my $this=shift; |
| 85 |
my $value=shift; |
| 86 |
|
| 87 |
# I need both the translated and the non-translated choices. |
| 88 |
my @choices=$this->question->choices_split; |
| 89 |
$this->question->template->i18n(''); |
| 90 |
my @choices_c=$this->question->choices_split; |
| 91 |
$this->question->template->i18n(1); |
| 92 |
|
| 93 |
for (my $x=0; $x <= $#choices; $x++) { |
| 94 |
return $choices_c[$x] if $choices[$x] eq $value; |
| 95 |
} |
| 96 |
debug developer => sprintf(gettext("Input value, \"%s\" not found in C choices! This should never happen. Perhaps the templates were incorrectly localized."), $value); |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
sub translate_to_C_uni { |
| 101 |
my $this=shift; |
| 102 |
my $value=shift; |
| 103 |
my @choices=$this->question->choices_split; |
| 104 |
$this->question->template->i18n(''); |
| 105 |
my @choices_c=$this->question->choices_split; |
| 106 |
$this->question->template->i18n(1); |
| 107 |
|
| 108 |
for (my $x=0; $x <= $#choices; $x++) { |
| 109 |
return $choices_c[$x] if to_Unicode($choices[$x]) eq $value; |
| 110 |
} |
| 111 |
debug developer => sprintf(gettext("Input value, \"%s\" not found in C choices! This should never happen. Perhaps the templates were incorrectly localized."), $value); |
| 112 |
return ''; |
| 113 |
} |
| 114 |
=back |
| 115 |
|
| 116 |
=head1 AUTHOR |
| 117 |
|
| 118 |
Joey Hess <[email protected]> |
| 119 |
|
| 120 |
=cut |
| 121 |
|
| 122 |
1 |