in reply to Multilingual design

I won't claim to have all the answers. I'm a simpleton who cannot grasp, and therefore does not use, OOP. That said, many modules are beyond my comprehension as well, and I cannot use them.

However, I have developed multiple websites that were multilingual. If only a few languages are anticipated, it is simple enough to load them into a JavaScript array and switch languages "live" without a callback to the server. However, when the language data begins to become more weighty, what I have done is to store them in a database and use AJAX to retrieve them as necessary.

Here is an example for the code I use.

#LANGUAGE ARRAY IS DEFINED FIRST VIA PERL
#SOMETHING LIKE . . .

   my %ilanguages = (

   language	=> [ 'English','ไทย','ລາວ','Espaņol','việtnam','简体中文','繁体中文','한국어' ],
    submit	=> [ 'Submit','กดส่ง','ກົດສົ່ງ','Someter','gửi đi','提交','提交','' ],
    preferences	=> [ 'Preferences','การตั้งค่า','ການຕັ້ງຄ່າ','Preferencias','sở thích','喜好','喜好','' ],
    reposition	=>  [ 'Reposition','เปลี่ยนตำแหน่ง','ປ່ຽນຕໍາແຫນ່ງ','Reposicionar','tái định vị','复位','复位','' ],

    );

#THEN THE ARRAY IS USED TO HASH THE REQUESTED LANGUAGE COLUMN
#SOMETHING LIKE THIS...

sub interfaceLanguage {

my $language = shift @_;
    unless ($language) { $language='English' };
    if ($language eq 'Lao') { $language = 'ລາວ' };
    if ($language eq 'Thai') { $language = 'ไทย' };
    if ($language eq 'Spanish') { $language = 'Espaņol' };
    if ($language eq 'Vietnamese') { $language = 'việtnam' };
    if ($language eq 'Korean') { $language = '한국어' };
    if ($language eq 'Simplified Chinese') { $language = '简体中文' };
    if ($language eq 'Traditional Chinese') { $language = '繁体中文' };

    my $index = -1;
    if ($language ne '') { 

        foreach my $lng (@{$ilanguages{'language'}}) {
            ++$index;
            last if ($lng eq $language);
	}
    } else {
	$index = 0;
    } 

    for my $eachkey (keys %ilanguages) {
        # %ilang PRE-DECLARED AS GLOBAL
        $ilang{$eachkey} = $ilanguages{$eachkey}$index;
    };
	
    return;
	
} #END SUB interfaceLanguage


Then the HTML code is formed using the language "handle" for each required expression.
#BUTTON VALUE <input type="button" id="resetwindows" onclick="setScreen();setpos();s +etScreen();setpos();" value="$ilang{reposition}" />


So instead of making the value of your button say something like "Submit", you set the value to "$ilang{submit}". This will get replaced by whichever language is selected.

In the case of a form element in which its value is its text or label on the screen, the value can be updated via JavaScript without necessitating a page reload. If its value is stored in a JS array, this can be done entirely client-side. Or if there is other text, it can be placed in a <div id="preferences"></div> and updated to the language of choice via the innerHTML property for that div element, e.g. document.getElementById("preferences").innerHTML="$ilang{preferences}";. But if the site is to have many language options, AJAX becomes more attractive, and instead of the Perl array I excerpted as an example, a database table for the language data becomes more expedient.

One major advantage to this system is that it also facilitates translation into other languages. It would be simple to create a utility for adding a language column to the database. Adding the new language to the website would then be as simple as adding its name, and possibly a language flag-icon to represent it, to a language menu. Whichever language was selected, that column in the table would be found to supply the language items throughout the page.

Blessings,

~Polyglot~

Replies are listed 'Best First'.
Re^2: Multilingual design
by Bod (Parson) on Dec 11, 2022 at 23:06 UTC

    Many thanks for that Polyglot - your knowledge of languages is undoubtedly greater than mine. Although, I would have anticipated that from your username 😜

    Your solution is workable. But I'm not sure about using AJAX instead of reloading the whole page given that pretty much every bit of content will change. It also suffers from violating the MVC model as the text to display is being handled by the controller (i.e. the Perl script) and not the view (the Template in my case). I do appreciate that your example prints the HTML directly from Perl and not through a template.

      I use AJAX instead of reloading the entire page because I am running the language conversion on interactive pages with web forms. A page reload will mean the user must reset and/or reenter all of his or her already-entered information again. But if you are only wanting to have static pages, there's no advantage to AJAX; it would make sense to just reload the page.

      Blessings,

      ~Polyglot~

        I use AJAX instead of reloading the entire page because I am running the language conversion on interactive pages with web forms

        That makes a lot of sense.

        BTW - you said elsewhere that Template is beyond you...
        If you can implement that level of AJAX, then you can quickly master Template!