Forum

Thread tagged as: Question, Configuration

Multi Language Template Page

I am currently building a site which allows admins to add content in both English and Welsh.

I've followed the instructions here - https://docs.grabaperch.com/perch/building/how-do-i-create-a-multilingual-site/ to make duplicate content areas like so - <?php perch_content('Description Text - '.$lang)?>

The page is a template however and the end user will be adding many pages at a time. When they add the page through the perch admin panel, they can only see the English language areas when they go-to-and-refresh the page. Then they have to switch to the Welsh version of the site and do the same again.

Is there any way to pass through both language options to the template when it is created so they don't need to refresh the page? Or even if they only have to do it once - that would be fine!

Alex Jeffers

Alex Jeffers 0 points

  • 4 years ago

Found my own solution to doing this, possibly not the most elegant - but it works!

At the top of the template page file there is this statement <?php include($_SERVER['DOCUMENT_ROOT'] . '/app/perch/lang_support.php'); ?>

The code contained within is this, taken from the multilingual site docs reference page :

<?php
session_start();
$lang = 'en'; // default lang
switch(perch_get('lang')) {
    case 'en':
        $lang = 'en';
        break;
    case 'cy':
        $lang = 'cy';
        break;
    // add more cases for language options here...

    default:
        if (isset($_SESSION['lang'])) {
            $lang = $_SESSION['lang'];
        }
        break;
}
$_SESSION['lang'] = $lang;
?>

Then, back to my template page, I have this section at the very top of the page

<?php
perch_content_create('Achievement - en', array(
    'template' => 'text.html'
));
perch_content_create('Achievement - cy', array(
    'template' => 'text.html'
));
?>

and further down the page, where the text needs to be rendered, a plain old fashioned php if statement:

          <?php if( $lang == 'en'){
            perch_content_custom('Achievement - en');
          } elseif ($lang == 'cy'){
            perch_content_custom('Achievement - cy');
          }
          ?>

The language can then be switched by adding a ?lang=cy or ?lang=en to the end of the URL