Jump to content

Issue with "Example 3: Display a different language based on URL pre-segment"


jacmaes
 Share

Recommended Posts

I've been trying to set up a bilingual site with Spanish as the default language, and English as the second choice. I've chosen the third option --"Example 3: Display a different language based on URL pre-segment"-- on the page:

http://processwire.c...anguage-fields/

I've placed the following code in the "language-gateway.php" as indicated (I think) in the tutorial:

<?
$user->language = $languages->get($page->name);
$path = '/';
foreach($input->urlSegments as $segment) {
$path .= $segment . '/';
}
$mypage = $pages->get($path);
if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();
// echo $mypage->render();
// the line above is commented out to avoid a "rendering" the page twice

$out = $mypage->render();
$regex = '{(<a [^>]*href=["\']/)(["\']';
$topnav = $page->parent->children();
foreach($topnav as $p) $regex .= "|" . $p->name;
$regex .= ")}";
$out = preg_replace($regex, '$1' . $page->name . '$2', $out);
echo $out;
?>

Everything works great, except that the regular expression does not seem to work. My links in the English version (which again is NOT the default option in my setup) are outputted as "domain.com/enabout/" for the About page in the default PW template instead of "domain.com/en/about/". Notice that there's a trailing slash missing after "en".

What am I doing wrong?

Any help would be greatly appreciated.

Link to comment
Share on other sites

I'm not really sure without testing and it's damn difficult ;)

Not sure if you noticed there's a collaborative module going here

Not sure if that would work with your setup (but looks like), it's so far pretty stable, but not tested heavly. But without using it we will never know. If your project isn't that complicated and may be a good canditate to give it a try. There's only very little you have to do to make it work (not like above) and can be disabled without problems. It even works with default install and one additional selector on the navigation to even publish languages on the page separate (checkbox).

If you have more questions, put them there in the thread and we can surely help.

Link to comment
Share on other sites

Thanks a million, Soma. Your code fix did work. I think this should be fixed on the instructions page mentioned above.

I have developing another bilingual site, and the module you suggested looks like a perfect fit. I'll let you know if I run into any issue.

Link to comment
Share on other sites

  • 3 months later...

I have followed this method #3, based on language gateway and url segments, and it’s mostly working fine.

Except that when accessing a URL which is including url segments, for default language it's OK, but for custom language, language-gateway fails to retrieve the page.

In the above code, the following fails :

$mypage = $pages->get($path);

example of $path which fails : /artiste/kandinsky/, where

- (/fr/ is the language gateway)

- /artiste/ is the real page url, and

- kandinsky is url segment No1

Any idea ?

** edit **

Given the huge success with my question ;-) , I'll try again by rephrasing it as follows :

The behaviour that I observe implies that the language gateway code does not support a target URL which has url segments.

It this the expected behaviour ? Or am I obviously doing somethig wrong ?

thanks

Edited by er314
Link to comment
Share on other sites

  • 5 weeks later...
The behaviour that I observe implies that the language gateway code does not support a target URL which has url segments.

It this the expected behaviour ?

Sorry I missed this before. Got your PM so replying here. I'm still having some trouble understanding the question, but think I follow a bit. It sounds like you are trying to use $pages->get() to retrieve a page via a path like /a/b/ where /a/ is an actual page and /b/ is a urlSegment. A path with a urlSegment in it doesn't resolve to an actual page except when requested from the browser. So you can't use the API to retrieve a page by path with a urlSegment in it -- you'll get a NullPage. You would want to use just the /a/ (the part that is actually a page) in your API call. urlSegments are just runtime things for you to branch from in your template. They don't represent physical pages to the API.

Link to comment
Share on other sites

Thanks for your reply.

So this means that the stock "language gateway.php" code does not work as is, when the application has pages with URL segments

-> I should adapt the template code to my needs, is order to detect whenever the target page has URL segments, and then only perform get() on the real base URL.

This leads to another questioning : once I have the real page object in one hand, at the target url segments in another hand, is there a way to render() the page by providing it with the urlsegments ? I don't see how this can be done with the API (I don't see neither how to do this with regular URL variables, instead of urlsegments, by the way)

I'll think about it and I'll post back here if I have ideas :-)

Link to comment
Share on other sites

So this means that the stock "language gateway.php" code does not work as is, when the application has pages with URL segments

-> I should adapt the template code to my needs, is order to detect whenever the target page has URL segments, and then only perform get() on the real base URL.

That's right, if you are using URL segments for something else, then that use would have to be considered by your language gateway.

This leads to another questioning : once I have the real page object in one hand, at the target url segments in another hand, is there a way to render() the page by providing it with the urlsegments ?

If the page you are rendering has a template that is also looking for URL segments (perhaps for some other purpose), then you could set them manually:

$input->setUrlSegment(1, 'something'); 
$input->setUrlSegment(2, 'something-else'); 
// and so on
I don't see neither how to do this with regular URL variables, instead of urlsegments, by the way)

This one would be even simpler:

$input->get->some_variable = 'some-value';
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...