





















The best medicine against depression is a cold beer!

Comment on A multilingual solution for PERL CGI web applications
Select or Download Code
Re: A multilingual solution for PERL CGI web applications
by Anonymous Monk on May 03, 2013 at 12:15 UTC
    Store your language specific strings in a database and use templates with place holders for the page content. Before rendering the page just do a mass replacement of the place holders with the proper strings. Then you can provide a tool for translators to make changes to the strings in the database without mucking around in your .pm files.

      This is definitely also a solution. However it requires a database which needs maintenance. In my case a database is not an appropriate storage solution because the system has to cope with text elements. The best way to find text elements is to store them as files and index the files with web search engine. So the implementation of the whole system is easier this way. In addition I noticed this solution is damned fast. This is due to the fact I replace all texts in one shot.


      The best medicine against depression is a cold beer!
Re: A multilingual solution for PERL CGI web applications
by kbrannen (Acolyte) on Jul 18, 2013 at 23:21 UTC
    In our product we use a DB to hold the translation tokens. However, if you're going to do it with files, why not just create a hash for each language, use Freeze then write it to store it, and later pull it in and Thaw it?

    I'd probably create a CGI app to maintain the data, rows for each token, a column for each language, and when saving it, each column is a hash with the row name as the key and that's what's frozen for the real app to use later. Use the same hash for each one so you have the same name (e.g. %tokens).

    Then your normal app can slurp in the frozen hash by opening the needed language file and it has all the correct token values. Then you can substitute $tokens{msg1} or whatever you need without worrying about the language and off you go.

    In fact, once you have your tokens in memory and assuming you have a unique pattern for them (e.g. [[msg1]]), it's a matter of doing something like:
    $page =~ s/\[\[(\w+)\]\]/$tokens{$1}/ge;
    and you're good to go. No need to manipulate @INC or anything else tricky.

    That also means that adding new languages is easy. Just create an empty file for the new language in your language dir, your CGI editing app will see it and create a new column of empty cells, then when you've filled it out and save, you'll get a new frozen hash in it's own file. If you want to remove a language, just remove the file.