The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Locale::Utils::PlaceholderBabelFish - Utils to expand BabelFish palaceholders

$Id: PlaceholderBabelFish.pm 663 2017-07-16 09:59:32Z steffenw $

$HeadURL: svn+ssh://steffenw@svn.code.sf.net/p/perl-gettext-oo/code/Locale-Utils-PlaceholderBabelFish/trunk/lib/Locale/Utils/PlaceholderBabelFish.pm $

VERSION

0.006

SYNOPSIS

    use Locale::Utils::PlaceholderBabelFish;

    my $obj = Locale::Utils::PlaceholderBabelFish->new(
        # optional is_strict switch
        is_strict => 1,
        # optional modifier code
        modifier_code => sub {
            my ( $value, $attribute ) = @_;
            return
                $attribute =~ m{ \b numf \b }xms
                ? format_number($value)
                : $attribute =~ m{ \b html \b }xms
                ? encode_entiaccusative($value)
                : $value;
        },
        # optional plural code
        plural_code => sub { # the default for English
            my $n = shift;
            0 + (
                $n != 1 # en
            );
        },
    );

    $expanded = $obj->expand_babel_fish($text, $count);
    $expanded = $obj->expand_babel_fish($text, $arg_ref);
    $expanded = $obj->expand_babel_fish($text, \%arg_of);

DESCRIPTION

Utils to expand placeholders in BabelFish style.

Placeholders are also extendable with attributes to run the modifier code. That is an extention to BabelFish style.

SUBROUTINES/METHODS

method new

see SYNOPSIS

method is_strict

If is_strict is false: undef will be converted to q{}. If is_strict is true: no replacement.

    $obj->is_strict(1); # boolean true or false;

method default_modifier_code

Implements the html attribute. For plain text messages in HTML the whole message will be escaped. In case of HTML messages the placeholder data have to escaped.

    # class method
    my $modifier_code = Locale::Utils::PlaceholderBabelFish->default_modifier_code;
    # object method
    my $modifier_code = $obj->default_modifier_code;

    # call example
    $value = $modifier_code->($value, $attributes);

method modifier_code, clear_modifier_code

The modifier code handles named attributes to modify the given placeholder value.

If the placeholder name is #{foo:bar} then foo is the placeholder name and bar the attribute name. Space in front of the attribute name is allowed, e.g. #{foo :bar}.

    my $code_ref = sub {
        my ( $value, $attributes ) = @_;
        return
            $attributes =~ m{ \b numf \b }
            ? $value =~ tr{.}{,}
            : $attribute =~ m{ \b accusative \b }xms
            ? accusative($value)
            : $value;
    };
    $obj->modifier_code($code_ref);

To switch off this code - clear them.

    $obj->clear_modifier_code;

method expand_babel_fish

Expands strings containing BabelFish placeholders.

variables and attributes

    #{name}
    #{name :attr_name}

plural with default count or other name for count

    ((Singular|Plural))
    ((Singular|Plural)):other

plural with special count 0

    ((=0 Zero|Singular|Plural))
    ((=0 Zero|Singular|Plural)):other

plural with placeholder

    ((#{count} Singular|#{count} Plural))
    ((#{other} Singular|#{other} Plural)):other

plural with placeholder and attributes

    ((#{count :attr_name} Singular|#{count} Plural))
    ((#{other :attr_name} Singular|#{other} Plural)):other

    $expanded = $obj->expand_babel_fish($babel_fish_text, $count);
    $expanded = $obj->expand_babel_fish($babel_fish_text, count => $count);
    $expanded = $obj->expand_babel_fish($babel_fish_text, { count => $count, key => $value });

JAVASCRIPT

Inside of this distribution is a directory named javascript. For more information see: Locale::TextDomain::OO::JavaScript

This script depends on http://jquery.com/.

EXAMPLE

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

DIAGNOSTICS

none

CONFIGURATION AND ENVIRONMENT

none

DEPENDENCIES

Carp

List::Util

Moo

MooX::StrictConstructor

MooX::Types::MooseLike

Scalar::Util

namespace::autoclean

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

not known

SEE ALSO

https://github.com/nodeca/babelfish

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2015 - 2017, 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.