lib/Serge/Engine/Plugin/keys_language.pm

filter: Statementstmt 26.1% 17 / 65 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 30.7% 4 / 13 Podpod 0.0% 0 / 9 totaltotal 16.4% 21 / 128 SCAR 52.1
CC
17
Cov
17%
validate_data
56.0
adjust_phases
30.0
generate_raw_key
20.0
CRAP 182.2
« lib/Serge/Command/help.pm ↑ Summary lib/Serge/Mail.pm »
1 package Serge::Engine::Plugin::keys_language;
2 1 ▶use parent Serge::Plugin::Base::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Generate localized resources with unique string key being the translation value
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Serge::Util qw(generate_hash is_flag_set set_flags);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $SEED_WITH_STRING = undef; # default `seed_with_string` value
12 1 our $STRING_FORMAT = '%HASH%'; # default `string_format` value
13 1 our $HINT_FORMAT = '%HASH%'; # default `hint_format` value
14 1 our $LANG = 'keys'; # default `language` value
15  
16 sub name {
17 0 ▶ return 'Language translation provider which generates unique string keys as translation values';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
18 }
19  
20 sub init {
21 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 0 $self->SUPER::init(@_);
24  
25 0 $self->merge_schema({
26 save_translations => 'BOOLEAN',
27 language => 'STRING',
28 seed => 'STRING',
29 seed_with_string => 'BOOLEAN',
30 string_format => 'STRING',
31 hint_format => 'STRING'
32 });
33  
34 0 $self->add({
35 add_hint => \&add_hint,
36 get_translation_pre => \&get_translation,
37 get_translation => \&get_translation,
38 can_generate_ts_file => \&can_process_ts_file,
39 can_process_ts_file => \&can_process_ts_file,
40 });
41 }
42  
43 sub validate_data {
44 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=7 SCAR=40.3
Pod undocumented
45  
46 0 $self->SUPER::validate_data;
47  
48 0 my $d = $self->{data};
49 0 ▶ $self->{language} = exists $d->{language} ? $d->{language} : $LANG;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'language'} ? : 0 0 0
50 0 ▶ $self->{string_format} = exists $d->{string_format} ? $d->{string_format} : $STRING_FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'string_format'} ? : 0 0 0
51 0 ▶ $self->{hint_format} = exists $d->{hint_format} ? $d->{hint_format} : $HINT_FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'hint_format'} ? : 0 0 0
52 0 ▶ $self->{seed_with_string} = exists $d->{seed_with_string} ? $d->{seed_with_string} : $SEED_WITH_STRING;
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'seed_with_string'} ? : 0 0 0
53  
54 0 ▶ if ($self->{string_format} !~ m/%HASH%/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"string_format"} =~ /%HASH%/) 0 0 0
55 0 die "`string_format` parameter value must have a %HASH% macro"
56 }
57  
58 0 ▶ if ($self->{hint_format} !~ m/%HASH%/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"hint_format"} =~ /%HASH%/) 0 0 0
59 0 die "`hint_format` parameter value must have a %HASH% macro"
60 }
61 }
62  
63 sub adjust_phases {
64 0 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: not called CC=5 SCAR=34.0
Pod undocumented
65  
66 0 $self->SUPER::adjust_phases($phases);
67  
68 # always tie to 'can_process_ts_file' phase
69 0 set_flags($phases, 'can_generate_ts_file', 'can_process_ts_file');
70  
71 # this plugin makes sense only when applied to either
72 # get_translation_pre or get_translation phase, but not both
73 0 my $f1 = is_flag_set($phases, 'get_translation_pre');
74 0 my $f2 = is_flag_set($phases, 'get_translation');
75 0 ▶ die "This plugin needs to be attached to either get_translation_pre or get_translation phase" if !$f1 && !$f2;
Branch: 1 branch
BranchTrueFalseTotal
unless $f1 or $f2 0 0 0
Condition0%0/3
$f1 or $f2
l!l&&r!l&&!r
000
MC/DC0%0/2
$f1 or $f2
$f1 $f2
Truth table0%0/3
A or B
A= $f1B= $f2
ABresult
1X1
011
000
76 0 ▶ die "This plugin needs to be attached to either get_translation_pre or get_translation phase, but not both" if $f1 && $f2;
Branch: 1 branch
BranchTrueFalseTotal
if $f1 and $f2 0 0 0
Condition0%0/3
$f1 and $f2
!ll&&!rl&&r
000
MC/DC0%0/2
$f1 and $f2
$f1 $f2
Truth table0%0/3
A and B
A= $f1B= $f2
ABresult
0X0
100
111
77 }
78  
79 # public static method
80 sub is_keys_language {
81 0 ▶ my ($self, $lang) = @_;
Subroutine is_keys_language: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return $lang eq $self->{language};
83 }
84  
85 sub generate_raw_key {
86 0 ▶ my ($self, $namespace, $filepath, $source_key, $string, $context) = @_;
Subroutine generate_raw_key: not called CC=4 SCAR=30.0
Pod undocumented
87  
88 # add initial seed, namespace and filepath as base disambiguation
89 # factors, so that keys in all namespaces / files are different;
90 # having an initial seed allows to control key uniqueness across
91 # multiple Serge databases
92 0 my @a = ($self->{data}->{seed}, $namespace, $filepath);
93  
94 0 ▶ if (defined $source_key && $source_key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_key and $source_key ne '') { } 0 0 0
Condition0%0/3
defined $source_key and $source_key ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $source_key and $source_key ne ""
defined $source_key $source_key ne ""
Truth table0%0/3
A and B
A= defined $source_keyB= $source_key ne ""
ABresult
0X0
100
111
95 # when source key is defined, use it as a string identifier
96 0 push @a, $source_key;
97 0 ▶ if ($self->{seed_with_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seed_with_string"}) 0 0 0
98 # optionally, add the string itself, which ensures that the key
99 # changes if the same source key is reused for a different value
100 0 push @a, $string;
101 }
102 } else {
103 # if no source key is present, use a combination of string and context
104 # as a unique string identifier within a file
105 0 push @a, ($string, $context);
106 }
107  
108 0 return generate_hash(@a);
109 }
110  
111 sub add_hint {
112 0 ▶ my ($self, $phase, $string, $context, $namespace, $filepath, $source_key, $lang, $aref) = @_;
Subroutine add_hint: not called CC=1 SCAR=6.9
Pod undocumented
113  
114 0 my $hash = $self->generate_raw_key($namespace, $filepath, $source_key, $string, $context);
115 0 my $hint = $self->{hint_format};
116 0 $hint =~ s/%HASH%/$hash/sg;
117  
118 0 push @$aref, $hint;
119 }
120  
121 sub get_translation {
122 0 ▶ my ($self, $phase, $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $source_key) = @_;
Subroutine get_translation: not called CC=3 SCAR=24.8
Pod undocumented
123  
124 0 ▶ return () unless $self->is_keys_language($lang);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_keys_language($lang) 0 0 0
125 0 my $hash = $self->generate_raw_key($namespace, $filepath, $source_key, $string, $context);
126 0 my $key = $self->{string_format};
127 0 $key =~ s/%HASH%/$hash/sg;
128  
129 0 ▶ return ($key, undef, undef, $self->{data}->{save_translations}) if $self->is_keys_language($lang);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_keys_language($lang) 0 0 0
130 0 return (); # otherwise, return an empty array
131 }
132  
133 sub can_process_ts_file {
134 0 ▶ my ($self, $phase, $file, $lang) = @_;
Subroutine can_process_ts_file: not called CC=2 SCAR=17.9
Pod undocumented
135  
136 # if this is a keys language, do not generate or process translation files:
137 # keys are not meant to be modified externally
138 0 ▶ return 0 if $self->is_keys_language($lang);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_keys_language($lang) 0 0 0
139  
140 # by default, allow to process any translation files for any given target language
141 0 return 1;
142 }
143  
144 1 1;
« lib/Serge/Command/help.pm ↑ Summary lib/Serge/Mail.pm »