NAME

Mojolicious::Plugin::Localize::Quantify - Localize Quantifiable Expressions

SYNOPSIS

my $g_counter = 5;

# Get singular and plural expression, depending on number
my $word = $c->quant($g_counter, 'guest', 'guests');

# In templates
%= quant $g_counter, 'guest', 'guests'

DESCRIPTION

Mojolicious::Plugin::Localize::Quantify helps you to get countable expressions of words depending on the number of units (e.g. singular or plural expressions).

METHODS

Mojolicious::Plugin::Localize::Quantify inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

# Mojolicious
$mojo->plugin('Localize::Quantify');

# Mojolicious::Lite
plugin 'Localize::Quantify';

Called when registering the plugin. The plugin is registered by Mojolicious::Plugin::Localize by default.

HELPERS

quant

my $number = 3;

my $was = $c->quant($number, 'was', 'were');
my $tree = $c->quant($number, 'tree', 'trees');
print "There $was $number $tree.";
# There were 3 trees.

my $count = $c->quant($number, 'some', {
  0 => 'no',
  1 => 'one',
  2 => 'both',
  '3..11' => 'a few',
  12 => 'a dozen',
  '>50' => 'many'
});
print "There $was $count $tree.";
# There were a few trees.

# In templates
%= quant $g_counter, 'guest', 'guests'

Return an expression based on a given number.

Expects at least 2 parameters: The number which the lookup is based on and a default dictionary entry (e.g. a singular term). This can be followed by two optional scalar parameters: the first optional parameter is an entry chosen for all numerical values > 1 or < -1, the second parameter is an entry chosen for the zero value.

A final hash reference can refine the lookup, overriding the given default parameters. Supported keys for dictionary lookups are as follows:

Exact Matches
{
  2  => 'both',
  12 => 'dozens'
}
Boundaries
{
  '< 5'  => 'few',
  '> 14' => 'many'
}
Ranges
{
  '100 .. 999'   => 'hundreds',
  '1000 .. 9999' => 'thousands'
}
Even/odd
{
  even => 'robots',
  odd  => 'humans'
}

Exact matches have the highest precedence, followed by ranges and boundaries in arbitrary order. Even and odd values will match before the default values.

DEPENDENCIES

Mojolicious.

AVAILABILITY

https://github.com/Akron/Mojolicious-Plugin-Localize

COPYRIGHT AND LICENSE

Copyright (C) 2014-2016, Nils Diewald.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.