Locale:...:Gettext(3)

πŸ“¦
version 1.036 Β· 2 versions
, in fedora 43

πŸ’– Locale::TextDomain::OO::Plugin::Expand::Gettext πŸ’–

okay so bestie, welcome to the glow-up era of your i18n game πŸ’… This is a User Contributed Perl Documentation page for Locale::TextDomain::OO::Plugin::Expand::Gettext, hanging out in the Locale:...:Gettext(3) corner of the manual universe. Buckle up, she's got extra gettext methods and they're all prefixed with __ ✨

$Id: Gettext.pm 651 2017-05-31 18:10:43Z steffenw $
$HeadURL:
svn:ssh://steffenw@svn.code.sf.net/p/perl-gettext-oo/code/module/trunk/lib/Locale/TextDomain/OO/Plugin/Expand/Gettext.pm
$

🌸 VERSION 🌸

1.027 β€” periodt.


πŸ¦‹ DESCRIPTION πŸ¦‹

This module hands you additional gettext methods for static domain and category handling. That's the whole vibe. 🫢


✨ SYNOPSIS ✨

my $loc = Locale::Text::TextDomain::OO->new(
    plugins => [ qw (
        Expand::Gettext
        ...
    )],
    ...
);

Optional type formatting or grammar stuff? Peep Locale::Utils::PlaceholderNamed for possible methods. πŸ‘€

$loc->expand_gettext->modifier_code($code_ref);

πŸ’… SUBROUTINES/METHODS πŸ’…

πŸŽ€ method expand_gettext

Returns the Locale::Utils::PlaceholderNamed object, so you can set some options on it. Iconic behaviour, no notes.

my $expander_object = $self->expand_gettext;

e.g.

$self->expand_gettext->modifier_code(
    sub {
        my ( $value, $attribute ) = @_;
        if ( $attribute eq 'numf' ) {
            # modify that numeric $value
            # e.g. change 1234.56 to 1.234,56 or 1,234.56
            ...
        }
        elsif ( $attribute eq 'accusative' ) {
            # modify the string with that grammar rule
            # e.g. needed for East-European languages
            # write grammar rules only on msgstr/msgstr_plural[n]
            # and not on msgid
            ...
        }
        ...
        return $value;
    },
);

🌈 translation methods

How do you build the method name? It's giving algorithm bestie:

Use __ and append this with "n", "p" and/or "x" in alphabetic order.

  • __ β€” Special marked for extraction.
  • n β€” Using plural forms.
  • p β€” Context is the first parameter.
  • x β€” Last parameters as hash/hash_ref are for named placeholders.

πŸ’œ method __

Translate only, that's it, that's the whole job.

print $loc->__(
    'Hello World!',
);

πŸ’œ method __x

Expand named placeholders ✨

print $loc->__x(
    'Hello {name}!',
    # hash or hash_ref
    name => 'Steffen',
);

πŸ’œ method __n

Plural, plural, plural 🎢

print $loc->__n(
    'one file read',       # Singular
    'a lot of files read', # Plural
    $file_count,           # number to select the right plural form
);

πŸ’œ method __nx

Plural and expand named placeholders. Both. At once. Main character energy.

print $loc->__nx(
    '{count:num} file read',
    '{count:num} files read',
    $file_count,
    # hash or hash_ref
    count => $file_count,
);

So what is the meaning of "{count:num}" or the alternative "{count :num}"? πŸ€”

That's an attribute. If there's such an attribute like ":num" and the modifier_code is set, the placeholder value will be modified before replacement.

Think about the attribute names, fr fr. Too technical names are able to destroy the translation process by translation office stuff. 😭

For better automatic translation, use the reserved attribute ":num" and tag all numeric placeholders.

You are allowed to set multiple attributes like "{count :num :numf}" β€” the resulting attribute string is then "num :numf". Slay.

πŸ’œ method __p

Context, because words mean different things in different places, iykyk.

print $loc->__p(
    'time', # Context
    'to',
);

print $loc->__p(
    'destination', # Context
    'to',
);

πŸ’œ method __px

Context and expand named placeholders 🫢

print $loc->__px(
    'destination',
    'from {town_from} to {town_to}',
    # hash or hash_ref
    town_from => 'Chemnitz',
    town_to   => 'Erlangen',
);

πŸ’œ method __np

Context and plural. The duo we didn't know we needed.

print $loc->__np(
    'maskulin',
    'Dear friend',
    'Dear friends',
    $friends,
);

πŸ’œ method __npx

Context, plural and expand named placeholders β€” the full combo πŸ†

print $loc->__npx(
    'maskulin',
    'Mr. {name} has {count:num} book.',
    'Mr. {name} has {count:num} books.',
    $book_count,
    # hash or hash_ref
    name  => $name,
    count => $book_count,
);

🧚 Methods to mark the translation for extraction only 🧚

How do you build the method name? Same deal, bestie:

Use N__ and append this with "n", "p" and/or "x" in alphabetic order.

  • __ β€” Special marked for extraction.
  • n β€” Using plural forms.
  • p β€” Context is the first parameter.
  • x β€” Last parameters as hash/hash_ref are for named placeholders.

πŸ’« methods N__, N__x, N__n, N__nx, N__p, N__px, N__np, N__npx

The extractor looks for "__('..." and has no problem with "$loc->N__('...".

That's the whole idea of the N-Methods. Big brain. 🧠

$loc->N__('...');
$loc->N__x('...', ...);
...

πŸ“ EXAMPLE πŸ“

Inside of this distribution is a directory named example. Run this *.pl files. Go off. πŸŽ€


πŸ’€ DIAGNOSTICS πŸ’€

confess


🌟 CONFIGURATION AND ENVIRONMENT 🌟

none


πŸ‘‘ DEPENDENCIES πŸ‘‘

  • Locale::Utils::PlaceholderNamed
  • Moo::Role

πŸ™ˆ INCOMPATIBILITIES πŸ™ˆ

not known


⭐ BUGS AND LIMITATIONS ⭐

none


🌈 SEE ALSO 🌈

Locale::TextDoamin::OO


πŸ’– AUTHOR πŸ’–

Steffen Winkler


Copyright (c) 2009 - 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.


cute little sign-off bestie: this page ships with perl v5.42.0, dated 2025-07-25, and it's filed under Locale:...:Gettext(3). take care of her πŸ’œβœ¨
πŸ“¦ where this page comes from