The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Locale::Maketext::TieHash::quant - Tying method quant to a hash

VERSION

0.06

SYNOPSIS

if you don't use Locale::Maketext::TieHash::L10N

    use strict;
    use warnings;

    use Carp qw(croak);
    use Locale::Maketext::TieHash::quant;
    use MyProgram::L10N;

    my %quant;

    my $lh = MyProgram::L10N->get_handle('de_DE')
        or croak 'What language?';

    # tie and configure
    tie %quant, 'Locale::Maketext::TieHash::quant', (
        L10N       => $lh, # save language handle
        numf_comma => 1,   # set option numf_comma
    );

    # if you use HTML
    # configure 'nbsp_flag', 'auto_nbsp_flag1' and 'auto_nbsp_flag2'
    tied(%quant)->config(
        nbsp_flag       => '~', # set flag to mark whitespaces
        auto_nbsp_flag1 => 1,   # set flag to use 'nbsp_flag' at the singular automatically
        auto_nbsp_flag2 => 1,   # set flag to use 'nbsp_flag' at the plural automatically
        # If you want to test your Script,
        # you set 'nbsp' on a string which you see in the Browser.
        nbsp            => '<span style="color:red">*</span>',
    );

    my $part = 5000.5;
    print <<"EOT";
    @{[ $lh->maketext('Example') ]}
    $quant{
        $part
        . q{ }
        . $lh->maketext('part,parts,no part')
    }
    EOT

if you use Locale::Maketext::TieHash::L10N

    use strict;
    use warnings;

    use Carp qw(croak);
    use Locale::Maketext::TieHash::L10N;
    use Locale::Maketext::TieHash::quant;
    use MyProgram::L10N;

    my %mt;
    {
        my $lh = MyProgram::L10N->get_handle('de_DE')
            or croak 'What language?';
        tie %mt, 'Locale::Maketext::TieHash::L10N', (
            L10N       => $lh,
            numf_comma => 1,
        );
    }

    tie my %quant, 'Locale::Maketext::TieHash::quant';
    tied(%quant)->config( # get back and set language handle and option
        # only if you use HTML
        L10N            => { tied(%mt)->config() }->{L10N},
        nbsp_flag       => '~',
        auto_nbsp_flag1 => 1,
        auto_nbsp_flag2 => 1,
    );

    my $part = 5000.5;
    print <<"EOT";
    $mt{Example}
    $quant{"$part $mt{'part,parts,no part'}"}
    EOT

read configuration

    my %config = tied(%quant)->config();

write configuration

    my %config = tied(%quant)->config(
        numf_comma => 0,
        nbsp_flag  => q{},
    );

EXAMPLE

Inside of this Distribution is a directory named example. Run this *.pl files.

DESCRIPTION

Object methods like quant don't have interpreted into strings. The module ties the method quant to a hash. The object method quant is executed at fetch hash. At long last this is the same, only the notation is shorter.

You can use the module also without Locale::Maketext::TieHash::L10N. Whether this is better for you, have decide you.

SUBROUTINES/METHODS

method TIEHASH

    use Locale::Maketext::TieHash::quant;
    tie my %quant, 'Locale::Maketext::TieHash::quant', %config;

'TIEHASH' ties your hash and set options defaults.

method config

'config' configures the language handle and/or options.

    # configure the language handle
    tied(%quant)->config(L10N => $lh);

    # configure option of language handle
    tied(%quant)->config(numf_comma => 1);
    # the same is:
    $lh->{numf_comma} = 1;

    # only for debugging your HTML response
    tied(%quant)->config(
        nbsp => 'see_position_of_nbsp_in_HTML_response',
    ); # default is '&nbsp;'

    # Set a flag to say:
    #  Substitute the whitespace before this flag and this flag to '&nbsp;'
    #  or your debugging string.
    # The "nbsp_flag" is a string (1 or more characters).
    tied(%quant)->config(nbsp_flag => '~');

    # You get the string 'singular,plural,zero' from any data base.
    # - As if the 'nbsp_flag' in front of 'singular' would stand.
    tied(%quant)->config(auto_nbsp_flag1 => 1);
    # - As if the 'nbsp_flag' in front of 'plural' would stand.
    tied(%quant)->config(auto_nbsp_flag2 => 1);

The method calls croak, if the key of your hash is undef or your key isn't correct and if the value, you set to option 'nbsp', is undef.

'config' accepts all parameters as Hash and gives a Hash back with all attitudes.

method FETCH

'FETCH' is quantifying the given key of your hash and give back the translated string as value.

    # quantifying
    print $quant{"$number singular,plural,zero"};
    # the same is:
    print $lh->quant($number, 'singular', 'plural', 'zero');
    ...
    # Use 'nbsp' and 'nbsp_flag', 'auto_nbsp_flag1' and 'auto_nbsp_flag2' are true.
    print $quant{"$number singular,plural,zero"};
    # the same is:
    my $result = $lh->quant($number, '~' . 'singular', '~' . 'plural', 'zero');
    $result =~ s{[ ] ~}{&nbsp;}xmsg; # But not a global debugging function is available.

The method calls croak, if the method 'quant' of your stored language handle dies.

DIAGNOSTICS

All methods can croak at false parameters.

CONFIGURATION AND ENVIRONMENT

nothing

DEPENDENCIES

Carp

English

Params::Validate

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

not known

SEE ALSO

Tie::Hash

Locale::Maketext

Locale::Maketext::TieHash::L10N

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2004 - 2010, Steffen Winkler <steffenw at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.