| 1 | package Locale::MaybeMaketext; | ||
| 2 | |||
| 3 | 9 | ▶ | use v5.20.0; # minimum of v5.20.0 due to use of signatures. |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use vars; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 9 | ▶ | use autodie qw/:all/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | use feature qw/signatures/; | ||
| 11 | use Carp qw/croak/; | ||
| 12 | use Scalar::Util qw/blessed/; | ||
| 13 | |||
| 14 | use constant { | ||
| 15 | _MAYBE_MAKETEXT_ALREADY_LOADED => 1, # Localizer is already loaded. | ||
| 16 | _MAYBE_MAKETEXT_LOADABLE => -1, # Lcalizer is not currently loaded, but could be loaded. | ||
| 17 | _MAYBE_MAKETEXT_NOT_LOADABLE => 0, # Localizer is not able to be loaded. | ||
| 18 | }; | ||
| 19 | |||
| 20 | # indirect references (such as new Class instead of Class->new) | ||
| 21 | # are discouraged. can only be disabled on v5.32.0 onwards and is disabled by default on v5.36.0+. | ||
| 22 | # https://metacpan.org/dist/perl/view/pod/perlobj.pod#Indirect-Object-Syntax | ||
| 23 | # need to use the old decimal version + (patch level / 1000) version strings here | ||
| 24 | no if $] >= 5.032, q|feature|, qw/indirect/; | ||
| 25 | no warnings qw/experimental::signatures/; | ||
| 26 | |||
| 27 | # ISA is needed to allow us to pick our parent | ||
| 28 | our @ISA = (); ## no critic (ClassHierarchies::ProhibitExplicitISA) | ||
| 29 | |||
| 30 | our $VERSION = '1.233180'; # VERSION: inserted by Dist::Zilla::Plugin::OurPkgVersion | ||
| 31 | |||
| 32 | # Encoding is needed for consistency with Maketext libraries | ||
| 33 | our $Encoding = 'utf-8'; ## no critic (NamingConventions::Capitalization,Variables::ProhibitPackageVars) | ||
| 34 | |||
| 35 | # Which localizer package are we currently using? | ||
| 36 | my $maybe_maketext_has_localizer = undef; | ||
| 37 | |||
| 38 | # What is the reasoning behind selecting that localizer? | ||
| 39 | my ( @maybe_maketext_default_localizers, @maybe_maketext_reasoning ); | ||
| 40 | |||
| 41 | # our default localizers; | ||
| 42 | @maybe_maketext_reasoning = @maybe_maketext_default_localizers = ( | ||
| 43 | 'Cpanel::CPAN::Locale::Maketext::Utils', | ||
| 44 | 'Locale::Maketext::Utils', | ||
| 45 | 'Locale::Maketext', | ||
| 46 | ); | ||
| 47 | |||
| 48 | # List of our known localizers to iterate through. | ||
| 49 | my @maybe_maketext_known_localizers = @maybe_maketext_default_localizers; | ||
| 50 | |||
| 51 | ## Private functions | ||
| 52 | |||
| 53 | # Checks to see if a package is already loaded or is loadable. | ||
| 54 | my $maybe_maketext_check_package_loaded = sub ($package_name) { | ||
| 55 | my $path = ( $package_name =~ tr{:}{\/}rs ) . '.pm'; | ||
| 56 | if ( exists( $INC{$path} ) ) { | ||
| 57 | if ( defined( $INC{$path} ) ) { | ||
| 58 | push @maybe_maketext_reasoning, sprintf( | ||
| 59 | '%s: Already %s', $package_name, | ||
| 60 | ref( $INC{$path} ) | ||
| 61 | ? 'loaded by hook' | ||
| 62 | : sprintf( 'loaded by filesystem from "%s"', $INC{$path} ) | ||
| 63 | ); | ||
| 64 | return _MAYBE_MAKETEXT_ALREADY_LOADED; | ||
| 65 | } | ||
| 66 | |||
| 67 | # if the INC entry exists but is 'undef', that means Perl was unable | ||
| 68 | # to previously load the package for some unknown reason. | ||
| 69 | push @maybe_maketext_reasoning, sprintf( | ||
| 70 | '%s: Unable to set as parent localizer due to previous erroring on load', | ||
| 71 | $package_name, | ||
| 72 | ); | ||
| 73 | return _MAYBE_MAKETEXT_NOT_LOADABLE; | ||
| 74 | } | ||
| 75 | push @maybe_maketext_reasoning, sprintf( | ||
| 76 | '%s: No record of load attempt found', | ||
| 77 | $package_name, | ||
| 78 | ); | ||
| 79 | return _MAYBE_MAKETEXT_LOADABLE; | ||
| 80 | |||
| 81 | }; | ||
| 82 | |||
| 83 | # Try and load a specific localization library. | ||
| 84 | # Will attempt to try to load if if already loaded - use maybe_maketext_check_package_loaded to check! | ||
| 85 | # Returns 1 if correctly loaded, 0 if not loaded (for whatever reason) | ||
| 86 | my $maybe_maketext_try_load = sub ($package_name) { | ||
| 87 | my $path = ( $package_name =~ tr{:}{\/}rs ) . '.pm'; | ||
| 88 | my $loaded = 0; | ||
| 89 | if ( | ||
| 90 | !eval { | ||
| 91 | |||
| 92 | # Convert any warnings encountered during loading | ||
| 93 | # into "dies" to catch "Subroutine redefined at..." and similar messages. | ||
| 94 | local $SIG{__WARN__} = sub { die $_[0] }; ## no critic (ErrorHandling::RequireCarping) | ||
| 95 | # try the load. | ||
| 96 | require $path; | ||
| 97 | if ( defined( $INC{$path} ) ) { | ||
| 98 | |||
| 99 | # ensure it has loaded - and if so, record what loaded. | ||
| 100 | push @maybe_maketext_reasoning, sprintf( | ||
| 101 | '%s: Loaded correctly from %s', | ||
| 102 | $package_name, | ||
| 103 | ref( $INC{$path} ) | ||
| 104 | ? 'loaded by hook' | ||
| 105 | : sprintf( 'loaded by filesystem from "%s"', $INC{$path} ) | ||
| 106 | ); | ||
| 107 | $loaded = 1; | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | push @maybe_maketext_reasoning, sprintf( | ||
| 111 | '%s: Failed to correctly load from %s', | ||
| 112 | $package_name, | ||
| 113 | $path | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | 1; | ||
| 117 | } | ||
| 118 | ) { | ||
| 119 | # reached if any part of the previous code block errors (such as a loading issue). | ||
| 120 | push @maybe_maketext_reasoning, sprintf( | ||
| 121 | '%s: Unable to set as parent localizer due to "%s" when loading from %s', | ||
| 122 | $package_name, | ||
| 123 | $@ =~ tr{\n}{ }rs, | ||
| 124 | $path | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | return $loaded; | ||
| 128 | }; | ||
| 129 | |||
| 130 | # Check we are passed in an appropriate class object when needed. | ||
| 131 | my $maybe_maketext_check_passed_class = sub ( $class, $method ) { | ||
| 132 | if ( !defined($class) ) { | ||
| 133 | croak( sprintf( '%s should not be called without a class', $method ) ); | ||
| 134 | } | ||
| 135 | my $is_blessed = blessed($class); | ||
| 136 | if ( defined($is_blessed) ) { | ||
| 137 | |||
| 138 | # we have an object, let's store its name as a scalar. | ||
| 139 | $class = $is_blessed; | ||
| 140 | } | ||
| 141 | elsif ( ref($class) ne q{} ) { | ||
| 142 | |||
| 143 | # it is a reference to something else. | ||
| 144 | croak( | ||
| 145 | sprintf( '%s should only be called with class objects: provided a reference of %s', $method, ref($class) ) | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | # check to see if it is ourselves being called. | ||
| 150 | if ( $class eq __PACKAGE__ ) { | ||
| 151 | croak( sprintf( '%s should be called on the translation file\'s parent class', $method ) ); | ||
| 152 | } | ||
| 153 | |||
| 154 | # we should have a scalar now. | ||
| 155 | if ( !$class->can($method) ) { | ||
| 156 | croak( sprintf( '%s was provided as a class to %s but it does not support %s', $class, $method, $method ) ); | ||
| 157 | } | ||
| 158 | return 1; | ||
| 159 | }; | ||
| 160 | |||
| 161 | ## Public functions | ||
| 162 | |||
| 163 | # Gets the list of known localizers. | ||
| 164 | sub maybe_maketext_get_localizer_list() { | ||
| 165 | return @maybe_maketext_known_localizers; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Sets the list of known localizers. | ||
| 169 | sub maybe_maketext_set_localizer_list (@localizers) { | ||
| 170 | @maybe_maketext_known_localizers = @localizers; | ||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | # Gets the current localizer if set - if not, tries to load an appropriate one | ||
| 175 | # using the private 'maybe_maketext_try_load' method. | ||
| 176 | sub maybe_maketext_get_localizer() { | ||
| 177 | if ($maybe_maketext_has_localizer) { | ||
| 178 | |||
| 179 | # already exists | ||
| 180 | return $maybe_maketext_has_localizer; | ||
| 181 | } | ||
| 182 | my @to_attempt = (); | ||
| 183 | for my $package_name (@maybe_maketext_known_localizers) { | ||
| 184 | my $is_loaded = $maybe_maketext_check_package_loaded->($package_name); | ||
| 185 | if ( $is_loaded == _MAYBE_MAKETEXT_ALREADY_LOADED ) { | ||
| 186 | $maybe_maketext_has_localizer = $package_name; | ||
| 187 | last; | ||
| 188 | } | ||
| 189 | elsif ( $is_loaded == _MAYBE_MAKETEXT_LOADABLE ) { | ||
| 190 | push @to_attempt, $package_name; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | if ( !$maybe_maketext_has_localizer ) { | ||
| 194 | push @maybe_maketext_reasoning, 'Attempting to load'; | ||
| 195 | for my $package_name (@to_attempt) { | ||
| 196 | if ( $maybe_maketext_try_load->($package_name) ) { | ||
| 197 | $maybe_maketext_has_localizer = $package_name; | ||
| 198 | last; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | if ( !$maybe_maketext_has_localizer ) { | ||
| 203 | croak( "Unable to load localizers: \n - " . join( "\n - ", @maybe_maketext_reasoning ) ); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Needed to allow us to pick our parent | ||
| 207 | push @ISA, $maybe_maketext_has_localizer; ## no critic (ClassHierarchies::ProhibitExplicitISA) | ||
| 208 | return $maybe_maketext_has_localizer; | ||
| 209 | } | ||
| 210 | |||
| 211 | # Reset which localizer we are currently using. | ||
| 212 | sub maybe_maketext_reset () { | ||
| 213 | |||
| 214 | # remove parent/inheritance. | ||
| 215 | if ($maybe_maketext_has_localizer) { | ||
| 216 | ## no critic (ClassHierarchies::ProhibitExplicitISA) | ||
| 217 | # If we inherited from other classes, it would be advisable to | ||
| 218 | # only remove the localizer - using this grep command: | ||
| 219 | #@ISA = grep { !/$maybe_maketext_has_localizer/ } @ISA; | ||
| 220 | # but since we don't, we can just reset the inheritence. | ||
| 221 | @ISA = (); | ||
| 222 | $maybe_maketext_has_localizer = undef; | ||
| 223 | } | ||
| 224 | @maybe_maketext_known_localizers = @maybe_maketext_default_localizers; | ||
| 225 | @maybe_maketext_reasoning = (); | ||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | |||
| 229 | # Get the reasoning for the current localizer. | ||
| 230 | sub maybe_maketext_get_reasoning() { | ||
| 231 | return @maybe_maketext_reasoning; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Get the localizer translation handle (after ensuring we have an appropriate | ||
| 235 | # localizer of course). | ||
| 236 | sub get_handle ( $class, @languages ) { | ||
| 237 | maybe_maketext_get_localizer(); # don't actually care about which localizer we get | ||
| 238 | $maybe_maketext_check_passed_class->( $class, 'get_handle' ); | ||
| 239 | |||
| 240 | my $return = $class->SUPER::get_handle(@languages); | ||
| 241 | return $return; | ||
| 242 | } | ||
| 243 | |||
| 244 | # Dummy method to ensure a localizer is set through get_handle first. | ||
| 245 | sub maketext ( $class, $string, @params ) { | ||
| 246 | if ( !$maybe_maketext_has_localizer ) { | ||
| 247 | croak('maketext called without get_handle'); | ||
| 248 | } | ||
| 249 | if ( !ref($class) ) { | ||
| 250 | croak('maketext must be called as a method'); | ||
| 251 | } | ||
| 252 | $maybe_maketext_check_passed_class->( $class, 'maketext' ); | ||
| 253 | if ( !defined($string) ) { | ||
| 254 | croak('maketext must be passed a scalar string to translate - it was passed an undefined item'); | ||
| 255 | } | ||
| 256 | if ( ref($string) ne q{} ) { | ||
| 257 | croak( | ||
| 258 | sprintf( | ||
| 259 | 'maketext must be passed a scalar string to translate - it was passed a %s reference', ref($string) | ||
| 260 | ) | ||
| 261 | ); | ||
| 262 | } | ||
| 263 | |||
| 264 | return $class->SUPER::maketext( $string, @params ); | ||
| 265 | } | ||
| 266 | |||
| 267 | 1; | ||
| 268 | |||
| 269 | __END__ | ||