Revision babf7f54
Von Bernd Bleßmann vor mehr als 1 Jahr hinzugefügt
SL/DB/Unit.pm | ||
---|---|---|
1 | 1 |
package SL::DB::Unit; |
2 | 2 |
|
3 | 3 |
use List::MoreUtils qw(any); |
4 |
|
|
4 |
use List::Util qw(first); |
|
5 | 5 |
|
6 | 6 |
use strict; |
7 | 7 |
|
... | ... | |
15 | 15 |
class => 'SL::DB::Unit', |
16 | 16 |
column_map => { base_unit => 'name' }, |
17 | 17 |
}, |
18 |
translations => { |
|
19 |
type => 'one to many', |
|
20 |
class => 'SL::DB::UnitsLanguage', |
|
21 |
column_map => { name => 'unit' }, |
|
22 |
}, |
|
18 | 23 |
); |
19 | 24 |
|
20 | 25 |
__PACKAGE__->meta->initialize; |
... | ... | |
66 | 71 |
return any { $_->id == $self->id } @{ SL::DB::Manager::Unit->time_based_units }; |
67 | 72 |
} |
68 | 73 |
|
74 |
sub get_translation_obj { |
|
75 |
my ($self, $language) = @_; |
|
76 |
|
|
77 |
my $language_id = (ref($language) eq 'SL::DB::Language' ? $language->id : $language) || undef; |
|
78 |
|
|
79 |
return first { $_->language_id => $language_id } @{ $self->translations || [] }; |
|
80 |
} |
|
81 |
|
|
82 |
sub get_translation { |
|
83 |
my ($self, $language, $qty) = @_; |
|
84 |
|
|
85 |
my $translation = $self->name; # fallback, if no translation found |
|
86 |
my $translation_obj = $self->get_translation_obj(language => $language); |
|
87 |
if ($translation_obj) { |
|
88 |
$translation = (($qty // 0) > 1 || ($qty // 0) == 0) ? $translation_obj->localized_plural : $translation_obj->localized; |
|
89 |
} |
|
90 |
return $translation; |
|
91 |
} |
|
92 |
|
|
69 | 93 |
1; |
Auch abrufbar als: Unified diff
S:D:Unit: Methode, um Übersetzung der Einheit zu bekommen