File-Gettext

 view release on metacpan or  search on metacpan

lib/File/Gettext.pm  view on Meta::CPAN

package File::Gettext;

use 5.010001;
use namespace::autoclean;
use version; our $VERSION = qv( sprintf '0.33.%d', q$Rev: 1 $ =~ /\d+/gmx );

use English                    qw( -no_match_vars );
use File::DataClass::Constants qw( EXCEPTION_CLASS FALSE NUL SPC TRUE );
use File::DataClass::Functions qw( is_hashref merge_attributes throw );
use File::DataClass::IO        qw( io );
use File::DataClass::Types     qw( ArrayRef Directory HashRef Str Undef );
use File::Gettext::Constants   qw( LOCALE_DIRS );
use File::Spec::Functions      qw( tmpdir );
use Type::Utils                qw( as coerce declare from enum via );
use Unexpected::Functions      qw( Unspecified );
use Moo;

extends q(File::DataClass::Schema);

# Private functions
my $_build_localedir = sub {
   my $dir = shift; $dir and $dir = io( $dir ) and $dir->is_dir and return $dir;

   for $dir (map { io $_ } @{ LOCALE_DIRS() }) {
      $dir->exists and $dir->is_dir and return $dir;
   }

   return io tmpdir();
};

my $LocaleDir  = declare as Directory;

coerce $LocaleDir,
   from ArrayRef, via { $_build_localedir->( $_ ) },
   from Str,      via { $_build_localedir->( $_ ) },
   from Undef,    via { $_build_localedir->( $_ ) };

my $SourceType = enum 'SourceType' => [ 'mo', 'po' ];

# Public attributes
has 'charset'           => is => 'ro', isa => Str, default => '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 'gettext_catagory'  => is => 'ro', isa => Str, default => 'LC_MESSAGES';

has 'header_key_table'  => is => 'ro', isa => HashRef,
   default              => sub { {
      project_id_version        => [ 0,  'Project-Id-Version'        ],
      report_msgid_bugs_to      => [ 1,  'Report-Msgid-Bugs-To'      ],
      pot_creation_date         => [ 2,  'POT-Creation-Date'         ],
      po_revision_date          => [ 3,  'PO-Revision-Date'          ],
      last_translator           => [ 4,  'Last-Translator'           ],
      language_team             => [ 5,  'Language-Team'             ],
      language                  => [ 6,  'Language'                  ],
      mime_version              => [ 7,  'MIME-Version'              ],
      content_type              => [ 8,  'Content-Type'              ],
      content_transfer_encoding => [ 9,  'Content-Transfer-Encoding' ],
      plural_forms              => [ 10, 'Plural-Forms'              ], } };

has 'localedir'      => is => 'ro', isa => $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  => 'labels',
      }, } };

has '+storage_class' => default => '+File::Gettext::Storage::PO';

has 'source_name'    => is => 'ro', isa => $SourceType,
   default           => 'po', trigger => TRUE;

# Private methods
my $_is_file_or_log_debug = sub {
   my ($self, $path) = @_;

   $path->exists  or ($self->log->debug( 'Path '.$path->pathname.' not found' )
                      and return FALSE);
   $path->is_file or ($self->log->debug( 'Path '.$path->pathname.' not a file' )
                      and return FALSE);

   return TRUE;
};

# Construction
around 'BUILDARGS' => sub {
   my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args );

   my $builder = $attr->{builder} or return $attr;
   my $config  = $builder->can( 'config' ) ? $builder->config : {};
   my $keys    = [ 'gettext_catagory', 'localedir' ];

   merge_attributes $attr, $builder, $keys;
   merge_attributes $attr, $config,  $keys;

   return $attr;
};



( run in 0.529 second using v1.01-cache-2.11-cpan-39bf76dae61 )