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

Hey everyone,
I got a project which is an existing and running website completely built in perl. No html, CGI-only. My perl CGI-scripts looking like:

#!/usr/bin/perl # Includes use strict; use warnings; ... print "Content-type: text/html\n\n"; my $sHTML = qq{ <html> <header> <title>Some title</title> </header> <body> }; $sHTML .= &MyPackages::MyFunctions(); $sHTML .= qq{ <p>SomeText</p> </body> </html> };


My task is to make this website multilingual. I'm actual doing research and try to find some great concepts which would fit to me. So I just wanted to ask you if anyone of you got some ideas for me how to do this.

I know some ways to do this if you're having html files and using PHP for dynamic content. I read about a simple database solution (get text from db) or a way to use the html-lang attribute. For now I would say the html-lang attribute won't work in my case, because usually you build multiple directory structures to store pages in each lang. In my case due to CGI this would mean any change on the website needs to be done on X-files. In my opinion this is a very bad idea...

I also found this A multilingual solution for PERL CGI web applications solution which was looking okay. But in case that I do use a database, I could also just store it there, which will be more effective I think.
Then I found some people using a perl Modules (Template-Toolkit, i18n, Locale::Maketext::Simple, Template::Multilingual, ...) and I took a look on them but right now I don't know if I can even use them in my case. How would this looks like if you got an built page like I got (example shown above)?
I'd like to add a information for this website, it is an multiple-client multiuser website.

Thank you so far.
Best regards.


Update:
Do you got an idea how do I use this if I got a dynamic website? Do I have to edit the template every time? Example: My website gets data out of a database to fill tables.

Ohh, just found: http://www.template-toolkit.org/docs/tutorial/Web.html#section_Dynamic_Content_Generation_Via_CGI_Script this - I'll try to do that

Update 2:
Okay, it's cool how it works, but which way do you think is better? Using differet templates for each language or still using 1 template and just retrieve information texts out of the database?

Replies are listed 'Best First'.
Re: Concepts of multilangual CGI-Website?
by 1nickt (Canon) on Jul 10, 2015 at 12:18 UTC

    Hi Yaerox. The thread you referenced is well-found; it contains three posts, each with a good idea.

    You will have to decide whether storing in a DB or in text files is the best solution for you (the questions of how the text is updated, and by whom, will be most of the decision, IMO). But whatever you do about that, you will need to get the text out of your code, and one step more, out of the HTML.

    Personally I have used, for big projects and for small, and I really like using, the Template Toolkit -- I find it easy to use and very powerful. You can pass complex data structures to the template and process them with conditional logic, or you can use logic to design your template library and pass simple data to the templates. You'll also be able to use the same system for other output, such as email messages you need to send in different languages, etc.

    You might have to spend a few days figuring out your setup, but once you have it going you will have an easy time.

    Basically you should separate the text both from the programmatic code, and from the display code.

    [ Perl programs to create output data ] V [ Languages library in DB or textfiles ] V [ Template Toolkit templates to display output ]

    For your example, you could make the change to your demo program as simple as follows:

    #!/usr/bin/perl # Includes use strict; use warnings; use MyPackages; use Template; my $tt = Template->new({ INCLUDE_PATH => '/usr/local/templates', INTERPOLATE => 1, }) || die "$Template::ERROR\n"; my ($lang, $data) = &MyPackages::MyFunctions(); # Module above changed to return data, not HTML # You're going to love deleting all that non-perl code!!! # $data is { first_name => 'Joe', last_name => 'Bloe' } $tt->process("${lang}/homepage.tmpl", $data) || die $tt->error(), "\n";
    # in en/homepage.tmpl Welcome $first_name $last_name # or, if you set INTERPOLATE to 0 Welcome [% first_name %] [% last_name %] # in es/homepage.tmpl Bienvenido $first_name $last_name # ...

    And so on.

    I strongly recommend keeping separate templates for your languages rather than trying to build on the fly, as in

    # in single_template.tmpl # Do not do this! $welcome $first_name $last_name, $next_sentence. $second_paragraph. ...

    Languages are too different in structure for that.

    Good luck: I think you will do fine, based on how you have researched your question so far. You have a lot of work ahead for a few weeks, then one day you will notice that you are able to advance much. much quicker than before the upgrade.

    Remember: Ne dederis in spiritu molere illegitimi!
      I really appreciate your reply sir,
      and I know there are really great techniques to do things, but I think I'll go with the more simple way and build 2 database tables and get data once the pages loads.
      Why I don't use a module? Well, I'll in future. I'm on this project for around 6 months now. The project needs to get ready for a realistic simulation right now, so I'll do it this way because It's simple and it requires only some effort.

      While developing this project I found alot of places where I feel like I have to change this in future. I feel like I'm always rising with my project ;) Based on the fact that I love using template-based homepages, I'll look to get an workaround to get into it and implement this on version 2 of my project.

      I took a little closer look (around 30 minutes) on this toolkit-template and I don't feel like I understood how it works. Now that I got an example of you, I'll work on this again in near future. Thanks for this example too.

      Have a good day.

        Always, the way forward is with a minimal test. It will always be overwhelming to think about converting what you have to ... anything! That is how most projects turn into a mess, because the developer is too intimidated by the size of the task involved in upgrading.

        To solve this, start small. In the case of converting a site where all the HTML is hard-coded in the Perl, and you are considering moving to a Template system, this means: do not consider trying to accomplish all the job! Consider instead taking the first step.

        • Install TT2, and in a completely separate project, write a 10-line Perl script that uses a 10-line template to output an HTML page. Make sure the Perl script uses no HTML and no hard-coded text that will be in the output.
        • This will take you ~ 30 minutes. After, enjoy the feeling of awesomeness you get from separating your HTML from your Perl. Have a cup of tea, then go back to your regular work. Even if this means going temporarily down the "wrong" path.
        • Next day, or next chance you get, go back to your new stuff and make the template pull in ("Process") another 10-line template. Put some condition in the Process directive in your main template so the sub-template is "chosen" in some way. (Like you might do with languages).
        • This will take you ~ 30 minutes. After, enjoy the feeling of awesomeness you get as you realize that structurally, you are now halfway to your goal. Have a cup of tea, then go back to your regular work. Be prepared to feel impatient with your regular work, and to stay late after getting it done, to work on your new project.

        And so on. Within less time that you imagine it will take, you will be on your way to an upgrade. But you have to take the first step . . . in the right direction.

        Remember: Ne dederis in spiritu molere illegitimi!
      Do you got an idea how do I use this if I got a dynamic website? Do I have to edit the template every time?

      Example: My website gets data out of a database to fill tables.
A reply falls below the community's threshold of quality. You may see it by logging in.