The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Gettext - Perl extension for emulating gettext-related API.


  use Gettext;

DESCRIPTION emulates the gettext library routines in Perl, although it calls the external utility program gettext to actually read .mo files.

man gettext on Solaris has pretty good documentation.

The steps to use this module are:

  • install the gnu gettext package if necessary (not needed on Solaris or Red Hat)

  • set TEXTDOMAINDIR, LANGUAGE, LANG, and LC_* if needed in your env.

  • use xgettext on your script that contains gettext calles to make the .po (portable message object) file, run msgfmt to make the .mo (message object) file, and move the .mo file to where you want it, normally $TEXTDOMAINDIR/lang/LC_MESSAGES.

  • call setlocale before calling these functions.

use Gettext;

my $gt = new Gettext;


$gt->bindtextdomain('domainname', 'dirname');


$gt->dgettext('domainname', 'msgid');

$gt->dcgettext('domainname', 'msgid', 'category (locale)');


use strict;

use diagnostics;

use POSIX 'locale_h';

use locale;

use Gettext;

   setlocale(LC_CTYPE, 'es_ES');

   my $gt = new Gettext();

   $gt->bindtextdomain("messages", "/root/work");

   print $gt->gettext("flower"),"\n";
   print $gt->gettext("yellow"),"\n";

   print $gt->dgettext("messages", "flower"),"\n";
   print $gt->dgettext("messages", "yellow"),"\n";

   print $gt->dcgettext("messages", "flower", "fr_FR"),"\n";
   print $gt->dcgettext("messages", "yellow", "fr_FR"),"\n";

   print $gt->textdomain(),"\n";
   print $gt->textdomain(''),"\n";

Tested on Solaris 2.6 and Red Hat Linux 6.0


James Briggs,


perldoc perllocale

TO DO calls the external gettext utility program, but someday should have an internal routine to directory read .mo files.