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, $verbatim) = @_;
15 $language_id = _check($self, $attribute, $language_id, $verbatim);
16 my $translation = _find_translation($self, $attribute, $language_id, 0);
17 $translation ||= _find_translation($self, $attribute, undef, 0) unless $verbatim;
19 return $translation ? $translation->translation
24 sub save_attribute_translation {
25 my ($self, $attribute, $language_id, $value) = @_;
27 $language_id = _check($self, $attribute, $language_id);
29 return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value);
33 my ($self, $attribute, $language_id, $verbatim) = @_;
35 croak "Invalid attribute '${attribute}'" unless $self->can($attribute);
36 croak "Object has not been saved yet" unless $self->id || $verbatim;
38 return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id;
41 sub _find_translation {
42 my ($self, $attribute, $language_id, $new_if_not_found) = @_;
44 my %params = (language_id => $language_id,
45 translation_type => ref($self). '/' . $attribute,
46 translation_id => $self->id);
48 return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef);
59 SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving
60 translations for certain model attributes in the table
61 I<generic_translations>
67 package SL::DB::SomeObject;
68 use SL::DB::Helper::Translated;
72 my $object = SL::DB::SomeObject->new(id => $::form->{id})->load;
73 my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch');
74 print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n";
76 print "Now saving new value\n";
77 my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus');
83 =item C<translated_attribute $attribute, $language_id, $verbatim>
85 Returns the translation stored for the attribute C<$attribute> and the
86 language C<$language_id> (either an ID or an instance of
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.
93 If C<$verbatim> is falsish and neither translation exists then the
94 value of C<< $self->$attribute >> is returned.
96 Requires that C<$self> has a primary ID column named C<id> and that
97 the object has been saved.
99 =item C<save_attribute_translation $attribute, $language_id, $value>
101 Saves the translation C<$value> for the attribute C<$attribute> and
102 the language C<$language_id> (either an ID or an instance of
103 L<SL::DB::Language>).
105 If C<$language_id> is undefined then the default translation will be
108 Requires that C<$self> has a primary ID column named C<id> and that
109 the object has been saved.
111 Returns the same value as C<save>.
117 This mixin exports the functions L</translated_attribute> and
118 L</save_attribute_translation>.
126 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>