Jump to content

Multi-language URL field


humanafterall
 Share

Recommended Posts

Hi,

I have a URL field that will sometimes have relative/local URLs on a multilingual site, for example /contact/ 

However the URL field does not seem to pick up when I'm on another language, for example /fr/ so I'm taken to the default language page for /contact/ rather than /fr/contact/

Is there a way to make the URL fields play well with a multi-language site?

Thanks!

 

Link to comment
Share on other sites

This is an interesting problem ?. I don't think it is possible out-of-the-box since having a language active does not prevent access to other languages. Hence, ProcessWire 'respects' the requested URL, as long as it is viewable. Off the top of my head I cannot remember if there is a module out there that solves this problem, but have a look, just in case, especially for modules by Robin S. Otherwise, you'll need a hook or a textformatter module that changes the links on the fly.

  • Like 1
Link to comment
Share on other sites

I can't seem to locate any module that tackles this (even from the incredible Robin S!) — it feels like this should be part of the URL field so it supports multilingual sites.

I think I'll need something that can work for all URL fields (now and future).

I guess the best thing would be to devise a hook that modified the URL before page rendering. Could you start me on the right road here please?

 

 

Link to comment
Share on other sites

45 minutes ago, humanafterall said:

I guess the best thing would be to devise a hook that modified the URL before page rendering.

Now that I think about it more, it seems that a redirect would suffice as well? I don't know zilch about redirects and SEO if that is not a problem then all we need is for the hook to redirect to the same page in the current language under certain conditions. So, thinking out loud here:

  1. We can limit the hook to certain frontend templates, a parent, etc. 
  2. If condition #1 is met, we check if the current user language is not the default language. If it is, we abort.
  3. If condition #2 is met, we check if the requested page is viewable in the current user language. If yes, we redirect to its URL. If not, we do nothing.

Having said that, I cannot off the top of my head remember where to hook. Maybe before some page render. I'll have a look, unless somebody else beats me to it ?.

  • Like 1
Link to comment
Share on other sites

On 12/7/2020 at 11:55 PM, humanafterall said:

Is there a way to make the URL fields play well with a multi-language site?

I know very little about multi-language development in PW, but just to confirm: are you using the FieldtypeURLLanguage module? It's created by Ryan but is different from the core FieldtypeURL module.

  • Like 1
Link to comment
Share on other sites

6 hours ago, Robin S said:

are you using the FieldtypeURLLanguage module? It's created by Ryan but is different from the core FieldtypeURL module.

Nice find Robin!

@humanafterall,

I've tested FieldtypeURLLanguage. It works like language fields with inputs for all available languages. It would work in your case but will obviously require input for all/required languages by your editors.

Link to comment
Share on other sites

  • 3 weeks later...

I had the same problem. After playing around I found out that there is a code in LanguageSupportPageNames.module with a comment "verify that page path doesn't have mixed languages where it shouldn't". This code was redirecting my non-default language page path to the default page path (and that didn't work correctly for my setup). I commented the redirect line out and it worked for me:

// verify that page path doesn't have mixed languages where it shouldn't
$redirectURL = $this->verifyPath($this->requestPath);
    if($redirectURL) {
    #$this->wire('session')->redirect($redirectURL);
    return;
}

But this can't be a solution (to change code in the wire). That brings me to my question:

Is there a way to deactivate this redirect using the config, a hook or something?

My Setup:
Nginx Webserver, PHP 7, Processwire 3.0.165.
I use 2 different domains, one for each of my two languages.
 

Edited by Andiministrator
  • Like 1
Link to comment
Share on other sites

On 12/9/2020 at 4:17 AM, kongondo said:

Nice find Robin!

@humanafterall,

I've tested FieldtypeURLLanguage. It works like language fields with inputs for all available languages. It would work in your case but will obviously require input for all/required languages by your editors.

I've installed it and, as you say, it just allows input for URLs for other languages. 

This would mean manually adding all the URLs to any URL fields, so somewhat workable but not really ideal solution to the problem.

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

i'm not sure i understand the question that well but let me try
the problem seems to be, you would like to have the localisation for each language in the url, for example as you said, xxx.com/contact xxx.com/fr/contact and this for all you pages

if so, the solution is really simple provide you have installed the FieldtypeURLLanguage like Robin an kogondo said
by default, the home page is the "mother" of all the other pages and, by default, the home page has no name in pw but has the name field usable then
- leave it blank in the default language
- in the other language(s) use whatever you want to set it's url (quite often the iso code will be the simplest)

this way the home page url wil be
- xxx.com in the default language
- xxx.com/fr/ in french and so on for all the languages you use
and the pages urls will be
- xxx.com/the-page/ in the default language
- xxx.com/fr/la-page in french for example
this works if you have the same name in both languages too, xxx.com/contact => xxx.com/fr/contact

hope this is what you were looking for ?

have a nice daay
 

  • 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

×
×
  • Create New...