line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
##---------------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
## PO Files Manipulation - ~/lib/Text/PO/MO.pm |
3
|
|
|
|
|
|
|
## Version v0.2.0 |
4
|
|
|
|
|
|
|
## Copyright(c) 2021 DEGUEST Pte. Ltd. |
5
|
|
|
|
|
|
|
## Author: Jacques Deguest <jack@deguest.jp> |
6
|
|
|
|
|
|
|
## Created 2021/06/25 |
7
|
|
|
|
|
|
|
## Modified 2022/07/06 |
8
|
|
|
|
|
|
|
## All rights reserved |
9
|
|
|
|
|
|
|
## |
10
|
|
|
|
|
|
|
## This program is free software; you can redistribute it and/or modify it |
11
|
|
|
|
|
|
|
## under the same terms as Perl itself. |
12
|
|
|
|
|
|
|
##---------------------------------------------------------------------------- |
13
|
|
|
|
|
|
|
package Text::PO::MO; |
14
|
|
|
|
|
|
|
BEGIN |
15
|
|
|
|
|
|
|
{ |
16
|
2
|
|
|
2
|
|
22795271
|
use strict; |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
60
|
|
17
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
18
|
2
|
|
|
2
|
|
20
|
use warnings::register; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
195
|
|
19
|
2
|
|
|
2
|
|
11
|
use parent qw( Module::Generic ); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
16
|
|
20
|
2
|
|
|
2
|
|
150
|
use vars qw( $VERSION @META $DEF_META ); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
115
|
|
21
|
2
|
|
|
2
|
|
19
|
use Encode (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
31
|
|
22
|
2
|
|
|
2
|
|
31
|
use IO::File; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
285
|
|
23
|
2
|
|
|
2
|
|
13
|
use Nice::Try; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
28
|
|
24
|
2
|
|
|
2
|
|
1851266
|
use Text::PO; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
62
|
|
25
|
2
|
|
|
2
|
|
842
|
our $VERSION = 'v0.2.0'; |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
53
|
|
29
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3478
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our @META = @Text::PO::META; |
32
|
|
|
|
|
|
|
our $DEF_META = $Text::PO::DEF_META; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub init |
35
|
|
|
|
|
|
|
{ |
36
|
3
|
|
|
3
|
1
|
53342
|
my $self = shift( @_ ); |
37
|
3
|
|
|
|
|
14
|
my $file; |
38
|
3
|
|
|
|
|
16
|
$file = shift( @_ ); |
39
|
3
|
|
|
|
|
126
|
$self->{auto_decode} = 1; |
40
|
3
|
|
|
|
|
25
|
$self->{default_encoding} = 'utf-8'; |
41
|
3
|
|
|
|
|
19
|
$self->{domain} = ''; |
42
|
3
|
|
|
|
|
23
|
$self->{encoding} = ''; |
43
|
3
|
|
|
|
|
13
|
$self->{file} = $file; |
44
|
3
|
|
|
|
|
15
|
$self->{use_cache} = 1; |
45
|
3
|
|
|
|
|
18
|
$self->{_init_strict_use_sub} = 1; |
46
|
3
|
|
|
|
|
34
|
$self->SUPER::init( @_ ); |
47
|
3
|
|
|
|
|
417
|
$self->{revision} = 0; |
48
|
3
|
|
|
|
|
23
|
$self->{magic} = '0x950412de'; |
49
|
3
|
|
|
|
|
19
|
$self->{_last_modified} = ''; |
50
|
3
|
|
|
|
|
16
|
return( $self ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub as_object |
54
|
|
|
|
|
|
|
{ |
55
|
1
|
|
|
1
|
1
|
702
|
my $self = shift( @_ ); |
56
|
1
|
|
|
|
|
4
|
my( $ref, $order ) = $self->read; |
57
|
1
|
50
|
|
|
|
3
|
return( $self->pass_error ) if( !defined( $ref ) ); |
58
|
|
|
|
|
|
|
# Get the raw meta element |
59
|
1
|
|
|
|
|
3
|
my $raw = $ref->{ '' }; |
60
|
1
|
|
|
|
|
15
|
my $arr = [split( "\\n", $raw )]; |
61
|
1
|
|
|
|
|
9
|
my $po = Text::PO->new( debug => $self->debug, encoding => $self->encoding, domain => $self->domain ); |
62
|
1
|
|
|
|
|
6
|
my $meta = {}; |
63
|
1
|
|
|
|
|
1
|
my $meta_keys = []; |
64
|
1
|
|
|
|
|
3
|
foreach my $s ( @$arr ) |
65
|
|
|
|
|
|
|
{ |
66
|
11
|
100
|
|
|
|
36
|
if( $s =~ /^([^\x00-\x1f\x80-\xff :=]+):[[:blank:]]*(.*?)$/ ) |
67
|
|
|
|
|
|
|
{ |
68
|
1
|
|
|
|
|
9
|
my( $k, $v ) = ( lc( $1 ), $2 ); |
69
|
1
|
|
|
|
|
16
|
$meta->{ $k } = $v; |
70
|
1
|
|
|
|
|
5
|
push( @$meta_keys, $k ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
1
|
|
|
|
|
5
|
my $rv = $po->meta( $meta ); |
74
|
1
|
|
|
|
|
3648527
|
$po->meta_keys( $meta_keys ); |
75
|
1
|
|
|
|
|
61
|
my $e = $po->new_element({ |
76
|
|
|
|
|
|
|
is_meta => 1, |
77
|
|
|
|
|
|
|
msgid => '', |
78
|
|
|
|
|
|
|
msgstr => $arr, |
79
|
|
|
|
|
|
|
}); |
80
|
1
|
|
|
|
|
4
|
push( @{$po->{elements}}, $e ); |
|
1
|
|
|
|
|
6
|
|
81
|
1
|
|
|
|
|
3
|
foreach my $k ( @$order ) |
82
|
|
|
|
|
|
|
{ |
83
|
9
|
100
|
|
|
|
20
|
next if( !length( $k ) ); |
84
|
|
|
|
|
|
|
my $e = $po->new_element({ |
85
|
|
|
|
|
|
|
msgid => $k, |
86
|
8
|
|
|
|
|
50
|
msgstr => $ref->{ $k }, |
87
|
|
|
|
|
|
|
}); |
88
|
8
|
|
|
|
|
20
|
push( @{$po->{elements}}, $e ); |
|
8
|
|
|
|
|
30
|
|
89
|
|
|
|
|
|
|
} |
90
|
1
|
|
|
|
|
9
|
return( $po ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
1
|
1
|
13
|
sub auto_decode { return( shift->_set_get_boolean( 'auto_decode', @_ ) ); } |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub decode |
96
|
|
|
|
|
|
|
{ |
97
|
1
|
|
|
1
|
1
|
3
|
my $self = shift( @_ ); |
98
|
1
|
|
|
|
|
3
|
my $hash = shift( @_ ); |
99
|
1
|
|
33
|
|
|
5
|
my $enc = shift( @_ ) || $self->encoding; |
100
|
1
|
50
|
|
|
|
112
|
return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' ); |
101
|
1
|
50
|
|
|
|
4
|
return( $self->error( "No character encoding was provided to decode the mo file data." ) ) if( !CORE::length( $enc ) ); |
102
|
1
|
50
|
33
|
|
|
3
|
try |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
15
|
|
103
|
1
|
|
|
1
|
|
2
|
{ |
104
|
1
|
|
|
|
|
10
|
foreach my $k ( sort( keys( %$hash ) ) ) |
105
|
|
|
|
|
|
|
{ |
106
|
9
|
|
|
|
|
15
|
my $v = $hash->{ $k }; |
107
|
9
|
|
|
|
|
32
|
my $k2 = Encode::decode( $enc, $k, Encode::FB_CROAK ); |
108
|
9
|
|
|
|
|
484
|
my $v2 = Encode::decode( $enc, $v, Encode::FB_CROAK ); |
109
|
9
|
50
|
|
|
|
270
|
CORE::delete( $hash->{ $k } ) if( CORE::length( $k ) ); |
110
|
9
|
|
|
|
|
23
|
$hash->{ $k2 } = $v2; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
1
|
50
|
50
|
|
|
11
|
catch( $e ) |
|
1
|
0
|
33
|
|
|
8
|
|
|
0
|
0
|
|
|
|
0
|
|
|
1
|
0
|
|
|
|
2
|
|
|
1
|
0
|
|
|
|
2
|
|
|
1
|
0
|
|
|
|
1
|
|
|
1
|
0
|
|
|
|
2
|
|
|
1
|
0
|
|
|
|
5
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
4
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
114
|
0
|
|
|
0
|
|
0
|
{ |
115
|
0
|
|
|
|
|
0
|
return( $self->error( "An error occurred while trying to decode mo data using character encoding \"$enc\": $e" ) ); |
116
|
2
|
0
|
0
|
2
|
|
18
|
} |
|
2
|
0
|
0
|
|
|
3
|
|
|
2
|
0
|
33
|
|
|
2511
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
33
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
1
|
0
|
|
|
|
4
|
|
|
0
|
0
|
|
|
|
0
|
|
|
1
|
0
|
|
|
|
56
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
9
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
5
|
|
117
|
1
|
|
|
|
|
4
|
return( $hash ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
0
|
1
|
0
|
sub default_encoding { return( shift->_set_get_scalar( 'default_encoding', @_ ) ); } |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
|
3
|
1
|
533
|
sub domain { return( shift->_set_get_scalar( 'domain', @_ ) ); } |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub encode |
125
|
|
|
|
|
|
|
{ |
126
|
9
|
|
|
9
|
1
|
20
|
my $self = shift( @_ ); |
127
|
9
|
|
|
|
|
23
|
my $hash = shift( @_ ); |
128
|
9
|
|
33
|
|
|
56
|
my $enc = shift( @_ ) || $self->encoding; |
129
|
9
|
50
|
|
|
|
76
|
return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' ); |
130
|
9
|
50
|
|
|
|
34
|
return( $self->error( "No character encoding was provided to encode data." ) ) if( !CORE::length( $enc ) ); |
131
|
9
|
50
|
33
|
|
|
40
|
try |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
26
|
|
|
9
|
|
|
|
|
105
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
28
|
|
|
9
|
|
|
|
|
61
|
|
|
9
|
|
|
|
|
23
|
|
132
|
9
|
|
|
9
|
|
20
|
{ |
133
|
9
|
|
|
|
|
37
|
foreach my $k ( keys( %$hash ) ) |
134
|
|
|
|
|
|
|
{ |
135
|
36
|
|
|
|
|
46
|
my $v = $hash->{ $k }; |
136
|
36
|
100
|
|
|
|
94
|
if( $self->_is_array( $hash->{ $k } ) ) |
|
|
50
|
|
|
|
|
|
137
|
|
|
|
|
|
|
{ |
138
|
1
|
|
|
|
|
21
|
for( my $i = 0; $i < scalar( @{$hash->{ $k }} ); $i++ ) |
|
1
|
|
|
|
|
15
|
|
139
|
|
|
|
|
|
|
{ |
140
|
0
|
0
|
|
|
|
0
|
$hash->{ $k }->[$i] = Encode::encode( $enc, $hash->{ $k }->[$i], Encode::FB_CROAK ) if( Encode::is_utf8( $hash->{ $k }->[$i] ) ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
elsif( !ref( $hash->{ $k } ) ) |
144
|
|
|
|
|
|
|
{ |
145
|
35
|
100
|
|
|
|
400
|
my $v2 = Encode::is_utf8( $v ) ? Encode::encode( $enc, $v, Encode::FB_CROAK ) : $v; |
146
|
35
|
|
|
|
|
507
|
$hash->{ $k } = $v2; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
9
|
0
|
50
|
|
|
114
|
catch( $e ) |
|
9
|
0
|
33
|
|
|
42
|
|
|
9
|
0
|
|
|
|
31
|
|
|
9
|
0
|
|
|
|
22
|
|
|
9
|
0
|
|
|
|
15
|
|
|
9
|
0
|
|
|
|
15
|
|
|
9
|
0
|
|
|
|
20
|
|
|
9
|
0
|
|
|
|
32
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
32
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
22
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
38
|
|
|
9
|
|
|
|
|
36
|
|
|
9
|
|
|
|
|
23
|
|
|
9
|
|
|
|
|
26
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
151
|
0
|
|
|
0
|
|
0
|
{ |
152
|
0
|
|
|
|
|
0
|
return( $self->error( "An error occurred while trying to encode data using character encoding \"$enc\": $e" ) ); |
153
|
2
|
0
|
0
|
2
|
|
16
|
} |
|
2
|
0
|
0
|
|
|
7
|
|
|
2
|
0
|
33
|
|
|
3861
|
|
|
0
|
0
|
33
|
|
|
0
|
|
|
0
|
0
|
33
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
0
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
9
|
0
|
|
|
|
21
|
|
|
0
|
0
|
|
|
|
0
|
|
|
9
|
0
|
|
|
|
234
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
0
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
50
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
32
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
9
|
|
|
|
|
26
|
|
154
|
9
|
|
|
|
|
30
|
return( $hash ); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
7
|
|
|
7
|
1
|
239
|
sub encoding { return( shift->_set_get_scalar( 'encoding', @_ ) ); } |
158
|
|
|
|
|
|
|
|
159
|
2
|
|
|
2
|
1
|
29
|
sub file { return( shift->_set_get_file( 'file', @_ ) ); } |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub read |
162
|
|
|
|
|
|
|
{ |
163
|
1
|
|
|
1
|
1
|
3
|
my $self = shift( @_ ); |
164
|
1
|
|
|
|
|
4
|
my $file = $self->file; |
165
|
1
|
|
|
|
|
140
|
my $opts = $self->_get_args_as_hash( @_ ); |
166
|
|
|
|
|
|
|
# Caching mechanism |
167
|
1
|
0
|
33
|
|
|
12
|
if( !$self->{use_cache} && |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
168
|
|
|
|
|
|
|
!$opts->{no_cache} && |
169
|
|
|
|
|
|
|
-e( $file ) && |
170
|
|
|
|
|
|
|
ref( $self->{_cache} ) eq 'ARRAY' && |
171
|
|
|
|
|
|
|
$self->{_last_modified} && |
172
|
|
|
|
|
|
|
[CORE::stat( $file )]->[9] > $self->{_last_modified} ) |
173
|
|
|
|
|
|
|
{ |
174
|
0
|
0
|
|
|
|
0
|
return( wantarray() ? @{$self->{_cache}} : $self->{_cache}->[0] ); |
|
0
|
|
|
|
|
0
|
|
175
|
|
|
|
|
|
|
} |
176
|
1
|
50
|
|
|
|
24
|
return( $self->error( "mo file \"$file\" does not exist." ) ) if( !-e( $file ) ); |
177
|
1
|
50
|
|
|
|
17
|
return( $self->error( "mo file \"$file\" is not readable." ) ) if( !-r( $file ) ); |
178
|
1
|
|
50
|
|
|
12
|
my $io = IO::File->new( "<$file" ) || return( $self->error( "Unable to open mo file \"$file\": $!" ) ); |
179
|
1
|
|
|
|
|
115
|
$io->binmode; |
180
|
1
|
|
|
|
|
10
|
my $data; |
181
|
1
|
|
|
|
|
23
|
$io->read( $data, -s( $file ) ); |
182
|
1
|
|
|
|
|
51
|
$io->close; |
183
|
1
|
|
|
|
|
21
|
my $byte_order = substr( $data, 0, 4 ); |
184
|
1
|
|
|
|
|
3
|
my $tmpl; |
185
|
|
|
|
|
|
|
# Little endian |
186
|
1
|
50
|
|
|
|
5
|
if( $byte_order eq "\xde\x12\x04\x95" ) |
|
|
50
|
|
|
|
|
|
187
|
|
|
|
|
|
|
{ |
188
|
0
|
|
|
|
|
0
|
$tmpl = "V"; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
# Big endian |
191
|
|
|
|
|
|
|
elsif( $byte_order eq "\x95\x04\x12\xde" ) |
192
|
|
|
|
|
|
|
{ |
193
|
1
|
|
|
|
|
3
|
$tmpl = "N"; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
else |
196
|
|
|
|
|
|
|
{ |
197
|
0
|
|
|
|
|
0
|
return( $self->error( "Provided file \"$file\" is not a valid mo file." ) ); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
# Check the MO format revision number |
200
|
1
|
|
|
|
|
8
|
my $rev_num = unpack( $tmpl, substr( $data, 4, 4 ) ); |
201
|
|
|
|
|
|
|
# There is only one revision now: revision 0. |
202
|
1
|
50
|
|
|
|
7
|
return if( $rev_num > 0 ); |
203
|
1
|
|
|
|
|
4
|
$self->{revision} = $rev_num; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# Total messages |
206
|
1
|
|
|
|
|
4
|
my $total = unpack( $tmpl, substr( $data, 8, 4 ) ); |
207
|
|
|
|
|
|
|
# Offset to the beginning of the original messages |
208
|
1
|
|
|
|
|
3
|
my $off_msgid = unpack( $tmpl, substr( $data, 12, 4 ) ); |
209
|
|
|
|
|
|
|
# Offset to the beginning of the translated messages |
210
|
1
|
|
|
|
|
3
|
my $off_msgstr = unpack( $tmpl, substr( $data, 16, 4 ) ); |
211
|
1
|
|
|
|
|
1
|
my $hash = {}; |
212
|
1
|
|
|
|
|
3
|
my $order = []; |
213
|
1
|
|
|
|
|
4
|
for( my $i = 0; $i < $total; $i++ ) |
214
|
|
|
|
|
|
|
{ |
215
|
9
|
|
|
|
|
10
|
my( $len, $off, $msgid, $msgstr ); |
216
|
|
|
|
|
|
|
# The first word is the length of the message |
217
|
9
|
|
|
|
|
16
|
$len = unpack( $tmpl, substr( $data, $off_msgid + $i * 8, 4 ) ); |
218
|
|
|
|
|
|
|
# The second word is the offset of the message |
219
|
9
|
|
|
|
|
13
|
$off = unpack( $tmpl, substr( $data, $off_msgid + $i * 8 + 4, 4 ) ); |
220
|
|
|
|
|
|
|
# Original message |
221
|
9
|
|
|
|
|
13
|
$msgid = substr( $data, $off, $len ); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# The first word is the length of the message |
224
|
9
|
|
|
|
|
13
|
$len = unpack( $tmpl, substr( $data, $off_msgstr + $i * 8, 4 ) ); |
225
|
|
|
|
|
|
|
# The second word is the offset of the message |
226
|
9
|
|
|
|
|
14
|
$off = unpack( $tmpl, substr( $data, $off_msgstr + $i * 8 + 4, 4 ) ); |
227
|
|
|
|
|
|
|
# Translated message |
228
|
9
|
|
|
|
|
12
|
$msgstr = substr( $data, $off, $len ); |
229
|
|
|
|
|
|
|
|
230
|
9
|
|
|
|
|
17
|
$hash->{ $msgid } = $msgstr; |
231
|
9
|
|
|
|
|
21
|
push( @$order, $msgid ); |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
1
|
50
|
33
|
|
|
4
|
if( $self->auto_decode || $opts->{auto_decode} ) |
235
|
|
|
|
|
|
|
{ |
236
|
1
|
50
|
|
|
|
210
|
unless( my $enc = $self->encoding ) |
237
|
|
|
|
|
|
|
{ |
238
|
|
|
|
|
|
|
# Find the encoding of that MO file |
239
|
1
|
50
|
|
|
|
131
|
if( $hash->{ '' } =~ /Content-Type:[[:blank:]\h]*text\/plain;[[:blank:]\h]*charset[[:blank:]\h]*=[[:blank:]\h]*(?<quote>["'])?(?<encoding>[\w\-]+)\g{quote}?/is ) |
240
|
|
|
|
|
|
|
{ |
241
|
1
|
|
|
|
|
13
|
$enc = $+{encoding}; |
242
|
1
|
|
|
|
|
5
|
$self->encoding( $enc ); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
# Default to US-ASCII |
245
|
|
|
|
|
|
|
else |
246
|
|
|
|
|
|
|
{ |
247
|
0
|
|
0
|
|
|
0
|
$enc = $self->default_encoding || $opts->{default_encoding}; |
248
|
|
|
|
|
|
|
} |
249
|
1
|
|
|
|
|
124
|
$self->encoding( $enc ); |
250
|
|
|
|
|
|
|
} |
251
|
1
|
|
|
|
|
113
|
$self->decode( $hash ); |
252
|
|
|
|
|
|
|
} |
253
|
1
|
|
|
|
|
22
|
$self->{_last_modified} = [CORE::stat( $file )]->[9]; |
254
|
1
|
|
|
|
|
7
|
$self->{_cache} = [ $hash, $order ]; |
255
|
1
|
50
|
|
|
|
10
|
return( wantarray() ? ( $hash, $order ) : $hash ); |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub reset |
259
|
|
|
|
|
|
|
{ |
260
|
0
|
|
|
0
|
1
|
0
|
my $self = shift( @_ ); |
261
|
0
|
|
|
|
|
0
|
$self->{_cache} = []; |
262
|
0
|
|
|
|
|
0
|
$self->{_last_modified} = ''; |
263
|
0
|
|
|
|
|
0
|
return( $self ); |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
0
|
|
|
0
|
1
|
0
|
sub revision { return( shift->_set_get_scalar( 'revision', @_ ) ); } |
267
|
|
|
|
|
|
|
|
268
|
0
|
|
|
0
|
1
|
0
|
sub use_cache { return( shift->_set_get_boolean( 'use_cache', @_ ) ); } |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub write |
271
|
|
|
|
|
|
|
{ |
272
|
1
|
|
|
1
|
1
|
48
|
my $self = shift( @_ ); |
273
|
1
|
|
|
|
|
26
|
my $po = shift( @_ ); |
274
|
1
|
|
|
|
|
18
|
my $opts = $self->_get_args_as_hash( @_ ); |
275
|
1
|
50
|
33
|
|
|
47
|
return( $self->error( "I was expecting a Text::PO object, and instead got '$po'." ) ) if( !$self->_is_object( $po ) || !$po->isa( 'Text::PO' ) ); |
276
|
1
|
|
|
|
|
59
|
my $ref = {}; |
277
|
1
|
|
|
|
|
18
|
my $keys = []; |
278
|
1
|
|
50
|
|
|
39
|
$opts->{encoding} //= ''; |
279
|
1
|
|
50
|
|
|
117
|
my $enc = $opts->{encoding} || $self->encoding || $self->default_encoding || 'utf-8'; |
280
|
|
|
|
|
|
|
my $add = sub |
281
|
|
|
|
|
|
|
{ |
282
|
9
|
|
|
9
|
|
22
|
my $this = shift( @_ ); |
283
|
|
|
|
|
|
|
$self->encode( $this => $enc ) || do |
284
|
9
|
50
|
|
|
|
55
|
{ |
285
|
0
|
0
|
|
|
|
0
|
warnings::warn( "An error occurred trying to encode value for key '${this}': ", $self->error, "\n" ) if( warnings::enabled() ); |
286
|
|
|
|
|
|
|
}; |
287
|
9
|
|
|
|
|
14
|
my $msgstr; |
288
|
9
|
50
|
|
|
|
20
|
if( $this->{msgid_plural} ) |
289
|
|
|
|
|
|
|
{ |
290
|
0
|
|
|
|
|
0
|
my $res = []; |
291
|
0
|
|
|
|
|
0
|
my $multi = $this->{msgstr}; |
292
|
0
|
|
|
|
|
0
|
for( my $i = 0; $i < scalar( @$multi ); $i++ ) |
293
|
|
|
|
|
|
|
{ |
294
|
0
|
|
|
|
|
0
|
push( @$res, join( null(), @{$multi->[$i]} ) ); |
|
0
|
|
|
|
|
0
|
|
295
|
|
|
|
|
|
|
} |
296
|
0
|
|
|
|
|
0
|
$msgstr = join( null(), @$res ); |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
else |
299
|
|
|
|
|
|
|
{ |
300
|
9
|
100
|
|
|
|
30
|
$msgstr = $self->_is_array( $this->{msgstr} ) ? join( null(), @{$this->{msgstr}} ) : $this->{msgstr}; |
|
1
|
|
|
|
|
9
|
|
301
|
|
|
|
|
|
|
} |
302
|
9
|
100
|
|
|
|
77
|
return if( !length( $msgstr ) ); |
303
|
8
|
|
|
|
|
20
|
my $ctx = ''; |
304
|
8
|
|
|
|
|
16
|
my $plural = ''; |
305
|
8
|
50
|
|
|
|
23
|
if( $this->{context} ) |
306
|
|
|
|
|
|
|
{ |
307
|
0
|
|
|
|
|
0
|
$ctx = $this->{context} . eot(); |
308
|
|
|
|
|
|
|
} |
309
|
8
|
50
|
|
|
|
26
|
if( $this->{msgid_plural} ) |
310
|
|
|
|
|
|
|
{ |
311
|
0
|
|
|
|
|
0
|
$plural = null() . $this->{msgid_plural}; |
312
|
|
|
|
|
|
|
} |
313
|
8
|
|
|
|
|
71
|
$ref->{ $ctx . $this->{msgid} . $plural } = $msgstr; |
314
|
8
|
|
|
|
|
74
|
push( @$keys, $ctx . $this->{msgid} . $plural ); |
315
|
1
|
|
|
|
|
207
|
}; |
316
|
|
|
|
|
|
|
|
317
|
1
|
|
|
|
|
32
|
my $elems = $po->elements; |
318
|
1
|
|
|
|
|
259
|
my $metaKeys = [@Text::PO::META]; |
319
|
1
|
|
|
|
|
33
|
my $metas = []; |
320
|
1
|
|
|
|
|
25
|
my $meta = $po->meta; |
321
|
1
|
50
|
|
|
|
214
|
if( scalar( @$metaKeys ) ) |
322
|
|
|
|
|
|
|
{ |
323
|
1
|
|
|
|
|
16
|
foreach my $k ( @$metaKeys ) |
324
|
|
|
|
|
|
|
{ |
325
|
11
|
|
|
|
|
187
|
my $k2 = lc( $k ); |
326
|
11
|
|
|
|
|
23
|
$k2 =~ tr/-/_/; |
327
|
11
|
50
|
|
|
|
47
|
next if( !CORE::exists( $meta->{ $k2 } ) ); |
328
|
0
|
|
|
|
|
0
|
my $v2 = $po->meta( $k ); |
329
|
0
|
|
|
|
|
0
|
push( @$metas, sprintf( "\"%s: %s\\n\"\n", $po->normalise_meta( $k ), $v2 ) ); |
330
|
|
|
|
|
|
|
} |
331
|
1
|
|
|
|
|
225
|
$add->({ |
332
|
|
|
|
|
|
|
context => '', |
333
|
|
|
|
|
|
|
msgid => '', |
334
|
|
|
|
|
|
|
msgid_plural => '', |
335
|
|
|
|
|
|
|
msgstr => $metas, |
336
|
|
|
|
|
|
|
}); |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
1
|
|
|
|
|
17
|
foreach my $e ( @$elems ) |
340
|
|
|
|
|
|
|
{ |
341
|
9
|
100
|
|
|
|
240
|
next if( $e->is_meta ); |
342
|
8
|
|
|
|
|
1312
|
$add->({ |
343
|
|
|
|
|
|
|
context => $e->context, |
344
|
|
|
|
|
|
|
msgid => $e->msgid, |
345
|
|
|
|
|
|
|
msgid_plural => $e->msgid_plural, |
346
|
|
|
|
|
|
|
msgstr => $e->msgstr, |
347
|
|
|
|
|
|
|
}); |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
1
|
|
|
|
|
13
|
my $cnt = scalar( keys( %$ref ) ); |
351
|
1
|
|
|
|
|
29
|
my $mem = 28 + ( $cnt * 16 ); |
352
|
1
|
|
|
|
|
16
|
my $l10n = [map( $ref->{ $_ }, @$keys )]; |
353
|
|
|
|
|
|
|
|
354
|
1
|
|
|
|
|
3
|
my $fh; |
355
|
|
|
|
|
|
|
my $file = ( CORE::exists( $opts->{file} ) && length( $opts->{file} ) ) |
356
|
|
|
|
|
|
|
? $opts->{file} |
357
|
1
|
50
|
33
|
|
|
33
|
: $self->file; |
358
|
1
|
50
|
|
|
|
246
|
if( $file eq '-' ) |
359
|
|
|
|
|
|
|
{ |
360
|
0
|
|
|
|
|
0
|
$fh = IO::File->new; |
361
|
0
|
|
|
|
|
0
|
$fh->fdopen( fileno( STDOUT ), 'w' ); |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
else |
364
|
|
|
|
|
|
|
{ |
365
|
1
|
50
|
|
|
|
12
|
my $mode = length( $opts->{mode} ) ? $opts->{mode} : '>'; |
366
|
1
|
|
50
|
|
|
33
|
$fh = IO::File->new( $file, $mode ) || |
367
|
|
|
|
|
|
|
return( $self->error( "Unable to open file \"$file\" in write mode: $!" ) ); |
368
|
|
|
|
|
|
|
} |
369
|
1
|
|
|
|
|
409
|
$fh->binmode; |
370
|
1
|
|
|
|
|
44
|
$fh->autoflush(1); |
371
|
1
|
|
|
|
|
183
|
$fh->print( from_hex( $self->{magic} ) ); |
372
|
1
|
|
|
|
|
74
|
$fh->print( character( $self->{revision} ) ); |
373
|
1
|
|
|
|
|
26
|
$fh->print( character( $cnt ) ); |
374
|
1
|
|
|
|
|
28
|
$fh->print( character(28) ); |
375
|
1
|
|
|
|
|
30
|
$fh->print( character( 28 + ( $cnt * 8 ) ) ); |
376
|
1
|
|
|
|
|
24
|
$fh->print( character(0) ); |
377
|
1
|
|
|
|
|
37
|
$fh->print( character(0) ); |
378
|
1
|
|
|
|
|
30
|
foreach my $k ( @$keys ) |
379
|
|
|
|
|
|
|
{ |
380
|
8
|
|
|
|
|
17
|
my $len = length( $k ); |
381
|
8
|
|
|
|
|
23
|
$fh->print( character( $len ) ); |
382
|
8
|
|
|
|
|
146
|
$fh->print( character( $mem ) ); |
383
|
8
|
|
|
|
|
143
|
$mem += $len + 1; |
384
|
|
|
|
|
|
|
} |
385
|
1
|
|
|
|
|
19
|
foreach my $v ( @$l10n ) |
386
|
|
|
|
|
|
|
{ |
387
|
8
|
|
|
|
|
17
|
my $len = length( $v ); |
388
|
8
|
|
|
|
|
33
|
$fh->print( character( $len ) ); |
389
|
8
|
|
|
|
|
152
|
$fh->print( character( $mem ) ); |
390
|
8
|
|
|
|
|
140
|
$mem += $len + 1; |
391
|
|
|
|
|
|
|
} |
392
|
1
|
|
|
|
|
15
|
foreach my $k ( @$keys ) |
393
|
|
|
|
|
|
|
{ |
394
|
8
|
|
|
|
|
132
|
$fh->print( null_terminate( $k ) ); |
395
|
|
|
|
|
|
|
} |
396
|
1
|
|
|
|
|
24
|
foreach my $v ( @$l10n ) |
397
|
|
|
|
|
|
|
{ |
398
|
8
|
|
|
|
|
126
|
$fh->print( null_terminate( $v ) ); |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
|
401
|
1
|
0
|
33
|
|
|
45
|
$fh->close unless( CORE::exists( $opts->{file} ) && defined( $opts->{file} ) && $opts->{file} eq '-' ); |
|
|
|
33
|
|
|
|
|
402
|
1
|
|
|
|
|
154
|
return( $self ); |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# NOTE: helper functions |
406
|
|
|
|
|
|
|
# Credits to Ryan Niebur |
407
|
|
|
|
|
|
|
sub character |
408
|
|
|
|
|
|
|
{ |
409
|
39
|
|
|
39
|
0
|
65
|
return( map{ pack( "N*", $_ ) } @_ ); |
|
39
|
|
|
|
|
249
|
|
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
sub eot |
413
|
|
|
|
|
|
|
{ |
414
|
0
|
|
|
0
|
0
|
0
|
return( chr(4) ); |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub from_character |
418
|
|
|
|
|
|
|
{ |
419
|
0
|
|
|
0
|
0
|
0
|
return( character( _from_character( @_ ) ) ); |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
sub from_hex |
423
|
|
|
|
|
|
|
{ |
424
|
1
|
|
|
1
|
0
|
14
|
return( character( _from_hex( @_ ) ) ); |
425
|
|
|
|
|
|
|
} |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
sub from_string |
428
|
|
|
|
|
|
|
{ |
429
|
0
|
|
|
0
|
0
|
0
|
return( join_string( from_character( _from_string( @_ ) ) ) ); |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
sub join_string |
433
|
|
|
|
|
|
|
{ |
434
|
0
|
|
|
0
|
0
|
0
|
return( join( '', @_ ) ); |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
sub null |
438
|
|
|
|
|
|
|
{ |
439
|
1
|
|
|
1
|
0
|
41
|
return( null_terminate( '' ) ); |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
sub null_terminate |
443
|
|
|
|
|
|
|
{ |
444
|
17
|
|
|
17
|
0
|
106
|
return( pack( "Z*", shift( @_ ) ) ); |
445
|
|
|
|
|
|
|
} |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
sub number_to_s |
448
|
|
|
|
|
|
|
{ |
449
|
0
|
|
|
0
|
0
|
0
|
return( sprintf( "%d", shift( @_ ) ) ); |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
sub _from_character |
453
|
|
|
|
|
|
|
{ |
454
|
0
|
|
|
0
|
|
0
|
return( map( ord( $_ ), @_ ) ); |
455
|
|
|
|
|
|
|
} |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
sub _from_hex |
458
|
|
|
|
|
|
|
{ |
459
|
1
|
|
|
1
|
|
17
|
return( map( hex( $_ ), @_ ) ); |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
sub _from_string |
463
|
|
|
|
|
|
|
{ |
464
|
0
|
|
|
0
|
|
|
return( split( //, join( '', @_ ) ) ); |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
1; |
468
|
|
|
|
|
|
|
# NOTE: POD |
469
|
|
|
|
|
|
|
__END__ |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
=head1 NAME |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
Text::PO::MO - Machine Object File Read, Write |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
=head1 SYNOPSIS |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
use Text::PO::MO; |
478
|
|
|
|
|
|
|
my $mo = Text::PO::MO->new( '/home/joe/locale/com.example.mo', |
479
|
|
|
|
|
|
|
{ |
480
|
|
|
|
|
|
|
auto_decode => 1, |
481
|
|
|
|
|
|
|
encoding => 'utf-8', |
482
|
|
|
|
|
|
|
default_encoding => 'utf-8', |
483
|
|
|
|
|
|
|
}); |
484
|
|
|
|
|
|
|
my $mo = Text::PO::MO->new( |
485
|
|
|
|
|
|
|
file => '/home/joe/locale/com.example.mo', |
486
|
|
|
|
|
|
|
auto_decode => 1, |
487
|
|
|
|
|
|
|
encoding => 'utf-8', |
488
|
|
|
|
|
|
|
default_encoding => 'utf-8', |
489
|
|
|
|
|
|
|
); |
490
|
|
|
|
|
|
|
my $hash = $mo->read; |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=head1 VERSION |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
v0.2.0 |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
=head1 DESCRIPTION |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
This is the class for read from and writing to GNU C<.mo> (machine object) files. |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=head2 new |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
Create a new Text::PO::MO object. |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
It accepts the following options: |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
=over 4 |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=item I<auto_decode> |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
Takes a boolean value and enables or disables auto decoding of data. |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
=item I<default_encoding> |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
Sets the default encoding. This is used when I<auto_decode> is enabled. |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=item I<encoding> |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Sets the value of the encoding to use when I<auto_decode> is enabled. |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=item I<file> |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
Sets or gets the C<.mo> file to read. |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=item I<use_cache> |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Takes a boolean value. If true, this will cache the data read by L</read> |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=back |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
=head1 METHODS |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
=head2 as_object |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
Returns the data read from the machine object file as a L<Text::PO> object. |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=head2 auto_decode |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
Takes a boolean value and enables or disables auto decode of data read from C<.mo> file. |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
This is used in L</read> |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
=head2 decode |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
Provided with an hash reference of key-value pairs and a string representing an encoding and this will decode all its keys and values. |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
It returns the hash reference, although being a reference, this is not necessary. |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=head2 default_encoding |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
Sets the default encoding to revert to if no encoding is set with L</encoding> and L</auto_decode> is enabled. |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
Otherwise, L</read> will attempt to find out the encoding used by looking at the meta information C<Content-type> |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
=head2 domain |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
Sets or gets the po file domain, such as C<com.example.api> |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
=head2 encoding |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
Sets or gets the encoding to use for decoding the data read from the C<.mo> file. |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
=head2 file |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
Sets or gets the gnu C<.mo> file to be read from or written to. |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
=head2 read |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
Provided with a file path to a gnu C<.mo> file and this returns an hash reference of key-value pairs corresponding to the msgid to msgstr or original text to localised text. |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
Note that there is one blank key corresponding to the meta informations. |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
It takes the following optional parameters: |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
=over 4 |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
=item I<auto_decode> |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
Boolean value. If true, the data will be automatically decoded using either the character encoding specified with L</encoding> or the one found in the C<Content-type> field in the file meta information. |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=item I<default_encoding> |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
The default encoding to use if no encoding was set using L</encoding> and none could be found in the C<.mo> file meta information. |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
=item I<no_cache> |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
Boolean value. If true, this will ignore any cached data and re-read the C<.mo> file. |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
=back |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
If caching is enabled with L</use_cache>, then L</read> will return the cache instead of actually reading the C<.mo> unless the last modification time has changed and increased. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
=head2 reset |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
Resets the cached data. This will have the effect of reading the C<.mo> file next time L</read> is called. |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
Returns the current object. |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
=head2 revision |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
Sets or gets the revision number. This should not be changed, or you might break things. |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
It defaults to 0 |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
=head2 use_cache |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
Takes a boolean value. |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
If true, this will enable caching based on the C<.mo> file last modification timestamp. |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
Default to true. |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
=head2 write |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
Provided with a L<Text::PO> object and this will write the C<.mo> file. |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
It takes an hash reference of parameters: |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
=over 4 |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
=item I<file> |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
The output file to write the data to. |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
This should be a file path, or C<-> if you want to write to STDOUT. |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=back |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
=head1 AUTHOR |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
=head1 SEE ALSO |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
L<Text::PO>, L<Text::PO::Element> |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
L<https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html> |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
L<http://www.gnu.org/software/gettext/manual/html_node/MO-Files.html#MO-Files> |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
Copyright (c) 2020-2021 DEGUEST Pte. Ltd. |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself. |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
=cut |