| 1 | package Valiant::Translation; | ||||||||||
| 2 | |||||||||||
| 3 | 45 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 45 | ▶ | use Text::Autoformat 'autoformat'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 45 | ▶ | use Valiant::Util 'debug'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 45 | with 'Valiant::Naming'; | |||||||||
| 8 | |||||||||||
| 9 | 316 | ▶ | sub i18n_class { 'Valiant::I18N' } | ||||||||
|
Subroutine
i18n_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 10 | |||||||||||
| 11 | has 'i18n' => ( | ||||||||||
| 12 | is => 'ro', | ||||||||||
| 13 | required => 1, | ||||||||||
| 14 | lazy => 1, | ||||||||||
| 15 | 316 | ▶ | default => sub { Module::Runtime::use_module(shift->i18n_class) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 45 | ); | |||||||||
| 17 | |||||||||||
| 18 | 1798 | ▶ | sub i18n_scope { 'valiant' } | ||||||||
|
Subroutine
i18n_scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | |||||||||||
| 20 | sub human_attribute_name { | ||||||||||
| 21 | 750 | ▶ | my ($self, $attribute, $options) = @_; | ||||||||
|
Subroutine
human_attribute_name: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||
| 22 | 750 | ▶ | return undef unless defined($attribute); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 750 | debug 1, "Begin building human name for attribute '$attribute'"; | |||||||||
| 24 | |||||||||||
| 25 | # TODO I think we need to clean $option here so I don't need to manually | ||||||||||
| 26 | # set count=>1 as I do below. | ||||||||||
| 27 | # | ||||||||||
| 28 | |||||||||||
| 29 | 750 | my @defaults = (); | |||||||||
| 30 | 750 | my $i18n_scope = $self->i18n_scope; | |||||||||
| 31 | 750 | my @parts = split '.', $attribute; | |||||||||
| 32 | 750 | my $attribute_name = pop @parts; | |||||||||
| 33 | 750 | ▶ | my $namespace = join '/', @parts if @parts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 750 | my $attributes_scope = "${i18n_scope}.attributes"; | |||||||||
| 35 | |||||||||||
| 36 | 750 | ▶ | if($self->can('i18n_lookup')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 749 | debug 2, "Building defaults for attributes '$attribute'"; | |||||||||
| 38 | 749 | ▶ | if($namespace) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | @defaults = map { | ||||||||||
| 40 | 0 | my $class = $_; | |||||||||
| 41 | 0 | "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.${attribute}" | |||||||||
| 42 | 0 | } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup; | |||||||||
| 43 | } else { | ||||||||||
| 44 | @defaults = map { | ||||||||||
| 45 | 1149 | my $class = $_; | |||||||||
| 46 | 1149 | "${attributes_scope}.${\$class->model_name->i18n_key}.${attribute}" | |||||||||
| 47 | 749 | } grep { $_->model_name->can('i18n_key') } $self->i18n_lookup; | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 750 | @defaults = map { $self->i18n->make_tag($_) } (@defaults, "attributes.${attribute}"); | |||||||||
| 52 | |||||||||||
| 53 | # Not sure if this should move up above the preceeding map... | ||||||||||
| 54 | |||||||||||
| 55 | 750 | ▶ | if(exists $options->{default}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 370 | my $default = delete $options->{default}; | |||||||||
| 57 | 370 | ▶ | my @default = ref($default) ? @$default : ($default); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 370 | push @defaults, @default; | |||||||||
| 59 | } | ||||||||||
| 60 | # The final default is just our best attempt to make a name out of the actual | ||||||||||
| 61 | # attribute name. This is passed as a plain string so we don't actually try | ||||||||||
| 62 | # to localize it. | ||||||||||
| 63 | 750 | push @defaults, do { | |||||||||
| 64 | 750 | my $human_attr = $attribute; | |||||||||
| 65 | 750 | $human_attr =~s/_id$//; # remove trailing _id | |||||||||
| 66 | 750 | $human_attr =~s/_/ /g; | |||||||||
| 67 | 750 | $human_attr = autoformat($human_attr, +{case=>'title'}); | |||||||||
| 68 | 750 | $human_attr =~s/[\n]//g; # Is this a bug in Text::Autoformat??? | |||||||||
| 69 | 750 | $human_attr; | |||||||||
| 70 | }; | ||||||||||
| 71 | |||||||||||
| 72 | 750 | my $key = shift @defaults; | |||||||||
| 73 | 750 | $options->{default} = \@defaults; | |||||||||
| 74 | 750 | ▶ | $options->{count} = 1 unless exists $options->{count}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 750 | ▶ | return my $localized = $self->i18n->translate($key, %{$options||+{}}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 45 | 1; | |||||||||