Jump to content

urlsegments on multiligual site


neosin
 Share

Recommended Posts

I haven't test this yet, just trying to pre-strategize before I get the code going

When using urlsegments on a multilingual site would I need to include the translated url segment value for the alternate language or will PW handle it?

for example in my template if I put

if($input->urlSegment1 == 'map')

but in the alternate language the segment would be "carte" instead of "map", will PW know by itself that map=carte or carte=map or do I need to take that into consideration in the template also?

if($input->urlSegment1 == 'map' || $input->urlSegment1 == 'carte')

thanks in advance

 

Link to comment
Share on other sites

URL segments that you enable on the template settings are just strings, not selectors, so ProcessWire won't be able to do this kind of check. 

But you can accomplish it by doing the following:

On Template Settings, add a regex on the URL Segments field like: regex:^(map|carte)$

On your template code, add a check:

// we are only using 1 URL segment, so send a 404 if there's more than 1
if($input->urlSegment2) throw new Wire404Exception();

if($input->urlSegment1) { //if there's any segment that matches the ones you specified on the Template Settings, PW will proceed.
	//do your code
}

Going to example.com/map or /carte will pass the conditional.

  • Like 3
Link to comment
Share on other sites

1 minute ago, Sergio said:

URL segments that you enable on the template settings are just strings, not selectors, so ProcessWire won't be able to do this kind of check. 

But you can accomplish it by doing the following:

On Template Settings, add a regex on the URL Segments field like: regex:^(map|carte)$

On your template code, add a check:


// we are only using 1 URL segment, so send a 404 if there's more than 1
if($input->urlSegment2) throw new Wire404Exception();

if($input->urlSegment1) { //if there's any segment that matches the ones you specified on the Template Settings, PW will proceed.
	//do your code
}

Going to example.com/map or /carte will pass the conditional.

thank you for the clear example, I was not even aware of the template regex possibility until this moment, mind you I've only been playing with PW for 2 days.

This is great news and means less coding required and no changes of template code if things change down the road in the project.

thanks!

Link to comment
Share on other sites

Came back to say that the regex example I gave can also work without regex, because we're not actually using its power.

So, if you just add "map" and "carte", one per line, on the URL Segment field, in the template settings, it will also work.

Regex is more appropriate when you have cases like these:

//to get segments like /export-json or /export-xml
regex:^export-(json|xml)$

//to get segments like "2018/episodes"
regex:^\d{4}/episodes$

//to get segments like "category/ebooks/". I used this when there's no category pages associated with the template, just options of a Options field.
regex:^category/[a-z]+$

 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...