use
version;
our
$VERSION
= qv(
sprintf
'0.7.%d'
,
q$Rev: 321 $
=~ /\d+/gmx );
subtype
'F_DC_Localedir'
, as
'F_DC_Directory'
;
coerce
'F_DC_Localedir'
=> from
'ArrayRef'
=> via { __build_localedir(
$_
) };
coerce
'F_DC_Localedir'
=> from
'Str'
=> via { __build_localedir(
$_
) };
coerce
'F_DC_Localedir'
=> from
'Undef'
=> via { __build_localedir(
$_
) };
has
'catagory_name'
=>
is
=>
'ro'
,
isa
=>
'Str'
,
default
=>
q(LC_MESSAGES)
;
has
'charset'
=>
is
=>
'ro'
,
isa
=>
'Str'
,
default
=>
q(iso-8859-1)
;
has
'default_po_header'
=>
is
=>
'ro'
,
isa
=>
'HashRef'
,
default
=>
sub
{ {
appname
=>
'Your_Application'
,
company
=>
'ExampleCom'
,
email
=>
'<translators@example.com>'
,
lang
=>
'en'
,
team
=>
'Translators'
,
translator
=>
'Athena'
, } };
has
'header_key_table'
=>
is
=>
'ro'
,
isa
=>
'HashRef'
,
default
=>
sub
{ {
project_id_version
=> [ 0,
q(Project-Id-Version)
],
report_msgid_bugs_to
=> [ 1,
q(Report-Msgid-Bugs-To)
],
pot_creation_date
=> [ 2,
q(POT-Creation-Date)
],
po_revision_date
=> [ 3,
q(PO-Revision-Date)
],
last_translator
=> [ 4,
q(Last-Translator)
],
language_team
=> [ 5,
q(Language-Team)
],
language
=> [ 6,
q(Language)
],
mime_version
=> [ 7,
q(MIME-Version)
],
content_type
=> [ 8,
q(Content-Type)
],
content_transfer_encoding
=> [ 9,
q(Content-Transfer-Encoding)
],
plural_forms
=> [ 10,
q(Plural-Forms)
], } };
has
'localedir'
=>
is
=>
'ro'
,
isa
=>
'F_DC_Localedir'
,
coerce
=> TRUE,
default
=> NUL;
has
'+result_source_attributes'
=>
default
=>
sub
{ {
mo
=> {
attributes
=> [
qw(msgid_plural msgstr)
],
defaults
=> {
msgstr
=> [], }, },
po
=> {
attributes
=>
[
qw(translator_comment extracted_comment reference flags
previous msgctxt msgid msgid_plural msgstr)
],
defaults
=> {
'flags'
=> [],
'msgstr'
=> [], },
label_attr
=>
q(labels)
,
}, } };
has
'+storage_class'
=>
default
=>
q(+File::Gettext::Storage::PO)
;
has
'source_name'
=>
is
=>
'ro'
,
isa
=> enum( [
qw(mo po)
] ),
default
=>
q(po)
,
trigger
=> \
&_set_storage_class
;
around
'source'
=>
sub
{
my
(
$next
,
$self
) =
@_
;
return
$self
->
$next
(
$self
->source_name );
};
around
'resultset'
=>
sub
{
my
(
$next
,
$self
) =
@_
;
return
$self
->
$next
(
$self
->source_name );
};
around
'load'
=>
sub
{
my
(
$next
,
$self
,
$lang
,
@names
) =
@_
;
my
@paths
=
grep
{
$self
->_is_file_or_log_debug(
$_
) }
map
{
$self
->_get_path(
$lang
,
$_
) }
@names
;
my
$data
=
$self
->
$next
(
@paths
);
my
$po_header
=
exists
$data
->{po_header}
?
$data
->{po_header}->{msgstr} || {} : {};
my
$plural_func
;
if
(
exists
$po_header
->{plural_forms}) {
my
$code
= SPC.
$po_header
->{plural_forms}.SPC;
$code
=~ s{ ([^_a-zA-Z0-9] | \A) ([_a-z][_A-Za-z0-9]*)
([^_a-zA-Z0-9]) }{$1\$$2$3}gmsx;
$code
= "
sub
{
my
\
$n
=
shift
;
my
(\
$plural
, \
$nplurals
);
$code
;
return
(\
$nplurals
, \
$plural
? \
$plural
: 0); }";
$plural_func
=
eval
$code
;
$EVAL_ERROR
and
$plural_func
=
undef
;
}
$data
->{plural_func} =
$plural_func
||
sub
{ (2,
shift
> 1) };
return
$data
;
};
sub
set_path {
my
(
$self
,
@rest
) =
@_
;
return
$self
->path(
$self
->_get_path(
@rest
) );
}
sub
_get_path {
my
(
$self
,
$lang
,
$file
) =
@_
;
$lang
or
$self
->throw(
'Language not specified'
);
$file
or
$self
->throw(
'Language file path not specified'
);
my
$cn
=
$self
->catagory_name;
my
$extn
=
$self
->storage->extn;
return
$self
->io( [
$self
->localedir,
$lang
,
$cn
,
$file
.
$extn
] );
}
sub
_is_file_or_log_debug {
my
(
$self
,
$path
) =
@_
;
$path
->is_file and
return
TRUE;
$self
->debug and
$self
->
log
->debug(
'Path '
.
$path
->pathname.
' not found'
);
return
FALSE;
}
sub
_set_storage_class {
my
$self
=
shift
;
$self
->source_name eq
q(mo)
and
$self
->storage_class(
q(+File::Gettext::Storage::MO)
);
return
;
}
sub
__build_localedir {
my
$dir
=
shift
;
my
$io
;
$dir
and
$io
= io(
$dir
) and
$io
->is_dir and
return
$io
;
for
$dir
(
map
{ io(
$_
) } @{ DIRECTORIES() }) {
$dir
->is_dir and
return
$dir
;
}
return
io( File::Spec->tmpdir );
}
__PACKAGE__->meta->make_immutable;
no
Moose::Util::TypeConstraints;
no
Moose;
1;