1 package SL::DB::Helper::TranslatedAttributes;
5 use SL::DB::GenericTranslation;
7 use parent qw(Exporter);
8 our @EXPORT = qw(translated_attribute save_attribute_translation);
12 sub translated_attribute {
13 my ($self, $attribute, $language_id, %params) = @_;
15 $language_id = _check($self, $attribute, $language_id);
16 my $translation = _find_translation($self, $attribute, $language_id, 0);
17 $translation ||= _find_translation($self, $attribute, undef, 0);
19 return $translation ? $translation->translation : $self->$attribute;
22 sub save_attribute_translation {
23 my ($self, $attribute, $language_id, $value) = @_;
25 $language_id = _check($self, $attribute, $language_id);
27 return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value);
31 my ($self, $attribute, $language_id) = @_;
33 croak "Invalid attribute '${attribute}'" unless $self->can($attribute);
34 croak "Object has not been saved yet" unless $self->id;
36 return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id;
39 sub _find_translation {
40 my ($self, $attribute, $language_id, $new_if_not_found) = @_;
42 my %params = (language_id => $language_id,
43 translation_type => ref($self). '/' . $attribute,
44 translation_id => $self->id);
46 return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef);
57 SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving
58 translations for certain model attributes in the table
59 I<generic_translations>
65 package SL::DB::SomeObject;
66 use SL::DB::Helper::Translated;
70 my $object = SL::DB::SomeObject->new(id => $::form->{id})->load;
71 my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch');
72 print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n";
74 print "Now saving new value\n";
75 my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus');
81 =item C<translated_attribute $attribute, $language_id>
83 Returns the translation stored for the attribute C<$attribute> and the
84 language C<$language_id> (either an ID or an instance of
87 If no translation exists for C<$language_id> or if C<$language_id> is
88 undefined then the default translation is looked up.
90 If neither translation exists then the value of C<< $self->$attribute >>
93 Requires that C<$self> has a primary ID column named C<id> and that
94 the object has been saved.
96 =item C<save_attribute_translation $attribute, $language_id, $value>
98 Saves the translation C<$value> for the attribute C<$attribute> and
99 the language C<$language_id> (either an ID or an instance of
100 L<SL::DB::Language>).
102 If C<$language_id> is undefined then the default translation will be
105 Requires that C<$self> has a primary ID column named C<id> and that
106 the object has been saved.
108 Returns the same value as C<save>.
114 This mixin exports the functions L</translated_attribute> and
115 L</save_attribute_translation>.
123 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>