Jump to content
humanafterall

Multi-language URL field

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!

 

Share this post


Link to post
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

Share this post


Link to post
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?

 

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Note: Custom Paths is incompatible with the core LanguageSupportPageNames module. I have no experience working with LanguageSupportPageNames or multi-language sites in general so I'm not in a position to work out if a fix is possible. If anyone with multi-language experience can contribute a fix it would be much appreciated!
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
    • By AndZyk
      Hello,
      I am currently building a intranet which will be hosted on the local network of a company. This intranet has many links to files on their fileserver with the protocol file://.
      So for example the links look like this file://domain.tld/filename.ext
      When I try to insert such a link into a URL field, I get the error, that only the protocol http:// is allowed. When I try to insert such a link into a CKEeditor link, it gets stripped out. Is it possible to insert such links into the FieldType URL and CKEditor.
      I know that I could use a FieldType Text or insert a RewriteRule in the .htaccess file, but I am looking for a more elegant solution. 😉
      Regards, Andreas
    • By stanoliver
      Hi! 
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      url_redirect|url 
      works just fine but when I try something similiar in the second code snippet
      $parent->url_redirect|url
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
    • By gregory
      Hi everyone.
      I can't see the PDF uploaded via a field called "pdf".
      I get a url like this: http://localhost:8888/mywebsite/site/assets/files/1129/test.pdf%EF%BB%BF%EF%BB%BF
      Could anyone help me? Thank you.
      <?php foreach($page->case_studies as $item) { if($item->type == 'contenuto') { echo " <a class='btn btn-primary btn-sm' target='blank' href='{$item->pdf->first()->url}' role='button'>Download</a> "; } } ?>  
    • By ce90
      Hello everyone,
      hopefully this is the right place to ask and is not a duplicate question.
      I'm pretty new to processwire, so... if this question is kind of funny for some of you, you're welcome 😄 
      I have the following issue and can't find anything understandable about it. Maybe I'm searching the wrong way, but anyways.. here is my question:

      How is it possible to rewrite the URLs, that I'll get a *.php ending?
      Example:
      https://www.mysite.de/urlsegment/ -> https://www.mysite.de/urlsegment.php
      https://www.mysite.de/urlsegment/urlsegment/ -> https://www.mysite.de/urlsegment/urlsegment.php
      Because I've read a lot about "Why do you wanna do this or have that?" – here my answer for that in advance:
      I've built a processwire installation inside or around an existing website. Therefore, we want to keep the existing *.php Google entries. Sure, we could redirect via 301 Redirect, but would prefer to keep the *.php ending.
      If you have further questions, please do not hesitate to ask.
      Thank you in advance for your help.

      Best regards
      ce90
×
×
  • Create New...