NAME

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

$Id: PlaceholderBabelFish.pm 625 2015-10-05 16:38:04Z 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.002

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 eq '%.3f'
            ? sprintf($attribute, $value)
            : $attribute eq 'accusative'
            ? accusative($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 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 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, $attribute ) = @_;
    return
        $attribute eq 'num.03'
        ? sprintf('%.03f, $value)
        : $attribute eq 'accusative'
        ? 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, 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.