Steffen Winkler > Locale-TextDomain-OO > Locale::TextDomain::OO



Annotate this POD


Open  1
View/Report Bugs
Module Version: 1.024   Source  


Locale::TextDomain::OO - Perl OO Interface to Uniforum Message Translation

$Id: 617 2015-08-22 05:39:27Z steffenw $

$HeadURL: svn+ssh:// $



Starting with version 1.000 the interface has changed.


This module provides a high-level interface to Perl message translation.

Why a new module?

This module is similar to Locale::TextDomain and Locale::Maketext.

This module is not using/changing any system locale like Locale::TextDomain.

This module has no magic in how to get the language like Locale::Maketext. You decide what you need.

There are some plugins, so it is possible to use the Maketext and/or Gettext style.

Locale::TextDomain::OO has a flexible object oriented interface based on Moo.

Creating the Lexicon and translating are two split things. So it is possible to create the lexicon during the initialization phase. The connection between both is the singleton mechanism of the lexicon module.

Plugin for the web framework?

This module was first used in a Catalyst based web project.

It was great to call everything on the Catalyst context object. It was great until we used module in web and shell scripts together and called the translation methods there.

What we have done was removing the Catalyst Maketext plugin. We are using a role in the Catalyst application and so we can call all the translation methods on the Catalyst context object like before.

For the shell scripts we have a role for this Cmd's to call all the methods on $self.

In that roles we delegate a bunch of methods to Locale::TextDomain::OO. We also added our project specific methods there.

Write you own role. There is no need for a plugin for any framework.

Who is setting the languages?

Not this module.

In a web application are lots of ideas to do that. But never we use the server settings, never.

The HTTP request contains that information. Extend this with super and panic languages. Normally you support only a bunch of world languages, so reduce. This module will find the first match.

Or you store and get the language in the session.

Or your application has a language selection menu.

How to extract the project files?

Use module Locale::TextDomain::OO::Extract. This is a base class for all source scanner to create po/pot files. Use this base class and give this module the rules or use one of the already extended classes. Locale::TextDomain::OO::Extract::Perl is a extension for Perl code and so on.

How to build the lexicon - without Gettext tools?

Use module Locale::TextDomain::OO::Extract::Process. This is able to read, strip, extract, merge, write and clean PO and MO files.

There is a module named Locale::TextDomain::OO::Util::JoinSplitLexiconKeys. That helps you to build the lexicon keys. Do not join the keys by yourself, it can break in future versions. Use the category, domain, language and project part to describe your lexicon. category, domain and language you can use during translation. The only reason for project is to separate your different lexicons.

How to use the lexicon?

The lexicons or all lexicons are stored in memory. The idea to build a combined lexicon is overwriting hash values by same key. So read and overwrite common stuff with specials. In an example case the region file contains only the differences. The common file contains all messages. So it is possible to create full lexicons for any language and region. What you have to describe is: Where are the files - directories. Pick up the common file as en. Pick up the common file and the special as en-gb.

Do not follow the dead end of Locale::Maketext!

But is is allowed to use that writing for backward compatiblity.

What is the problem of Maketext?

This is the reason for another module to have:

More information

Run the examples of this distribution (folder example).


 Application calls           Application calls         Application calls
 Gettext methods             Gettext and               Maketext methods
 (Hint: select this)         Maketext methods              |
         |                   (Hint: Maketext already       |
         |                   exists in your project)       |
         |                               |                 |
         v                               v                 v
 | Locale::TextDomain::OO                                              |
 | with plugin LanguageOfLanguages                                     |
 | with plugins Locale::TextDomain::OO::Plugin::Expand::...            |
 | Gettext                          |\       /| Maketext               |
 | Gettext::DomainAndCategory       | |     | |                        |
 | (experimental)                   | |     | | Maketext::Loc          |
 | Gettext::Loc (Hint: select this) | > and < | (Hint: select this)    |
 | Gettext::Loc::DomainAndCategory  | |     | |                        |
 | (experimental)                   | |     | | Maketext::Localise     |
 | Gettext::Named (experimental)    |/       \| Maketext::Localize     |
 | Locale::TextDomain::OO::Singleton::Lexicon |------------------------.
 `--------------------------------------------'                        |
                            ^                                          |
                            |                                          |
 .--------------------------'-------------------------------.          |
 | build lexicon using Locale::TextDomain::OO::Lexicon::... |          |
 |----------------------------------------------------------|          |
 |           Hash              |   File::MO     | File::PO  |          |
 `----------------------------------------------------------'          |
       ^               ^               ^              ^                |
       |               |               |              |                |
 .-----'-----.    _____|_____    .-----'----.       .-----'----.       |
 | Perl      |   /_ _ _ _ _ _\   | mo files |-.     | po files |-.     |
 | data      |   |           |   `----------' |-.   `----------' |-.   |
 | structure |   | Database  |     `----------' |     `----------' |   |
 `-----------'   `-----------'       `----------'       `----------'   |
                                       ^                   ^           |
                                       |                   |           |
                        build using                                    |
                        Locale::TextDomain::OO::Extract                |
                        and Locale::TextDomain::OO::Extract::Process   |
 | build JSON lexicon using Locale::TextDomain::OO::Lexicon::StoreJSON |
 |       to_json      |       to_javascript       |      to_html       |
              | var localeTextDomainOOLexicon = json; |
 | requires:                                                                        |
 | -                                                             |
 | - javascript/Locale/TextDomain/OO/Util/Constants.js                              |
 | - javascript/Locale/TextDomain/OO/Util/JoinSplitLexiconKeys.js                   |
 | - javascript/Locale/Utils/PlaceholderNamed.js                                    |
 |                                                                                  |
 | implemented:                                                                     |
 | javascript/Locale/TextDomain/OO.js                                               |
 | javascript/Locale/TextDomain/OO/Plugin/Expand/BabelFish.js (under development)   |
 | javascript/Locale/TextDomain/OO/Plugin/Expand/Gettext.js                         |
 | javascript/Locale/TextDomain/OO/Plugin/Expand/Gettext/DomainAndCategory.js       |
 | javascript/Locale/TextDomain/OO/Plugin/Expand/Gettext/Loc.js (Hint: select this) |
 | javascript/Locale/TextDomain/OO/Plugin/Expand/Gettext/Loc/DomainAndCategory.js   |
 |                                                                                  |
 | not implemented:                                                                 |
 | javascript/Locale/TextDomain/OO/Expand/Maketext.js                               |
 | javascript/Locale/TextDomain/OO/Expand/Maketext/Loc.js                           |
 | javascript/Locale/TextDomain/OO/Expand/Maketext/Localise.js                      |
 | javascript/Locale/TextDomain/OO/Expand/Maketext/Localize.js                      |
 |                                                                                  |
 | Example:                                                                         |
 | javascript/Example.html                                                          |
         ^                     ^                  ^
         |                     |                  |
 JavaScript calls      JavaScript calls   JavaScript calls
 Gettext methods       Gettext and        Maketext methods
 (Hint: select this)   Maketext methods   (not implemented)
                       (not implemented)


    require Locale::TextDomain::OO;
    my $loc = Locale::TextDomain::OO->new(
        # all parameters are optional
        plugins  => [ qw(
        ) ],
        language => 'de',          # default is i-default
        category => 'LC_MESSAGES', # default is q{}
        domain   => 'MyDomain',    # default is q{}
        project  => 'MyProject',   # default is undef
        filter   => sub {
            my ($self, $translation_ref) = @_;
            # encode if needed
            # run a formatter if needed, e.g.
            ${$translation_ref} =~ s{__ ( .+? ) __}{<b>$1</b>}xmsg;
        logger   => sub {
            my ($message, $arg_ref) = @_;
            my $type = $arg_ref->{type}; # debug or warn

This configuration would be use Lexicon "de:LC_MESSAGES:MyDomain::MyProject". That lexicon should be filled with data.

as singleton

Instead of method new call method instance to get a singleton.

    my $instance = Locale::TextDomain::OO->instance(
        # Same parameters like new,
        # Initialization on first call only.
    $same_instance = Locale::TextDomain::OO->instance;

Attributes handled with plugin Expand::Gettext::Loc

This plugin can handle named placeholders like {name}. For numeric placeholders add attribute :num. That allows easier automatic translation and to localize numbers. E.g. write {books :numf} to msgid, msgid_plural.

Grammar rules for string placeholders are able to handle affixes. The translation office can add attributes, e.g. {town :accusative} to msgstr, msgstr[n].

Add the modifier code like that to handle that attributes.

        sub {
            my ( $value, $attribute ) = @_;
            if ( $loc->language eq 'ru' ) {
                if ( $attribute eq 'accusative' ) {
            elsif ( $loc->language eq 'de' }xms ) {
                if ( $attribute eq 'numf' ) {
                    $value =~ tr{.,}{,.};
            return $value;

Attributes handled with plugin Expand::Gettext

Like before but a little difference. Instead of




Localize numbers with plugin Expand::Maketext

Add the code to localize numbers.

        sub {
            my $value = shift;
            if ( $loc->language eq 'de' ) {
                $value =~ tr{.,}{,.};
            return $value;


How to use the JavaScript framework see Locale::TextDomain::OO::JavaScript.


method new


It is not possible to call method new with different plugins parameter. The first call will load the plugins. Later call set parameter plugins equal or do not set.

method instance

When using the singleton mechanism, the object need not be transported through all the subroutines.

Instead of

    my $loc = Locale::TextDomain::OO->new(...);


    my $loc = Locale::TextDomain::OO->instance;

In case of webserver child or similar, set the language for every request not as parameter of the first instance call.

method language

Set the language an prepare the translation. You know exactly how to set. This module is stupid.

    $loc->language( $language );

Get back

    $language = $loc->language;

method category

You are able to ignore or set the category. That depends on your project.

    $loc->category($category || q{} );
    $category = $loc->category;

method domain

You are able to ignore or set the domain. That depends on your project.

    $loc->domain($domain || q{} );
    $domain = $loc->domain;

method filter

You are allowed to run code after each translation.

    $loc->filter( sub {
        my ( $self, $translation_ref ) = @_;

        # $self is $loc
        # manipulate ${$translation_ref}
        # do not undef ${$translation_ref}

    } );

Switch off the filter


method logger

Set the logger

        sub {
            my ($message, $arg_ref) = @_;
            my $type = $arg_ref->{type};

$arg_ref contains

    object => $loc, # the object itself
    type   => 'debug', # or 'warn'
    event  => 'language,selection', # 'language,selection,fallback'
                                    # or 'translation,fallback'

method translate

Do never call that method in your project. This method was called from expand plugins only.

        = $self->translate($msgctxt, $msgid, $msgid_plural, $count, $is_n);

method run_filter

Do never call that method in your project. This method was called from expand plugins only.



Inside of this distribution is a directory named example. Read the file README there. Then run the *.pl files.








not known


In the Gettext manual you can read at " Bugs, Pitfalls, And Things That Do Not Work" something that is not working with Perl. The examples there are rewritten and explained here.

string interpolation and joined strings

    print <<"EOT";
            'The dot operator'
            . ' does not work'
            . ' here!'
        Likewise, you cannot @{[ $loc->loc_('interpolate function calls') ]}
        inside quoted strings or quote-like expressions.

The fist call can not work. Methods are not callable in interpolated strings/"here documents". The . operator is normally not implemented at the extractor. The first parameter of method loc_ must be a constant.

There is no problem for the second call because the extractor extracts the Perl file as text and did not parse the code.

Regex eval

This example is no problem here, because the file is extracted as text.


named placeholders

Method loc_ is an alias for method loc_x. But {OPTIONS} is not a placeholder because key "OPTIONS" is not in parameters.

    die $loc->loc_("usage: $0 {OPTIONS} FILENAME...\n");

    die $loc->loc_x("usage: {program} {OPTIONS} FILENAME...\n", program => $0);



Locale::Maketext The choice of the right module for the translation.


Steffen Winkler


Copyright (c) 2009 - 2015, Steffen Winkler <steffenw at>. All rights reserved.

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

syntax highlighting: