Forum

Thread tagged as: Question, Runway

Mutilingual site strategy routing issue (Perch Runway)

I'm using Perch Runway to rebuild a multilingual website (en_EN / de_DE) and have more or less adopted the second strategy described in https://solutions.grabaperch.com/architecture/how-do-i-create-a-multilingual-site but I'm running into an issue with the routing.

My idea is to use the 2 letter language code at the beginning of the URL to load the correct layout while using a single Master Page for both languages:

For example:
I have a page called "Range of services" using '/templates/pages/services.php' as a Master Template.
Its location path is '/services' with two routes to reach it: '/en/services' and '/de/services'.

I was hoping to use PerchSystem::get_page() to access the URL called and explode it into an array of which the second element would be the language code.

$page = PerchSystem::get_page();
$lang = $div = $sec = $chap = "";
$page_array = explode("/", $page);
if (!empty($page_array[1])) { $lang = $page_array[1]; }
if (!empty($page_array[2])) { $div = $page_array[2]; }
if (!empty($page_array[3])) { $sec = $page_array[3]; }
if (!empty($page_array[4])) { $chap = $page_array[4]; }

I could then pass the $lang variable to the layout:

perch_layout('services/services_main', [
    'lang' => $lang,
]);

to target the correct region dynamically:

<?php perch_content('Title - '.perch_layout_var('lang', true)); ?>

My problem is that I can't figure out an elegant way from within Perch Runway to access the URL called before it got routed.

PerchSystem::get_page() will return '/services' when I load the URL '/en/services' in the example above.

Is there way to get hold of what the URL was initially before it get routed ?

I guess I could always use $_SERVER['REQUEST_URI'], but I'd rather stick to Perch functions if possible…

David Roessli

David Roessli 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you name the segment in your routes:

[en:lang]/services

and

[de:lang]/services

you can then get the language with:

perch_get('lang')

If you turn on debug, you should see the various page arguments that have been extracted from the URL and are available in your page.

Brilliant! Thanks a million @drewm :)