Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Despite an incredible amount of literature on this topic, I could not find a proper solution for this issue. Accordingly I post my one here.

I had to realize a relatively complex web application. This application bases on and mod_perl for performance reasons and supports several languages. I wanted to have one single code for the logic of the application. An additional requirement was be to be able to add languages easily. After reading a lot about solutions involving templates or dictionaries (typically gettext), I figured out using PERL language packages would fulfill most of my requirements. The problem was to load the appropriate language module at application start up time and to allow the user to change the language for example for print outs. Here is the solution I worked out.

First make a set of simple language packages with names like,, Each of these modules holds hashes with the texts for the application's forms. For this solution to work properly, the hash names must be in lower case. Example for a log in form:

- French in $login{msg1} = "Identifiant";

- German in $login{msg1} = "Benutzername";

- English in $login{msg1} = "User name";

Next use the following at the beginning of each language package to export the content. Example for

foreach $key (sort keys %EN::) { if ( $key !~ /[A-Z]+/ ) { push @EXPORT, "\%$key"; } }

This will export all hashes in lower case and avoid for example the EXPORT array. Next you will have to import the correct languages package in your cgi script with a statement like use [Modulename];.

Unfortunaltely you can hardly load modules dynamically and can hardly reference a variable in a module dynamically. Constructs like use $language; or ${language}::login{msg1}; will not work.

Despite lot of posts on this topic, I haven't found a solution to solve this issue. This means so or so I will not be able to load the languages dynamically but will have to manage the supported languages in the code of the application's logic.

My solution bases on a language variable I called syslang. This variable is either set by the calling cgi script or defined by the selected language of the client's browser. I begin my cgi scripts the following way and include a global language variable:

# ---------------------------------------------------------------- +---------------- # Script header # ---------------------------------------------------------------- +---------------- # Loading perl modules my $start = time(); use FindBin qw($Bin); use File::Basename; use Cwd; use CGI qw/:standard *table tr td select/; use CGI::Carp; use CGI::Session; use Data::Dumper; use strict; # Our global language variable our $syslang = undef;

Next I add the path to my application or packages to @INC array. My application packages are always in a lib directory. This is also the location of the language packages. For the solution to work the @INC array must be enhanced with a begin block or the script will not compile:

# Notice: this will work with Apache, cheap web-server might be a +serious issue! # a) Detect if we are called by the web-server or not # b) Set the path accordingty BEGIN { # Adding application lib path my $libdir = undef; if ( not defined($ENV{SERVER_NAME}) ) { $libdir = getcwd; } else { $libdir = File::Basename::dirname($ENV{SCRIPT_FILENAME} +); } # I know.... but sometimes I am lazy! chdir "$libdir/../lib"; $libdir = getcwd; if ( not grep(/$libdir/, @INC) ) { push (@INC, $libdir); } undef $libdir; }

Now we need to manage the languages. We need to load the package with the correct texts, knowing the user can change the language of the application. Because of mod_perl we need to unload the language modules first and the reload the correct module to force Apache to recompile the script. Here my solution for this:

# Setting the language # This one is to fool the compiler! Don't ask! use EN; # Testing if the language has allready be set or using the one def +ines by the browser # frdlang is my cgi language parameter my $q = new CGI; if ( $q->param("frdlang") ) { $syslang = uc($q->param("frdlang")); + } elsif ( not $syslang ) { $syslang = uc(substr($ENV{HTTP_ACCEPT_LAN +GUAGE}, 0, 2)); } # Unloading language modules to force recompiling under mod_perl foreach my $lang ( grep(/$|$|$/, keys(%INC)) ) { de +lete $INC {$lang}; } # Loading apropriate language module if ( uc($syslang) =~ /^DE/ ) { require DE; DE->import(); } elsif ( uc($syslang) =~ /^FR/ ) { require FR; FR->import(); } elsif ( uc($syslang) =~ /^EN/ ) { require EN; EN->import(); }

Now we might have the issue that some packages return some part of the HTML form, tipically the menus. The language of these parts musst also be changed each time the user switches the language. So we need to reload all the modules impacted. Here my solution for this:

# Unloading the application language related module to force recom +pile under mod perl foreach my $module ( grep(/$|$|$/, keys( +%INC)) ) { delete $INC{$module}; } # setting languages for each modules to load $utils::syslang = $syslang; $install::syslang = $syslang; $login::syslang = $syslang; # Loading language dependent modules require utils; utils->import(); require install; install->import(); require login; login->import(); # ... your cgi code.

This solution has beed tested extensively however any suggestion for something more decent is welcome.


The best medicine against depression is a cold beer!

In reply to A multilingual solution for PERL CGI web applications by Zzenmonk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2017-03-25 23:41 GMT
    Find Nodes?
      Voting Booth?
      Should Pluto Get Its Planethood Back?

      Results (313 votes). Check out past polls.