Revision 76da5bf5
Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt
SL/DB/Helper/TranslatedAttributes.pm | ||
---|---|---|
10 | 10 |
use Carp; |
11 | 11 |
|
12 | 12 |
sub translated_attribute { |
13 |
my ($self, $attribute, $language_id, %params) = @_;
|
|
13 |
my ($self, $attribute, $language_id, $verbatim) = @_;
|
|
14 | 14 |
|
15 |
$language_id = _check($self, $attribute, $language_id); |
|
15 |
$language_id = _check($self, $attribute, $language_id, $verbatim);
|
|
16 | 16 |
my $translation = _find_translation($self, $attribute, $language_id, 0); |
17 |
$translation ||= _find_translation($self, $attribute, undef, 0); |
|
17 |
$translation ||= _find_translation($self, $attribute, undef, 0) unless $verbatim;
|
|
18 | 18 |
|
19 |
return $translation ? $translation->translation : $self->$attribute; |
|
19 |
return $translation ? $translation->translation |
|
20 |
: $verbatim ? undef |
|
21 |
: $self->$attribute; |
|
20 | 22 |
} |
21 | 23 |
|
22 | 24 |
sub save_attribute_translation { |
... | ... | |
28 | 30 |
} |
29 | 31 |
|
30 | 32 |
sub _check { |
31 |
my ($self, $attribute, $language_id) = @_; |
|
33 |
my ($self, $attribute, $language_id, $verbatim) = @_;
|
|
32 | 34 |
|
33 | 35 |
croak "Invalid attribute '${attribute}'" unless $self->can($attribute); |
34 |
croak "Object has not been saved yet" unless $self->id; |
|
36 |
croak "Object has not been saved yet" unless $self->id || $verbatim;
|
|
35 | 37 |
|
36 | 38 |
return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id; |
37 | 39 |
} |
... | ... | |
78 | 80 |
|
79 | 81 |
=over 4 |
80 | 82 |
|
81 |
=item C<translated_attribute $attribute, $language_id> |
|
83 |
=item C<translated_attribute $attribute, $language_id, $verbatim>
|
|
82 | 84 |
|
83 | 85 |
Returns the translation stored for the attribute C<$attribute> and the |
84 | 86 |
language C<$language_id> (either an ID or an instance of |
85 | 87 |
L<SL::DB::Language>). |
86 | 88 |
|
87 |
If no translation exists for C<$language_id> or if C<$language_id> is |
|
88 |
undefined then the default translation is looked up. |
|
89 |
If C<$verbatim> is falsish and either no translation exists for |
|
90 |
C<$language_id> or if C<$language_id> is undefined then the default |
|
91 |
translation is looked up. |
|
89 | 92 |
|
90 |
If neither translation exists then the value of C<< $self->$attribute >>
|
|
91 |
is returned. |
|
93 |
If C<$verbatim> is falsish and neither translation exists then the
|
|
94 |
value of C<< $self->$attribute >> is returned.
|
|
92 | 95 |
|
93 | 96 |
Requires that C<$self> has a primary ID column named C<id> and that |
94 | 97 |
the object has been saved. |
Auch abrufbar als: Unified diff
Modus, um nur die tatsächliche Übersetzung zu liefern, keinen Fallback