]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DB/Unit.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / DB / Unit.pm
index 508aa747718cd2fe9a0520df18df204c347e023b..2c58ec66d95ab9e52f3a590bf912b07f8153afdb 100644 (file)
@@ -1,7 +1,7 @@
 package SL::DB::Unit;
 
 use List::MoreUtils qw(any);
-
+use List::Util qw(first);
 
 use strict;
 
@@ -15,6 +15,11 @@ __PACKAGE__->meta->add_relationships(
     class        => 'SL::DB::Unit',
     column_map   => { base_unit => 'name' },
   },
+  translations => {
+    type         => 'one to many',
+    class        => 'SL::DB::UnitsLanguage',
+    column_map   => { name => 'unit' },
+  },
 );
 
 __PACKAGE__->meta->initialize;
@@ -66,4 +71,23 @@ sub is_time_based {
   return any { $_->id == $self->id } @{ SL::DB::Manager::Unit->time_based_units };
 }
 
+sub get_translation_obj {
+  my ($self, $language) = @_;
+
+  my $language_id = (ref($language) eq 'SL::DB::Language' ? $language->id : $language) || undef;
+
+  return first { $_->language_id => $language_id } @{ $self->translations || [] };
+}
+
+sub get_translation {
+  my ($self, $language, $qty) = @_;
+
+  my $translation     = $self->name; # fallback, if no translation found
+  my $translation_obj = $self->get_translation_obj(language => $language);
+  if ($translation_obj) {
+    $translation = (($qty // 0) > 1 || ($qty // 0) == 0) ? $translation_obj->localized_plural : $translation_obj->localized;
+  }
+  return $translation;
+}
+
 1;