X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FTranslatedAttributes.pm;h=2c3857c7acc97e9ccb3c6aa62d2db935ed8071fa;hb=0ebb8f829ac2d23b65e8d993c7d03aa0b172b637;hp=51e7e295fb01fbe6691bbf72df13907175633288;hpb=0f63a447e4ba5d3c85741dda984a0236f7e79abd;p=kivitendo-erp.git diff --git a/SL/DB/Helper/TranslatedAttributes.pm b/SL/DB/Helper/TranslatedAttributes.pm index 51e7e295f..2c3857c7a 100644 --- a/SL/DB/Helper/TranslatedAttributes.pm +++ b/SL/DB/Helper/TranslatedAttributes.pm @@ -10,13 +10,18 @@ our @EXPORT = qw(translated_attribute save_attribute_translation); use Carp; sub translated_attribute { - my ($self, $attribute, $language_id, %params) = @_; + my ($self, $attribute, $language_id, $verbatim) = @_; - $language_id = _check($self, $attribute, $language_id); - my $translation = _find_translation($self, $attribute, $language_id, 0); - $translation ||= _find_translation($self, $attribute, undef, 0); + $language_id = _check($self, $attribute, $language_id, $verbatim); + my $translation_obj = _find_translation($self, $attribute, $language_id, 0); + my $translation = $translation_obj ? $translation_obj->translation : ''; - return $translation ? $translation->translation : $self->$attribute; + return $translation if $verbatim || $translation; + + $translation_obj = _find_translation($self, $attribute, undef, 0); + $translation = $translation_obj ? $translation_obj->translation : ''; + + return $translation || $self->$attribute; } sub save_attribute_translation { @@ -28,12 +33,12 @@ sub save_attribute_translation { } sub _check { - my ($self, $attribute, $language_id) = @_; + my ($self, $attribute, $language_id, $verbatim) = @_; croak "Invalid attribute '${attribute}'" unless $self->can($attribute); - croak "Object has not been saved yet" unless $self->id; + croak "Object has not been saved yet" unless $self->id || $verbatim; - return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id; + return (ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id) || undef; } sub _find_translation { @@ -78,17 +83,18 @@ Usage: =over 4 -=item C +=item C Returns the translation stored for the attribute C<$attribute> and the language C<$language_id> (either an ID or an instance of L). -If no translation exists for C<$language_id> or if C<$language_id> is -undefined then the default translation is looked up. +If C<$verbatim> is falsish and either no translation exists for +C<$language_id> or if C<$language_id> is undefined then the default +translation is looked up. -If neither translation exists then the value of C<< $self->$attribute >> -is returned. +If C<$verbatim> is falsish and neither translation exists then the +value of C<< $self->$attribute >> is returned. Requires that C<$self> has a primary ID column named C and that the object has been saved.