Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

RFC: improvising multi language HTML

by LanX (Saint)
on Aug 11, 2015 at 16:43 UTC ( [id://1138200]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

honorable monks!

I have to improvise a multi-language CGI (sorry) without template system with in Perl embedded HTML (sorry)

I "invented" my own pure Perl language templating in the following code and would be interested in feedback and suggestions.

use strict; use warnings; use Data::Dump; my $language ='de'; # cgi-par +am # $language ='fr'; # cgi-para +m my %allowed_language = map { ($_=>1) } qw/en de/ ; unless ( $allowed_language{$language} ) { my $default='en'; warn "Unknown language code '$language', falling back to '$default' +"; $language = $default; } my $ht = <<"__HTML__"; <title>@{[ text( en=>"English", de=>"Deutsch" ) ]}</title> __HTML__ print $ht; sub text { my %texts =@_; return $texts{$language}; }

Most likely reinventing the wheel ...

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: RFC: improvising multi language HTML
by shmem (Chancellor) on Aug 12, 2015 at 19:57 UTC
    ... without template system ...
    I "invented" my own pure Perl language templating ...

    First off, templating without a system which leads to a templating solution re-inventing the wheel is not something which won't be frowned upon not too much here. That said, your templating just consists in interpolating function call returns into here-docs via the babycart operator @{[]} - which is - boiled down - all what templating is about.

    In a node above, you write:

    The whole app is still in conception, texts and design will change often, and the form of translation isn't sure yet.
    ...
    Transition shouldn't be to complicated later.

    Well, most likely later on your static here-docs will be split up and parametrized as well, you'll most likely get tag classes, iterations, conditional iterations, css and javascript and such - and complexity will grow.

    It is much like parsing XML with regular expressions - by the time you will have resolved all those issues, you will have written the better part of a templating system. And I bet it will be close to any one already done (including my attempt).

    If none of the above will ever happen - well, then: looks fine to me. Hashes, function calls - straight forward.

    I feel like an Old Grey Monk, now.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: RFC: improvising multi language HTML (gettext)
by Anonymous Monk on Aug 11, 2015 at 23:47 UTC

    Why not regular dictionary like gettext?

    PickTheLanguage( $cgi ); ... <title>@{[ text('title') ]}</title> ... <h1>@{[ text('The Choosening') ]}</h1> sub PickTheLanguage { ... warn ... our $LANG = 'fr'; # from $query ## ensure loaded %dictionary = ( fr => { title => "French", "The C +hoosening" => "Le choisirning" } ); } sub text { $dictionary{$LANG}{"@_"} || "@_"; }
    ppixregexplain.pl
      > Why not regular dictionary like gettext?

      For the same reason why I use literal strings instead of a variable which is used only once.

      The whole app is still in conception, texts and design will change often, and the form of translation isn't sure yet.

      But I already using a hash with keys for frequently used expressions.

      Transition shouldn't be to complicated later.

      > ppixregexplain.pl

      sorry?

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1138200]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found