| 1 | package i18n; | ||
| 2 | $i18n::VERSION = '0.12'; | ||
| 3 | |||
| 4 | use 5.006; | ||
| 5 | use strict; | ||
| 6 | use constant DATA => 0; | ||
| 7 | use constant LINE => 1; | ||
| 8 | use constant PACKAGE => 2; | ||
| 9 | use constant NEGATED => 3; | ||
| 10 | use warnings::register; | ||
| 11 | use overload ( | ||
| 12 | '~' => \&_negate, | ||
| 13 | '.' => \&_concat, | ||
| 14 | '""' => \&_stringify, | ||
| 15 | fallback => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub import { | ||
| 19 | my $class = shift; | ||
| 20 | my $caller = caller; | ||
| 21 | |||
| 22 | local $@; | ||
| 23 | eval { require Locale::Maketext::Simple; 1 } or return; | ||
| 24 | |||
| 25 | overload::constant( | ||
| 26 | q => sub { | ||
| 27 | shift; | ||
| 28 | pop; | ||
| 29 | bless( | ||
| 30 | [ | ||
| 31 | \@_, # DATA | ||
| 32 | ( caller(1) )[2], # LINE | ||
| 33 | $caller, # PACKAGE | ||
| 34 | ], | ||
| 35 | $class | ||
| 36 | ); | ||
| 37 | }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | { | ||
| 41 | no strict 'refs'; | ||
| 42 | no warnings 'redefine'; | ||
| 43 | |||
| 44 | delete ${"$class\::"}{loc}; | ||
| 45 | delete ${"$class\::"}{loc_lang}; | ||
| 46 | |||
| 47 | unshift @_, 'Path' if @_ % 2; | ||
| 48 | Locale::Maketext::Simple->import(@_); | ||
| 49 | |||
| 50 | *{"$caller\::loc"} = $class->can('loc'); | ||
| 51 | *{"$caller\::loc_lang"} = $class->can('loc_lang'); | ||
| 52 | |||
| 53 | *{"$class\::loc"} = \&_loc; | ||
| 54 | *{"$class\::loc_lang"} = \&_loc_lang; | ||
| 55 | } | ||
| 56 | |||
| 57 | @_ = ( warnings => $class ); | ||
| 58 | goto &warnings::import; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub unimport { | ||
| 62 | my $class = shift; | ||
| 63 | overload::remove_constant('q'); | ||
| 64 | |||
| 65 | @_ = ( warnings => $class ); | ||
| 66 | goto &warnings::unimport; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub loc { goto \&_loc } | ||
| 70 | sub loc_lang { goto \&_loc_lang } | ||
| 71 | |||
| 72 | sub _loc { | ||
| 73 | my $class = shift; | ||
| 74 | return $_[0] unless UNIVERSAL::can( $_[0], '_negate' ); | ||
| 75 | goto &_do_loc; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _loc_lang { | ||
| 79 | my $class = shift; | ||
| 80 | my $caller = caller; | ||
| 81 | my $loc_lang = $caller->can('loc_lang') or return; | ||
| 82 | goto &$loc_lang; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _negate { | ||
| 86 | my $class = ref $_[0]; | ||
| 87 | |||
| 88 | return ~_stringify( $_[0] ) unless warnings::enabled($class); | ||
| 89 | |||
| 90 | goto &_do_loc if $_[0][NEGATED]; | ||
| 91 | |||
| 92 | bless( | ||
| 93 | [ | ||
| 94 | [ @{ $_[0][DATA] } ], # DATA | ||
| 95 | $_[0][LINE], # LINE | ||
| 96 | $_[0][PACKAGE], # PACKAGE | ||
| 97 | 1, # NEGATED | ||
| 98 | ], | ||
| 99 | $class | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _concat { | ||
| 104 | my $class = ref $_[0]; | ||
| 105 | my $pkg = $_[0][PACKAGE]; | ||
| 106 | |||
| 107 | @_ = reverse(@_) if pop; | ||
| 108 | return join( '', @_ ) unless warnings::enabled($class); | ||
| 109 | |||
| 110 | my $line = (caller)[2]; | ||
| 111 | my ( $seen, @data ); | ||
| 112 | |||
| 113 | foreach (@_) { | ||
| 114 | ( push( @data, bless( \\$_, "$class\::var" ) ), next ) | ||
| 115 | unless ref($_) | ||
| 116 | and UNIVERSAL::isa( $_, $class ); | ||
| 117 | $seen++; | ||
| 118 | |||
| 119 | ( push( @data, bless( \\$_, "$class\::var" ) ), next ) | ||
| 120 | unless $_->[LINE] == $line and !$_->[NEGATED]; | ||
| 121 | $seen++; | ||
| 122 | |||
| 123 | $pkg = $_->[PACKAGE]; | ||
| 124 | push @data, @{ $_->[DATA] }; | ||
| 125 | } | ||
| 126 | |||
| 127 | return join( '', @data ) if $seen < 2; | ||
| 128 | |||
| 129 | return bless( | ||
| 130 | [ | ||
| 131 | \@data, # DATA | ||
| 132 | $line, # LINE | ||
| 133 | $pkg, # PACKAGE | ||
| 134 | ], | ||
| 135 | $class | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _stringify { | ||
| 140 | ( $_[0][NEGATED] ) | ||
| 141 | ? ~join( '', map { ( ref $_ ) ? "$$$_" : "$_" } @{ $_[0][DATA] } ) | ||
| 142 | : join( '', map { ( ref $_ ) ? "$$$_" : "$_" } @{ $_[0][DATA] } ); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _do_loc { | ||
| 146 | my $class = ref $_[0]; | ||
| 147 | my $pkg = $_[0][PACKAGE]; | ||
| 148 | |||
| 149 | ( $pkg eq caller ) and my $loc = $pkg->can('loc') | ||
| 150 | or return ~"$_[0]"; | ||
| 151 | |||
| 152 | my @vars; | ||
| 153 | my $format = join( | ||
| 154 | '', | ||
| 155 | map { | ||
| 156 | UNIVERSAL::isa( $_, "$class\::var" ) | ||
| 157 | ? do { push( @vars, $$$_ ); "[_" . @vars . "]" } | ||
| 158 | : do { my $str = $_; $str =~ s/(?=[\[\]~])/~/g; $str }; | ||
| 159 | } @{ $_[0][DATA] } | ||
| 160 | ); | ||
| 161 | |||
| 162 | # Defeat constant folding | ||
| 163 | return bless( [ $loc => $format ], 'i18n::string' ) if !@vars; | ||
| 164 | |||
| 165 | @_ = ( $format, @vars ); | ||
| 166 | goto &$loc; | ||
| 167 | } | ||
| 168 | |||
| 169 | package | ||
| 170 | i18n::string; | ||
| 171 | |||
| 172 | use overload ( | ||
| 173 | '""' => \&_stringify, | ||
| 174 | '0+' => \&_stringify, | ||
| 175 | fallback => 1, | ||
| 176 | ); | ||
| 177 | |||
| 178 | sub _stringify { | ||
| 179 | $_[0][0]->( $_[0][1] ); | ||
| 180 | } | ||
| 181 | |||
| 182 | package | ||
| 183 | i18n::var; | ||
| 184 | |||
| 185 | use overload ( | ||
| 186 | '""' => \&_stringify, | ||
| 187 | '0+' => \&_stringify, | ||
| 188 | fallback => 1, | ||
| 189 | ); | ||
| 190 | |||
| 191 | sub _stringify { ${ ${ $_[0] } } } | ||
| 192 | |||
| 193 | 1; | ||
| 194 | |||
| 195 | __END__ |