You are here:  » Multi-lingual Site

Active Forum Topics


Multi-lingual Site

Submitted by support on Mon, 2015-10-19 09:32 in

Hi everyone,

I've had a couple of enquiries recently regarding using Price Tapestry for a multi-lingual site, so I thought I'd describe an easy way to set this up by using a language cookie to determine which version of translate.php to include. To set this up, first create multiple copies of includes/translate.php with the languages required, renamed with an appropriate _{code} suffix, for example:

English:
includes/translate_en.php
(this can just be a copy of includes/translate.php from the distribution)

French:
includes/translate_fr.php
(French, German, Spanish, Italian and a "fill in the blanks" version are available to download)

With the required versions in place, now create a new includes/translate.php as follows:

<?php
  $translate_lang
["en"] = "English";
  
$translate_lang["fr"] = "Française";
  
$translate_langDefault "en";
  if (isset(
$_POST["lang"]))
  {
    
$lang $_POST["lang"];
    if (!isset(
$translate_lang[$lang])) $lang $translate_langDefault;
    
setcookie("lang",$lang,0,"/");
    
header("Location: ".$_SERVER["REQUEST_URI"]);
    exit();
  }
  
$lang = (isset($_COOKIE["lang"])?$_COOKIE["lang"]:"");
  if (!isset(
$translate_lang[$lang])) $lang $translate_langDefault;
  require(
"translate_".$lang.".php");
  function 
translate_form()
  {
    global 
$translate_lang;
    global 
$lang;
    print 
"<form method='POST'>";
    print 
"<select name='lang' onChange='JavaScript:this.form.submit();'>";
    foreach(
$translate_lang as $k => $v)
    {
      
$selected = ($lang==$k?"selected='selected'":"");
      print 
"<option value='".$k."' ".$selected.">".$v."</option>";
    }
    print 
"</select>";
    print 
"<noscript><input type='submit' value='Change'></noscript>";
    print 
"</form>";
  }
?>

Edit the $translate_lang array at the top of the new file containing entries for each version of translate_{code}.php created, with the array keys matching {code} and the values being the label to display in the change language select box.

Finally, to display the form, edit your template (e.g. html/header.php or html/footer.php) and at the point you wish to display the change language form simply add the code:

<?php translate_form(); ?>

The form submission is picked up using isset($_POST["lang"]), and if TRUE a "lang" cookie is set using setcookie() with the value selected. The first entry in the $translate_lang acts as the default in the case of an invalid submission.

Cheers!
David
--
PriceTapestry.com

Submitted by Rocket32 on Mon, 2015-11-16 12:10

I tried the above by making my site multi-language, but when adding the codes a blank page comes up. Also is the form required for this to work? Can the language be auto detected and translate picks correct language if available? My in header.php reads language=en right now. Does this need changing?

Submitted by support on Tue, 2015-11-17 13:00

Hi,

I've updated the above to use a different method of default selection - see line 4:

  $translate_langDefault = "en";

The previous method can cause problems on some versions of PHP, so if the default wasn't being selected correctly the require() would fail which is a fatal error so that might be what's happening here.

If you're still getting a blank page, add the following lines to the very top of the script after the opening <?php tag:

  error_reporting(E_ALL);
  ini_set('display_errors','on');

...and see if that reports the problem - let me know what is displayed (or if no difference) and Ill check it out further for you. Once up and running, let me know and I'll have a think about automatic selection - it would need to come from something in user context rather than the HTML source as that will be fixed - I'll have a look at what JavaScript system variables might be available to do this...

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Wed, 2015-11-18 23:46

Still now success. I am getting the following error;

On Chrome Browser;

This webpage is not available

ERR_CONTENT_DECODING_FAILED

On Firefox Browser;

Content Encoding Error

The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

Please contact the website owners to inform them of this problem.

Submitted by support on Thu, 2015-11-19 08:36

Hi,

Please could you email me your modified translate.php and each of the language versions you are using e.g. translate_en.php, translate_fr.php etc. and I'll try out your exact same files on my test server for you and see if I can spot the problem...

Thanks,
David.
--
PriceTapestry.com

Submitted by smartprice24 on Thu, 2018-01-11 17:58

Hi David. In the last version PT, no swith language. Only default language work.

The dropdown select language, but no change.

Thanks.

Giuseppe.

Submitted by support on Fri, 2018-01-12 10:35

Hello Giuseppe,

I just doubled checked everything against the latest distribution and the language was selected and changed as expected so as above, if you could email me your modified translate.php and each of the language versions you are using e.g. translate_en.php, translate_fr.php etc. and I'll try out your exact same files on my test server...

Cheers,
David.
--
PriceTapestry.com