Jump to content

Multiple trees: set one tree as root


u-nikos
 Share

Recommended Posts

Hey all,

I'm developing a multilingual website with the following tree structure:

- root

    - en

        - about-us

    - nl

        - over-ons

But, because "nl" is the main language, I want to set the "nl" page as the website root (essentially stripping out the "/nl/" part within URLs).

For example:

http://www.website.nl/over-ons/ = page found

http://www.website.nl/nl/over-ons/ = page not found

http://www.website.nl/en/about-us/ = page found

http://www.website.nl/about-us/ = page not found

Is this possible with ProcessWire or do I need to modify Apeisa's Multisite module to enable this functionality?

Greetings,

Niek

Link to comment
Share on other sites

Unless you prefer separate trees for each language, you might want to consider the new LanguageSupportPageNames module (included with the core), which will let you accomplish that URL structure pretty easily. If you try it out, use the one from the dev branch, which is a little more up-to-date, as this module is in development. To accomplish the structure, you would do this:

  1. In Language support, assume that "nl" is your "default" language. So edit the "default" language and change the title to Nederlands or whatever the right spelling is. Add another language and name it "en", with title: "English". 
  2. Install both the LanguageSupportFields and LanguageSupportPageNames modules (in that order), as they are not automatically installed with LanguageSupport. Though they are included with the core, so all you have to do is click "install".
  3. Change title field to be of type "TitleLanguage" and all relevant text fields to use TextareaLanguage or TextLanguage. 
  4. Edit your homepage and click to "settings" tab. Make the "name" field be blank for Nederlands, and make it "en" for English. Check the box to make English active, if necessary. Save. 
  5. Edit your About Us page, and again click to the "settings" tab. Make the default page name "over-ons" and the English page name "about-us". Do the same with any other pages.

Keep in mind that the "name" field is optional for all pages. When left blank, it will just use the default (nl) name instead. But it should still be able to determine the proper language based on the URL, so long as you've configured the language names at the homepage.

  • Like 2
Link to comment
Share on other sites

If you have a drop down list on the top right corner of the homepage where visitors can select a language, or when using little country flags where visitors can click on to select a language, how do you link from them to the pages in the selected language ?

Link to comment
Share on other sites

If you have a drop down list on the top right corner of the homepage where visitors can select a language, or when using little country flags where visitors can click on to select a language, how do you link from them to the pages in the selected language ?

It would depend on what method of multi-language support you are using. If using LanguageSupportPageNames, you could do this:

foreach($languages as $language) {
  $url = $page->localUrl($language); 
  echo "<a href='$url'>$language->title</a>";
} 

or this

$saved = $user->language; 
foreach($languages as $language) {
  $user->language = $language;
  echo "<a href='$page->url'>$language->title</a>";
}
$user->language = $saved; 
  • Like 3
Link to comment
Share on other sites

Basicly, you can just change language of user and call $page->url

$language = $user->language; //save current lang
foreach($languages as $lang){ // cycle languages
    $user->language = $lang; // switch user language
    echo $lang->name . ' : ' . $page->url . '<br/>'; // echo url
}
$user->language = $language; // put back current language

There's other way with new LanguageSupportPageNames module you could get a language urls of a certain page

$page_names = $modules->get("LanguageSupportPageNames");

$de = $languages->get("de");
echo "de url: ".$page_names->getPagePath($page, $de);
  • Like 2
Link to comment
Share on other sites

Thanks for the quick response and suggestion Ryan. This particular project requires separate tree's, but I'm sure the LanguageSupportPageNames module will come in handy some day!

Guess I'll need to modify the Multisite module then?

Link to comment
Share on other sites

That is one way to go - or at least see how to modify the paths.

Although I would much more prefer having /nl/ on the urls (I don't see anything bad in that). Modifying the path can get messy.

Link to comment
Share on other sites

  • 3 weeks later...

Hi Ryan -

The following method for switching languages works great thank you:

$saved = $user->language; 
foreach($languages as $language) {
  $user->language = $language;
  echo "<a href='$page->url'>$language->title</a>";
}
$user->language = $saved; 
 

What does the conditional statement look like if I don't have a different language only for that particular page? The snippet above will show the languages anyway, but point to the same URL, right? How can I avoid that and either link the customer to a hard coded page (such as /home) or just hide the options to switch lanugages on that page only?

u-nikos, what was the final approach you chose for your project?

Tks

jw

Link to comment
Share on other sites

What does the conditional statement look like if I don't have a different language only for that particular page? The snippet above will show the languages anyway, but point to the same URL, right?

I recommend using this method (requires 2.3 dev branch): 

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // language marked not available
  $url = $page->localUrl($language);
  echo "<a href='$url'>$language->title</a>"; 
}
Link to comment
Share on other sites

  • 4 weeks later...

Hi..

Ryan..  I used this snippet (founded in many post), it works (url are localized) but I got many

Exception: Method Page::localUrl does not exist or is not callable in this context
 

Why?

2.3 dev obviously.

Thanks,

Marco

Link to comment
Share on other sites

Yes, version 0.0.6.

Localized URL works well, but I got the error.

User: guest

Error:
Exception: Method Page::localUrl does not exist or is not callable in this context (in /home/customer/www/wire/core/Wire.php line 320)

#0 /home/customer/www/site/templates/includes/mainHeader.php(31): Wire->__call('localUrl', Array)
#1 /home/customer/www/site/templates/includes/mainHeader.php(31): Page->localUrl(Object(Language))
#2 /home/customer/www/site/templates/_out.php(81): include('/home/nsgroup/s...')
#3 /home/customer/www/wire/core/TemplateFile.php(140): require('/home/nsgroup/s...')
#4 [internal function]: TemplateFile->___render()
#5 /home/customer/www/wire/core/Wire.php(359): call_user_func_array(Array, Array)
#6 /home/customer/www/wire/core/Wire.php(317): Wire->runHooks('render', Array)
#7 /home/customer/www/wire/modules/PageRender.module(328): Wire->__call('render', Array)
Link to comment
Share on other sites

I'm not sure why localUrl isn't showing up there and haven't been able to duplicate. But I have several updates to commit to the dev branch soon so interested to see if the issue resolves from there. Also interested to know if anyone else has seen this particular error?

Link to comment
Share on other sites

Hi Ryan.. thanks. I found the problem. I receive the error when in the page there is a 404 error.

One image missing? One error.

Three images missing? Three errors.

But.. why?

If you have a bit of time :) .. Can you help me here? :D

Link to comment
Share on other sites

Hi Ryan, in your opinion.. Is there a fast work around for this problem?

Example:

http://novasiria.it/en

Error page:

http://novasiria.it/en/http404

Wrong link:

http://novasiria.it/en/test

Follow the last link and menu breaks the page rendering 

Unable to complete this request due to an error. Error has been logged. Administrator has been notified. 

due to the same error listed above.

Thanks,

Marco

Link to comment
Share on other sites

It looks like your URLs are lacking trailing slashes. I would try to enable trailing slashes and see if that fixes the issue. I only set my templates to omit trailing slashes for certain instances, like for a /sitemap.xml template, for example. I've not done extensive testing of the language-support modules on templates that are set to exclude trailing slashes, so it's possible there may be issues there. 

Link to comment
Share on other sites

Ryan as mentioned in an other thread about urls it looks like if language page names is installed it doesnt redirect to trailing slashes as without module. Maybe related?

Link to comment
Share on other sites

Ryan as mentioned in an other thread about urls it looks like if language page names is installed it doesnt redirect to trailing slashes as without module. Maybe related?

Not sure I get it. What are the situations where it redirects to a URL without a slash?

Link to comment
Share on other sites

Try http://novasiria.it/en or http://novasiria.it/en/ and got same result (works), like http://novasiria.it/en/http404 or http://novasiria.it/en/http404/ (works).  Same thing for test example url (error).

Without the switch language menu all works well, and with test or test/ we got the 404 error page.

Error notifies are sent for any image not found (just for test), when crawler bot runs or when we call a not existent page.  

  • Like 1
Link to comment
Share on other sites

Thanks for the update Ryan.. 

But seems that it not solve my problem.

Menu is broked by localUrl().

<?php
    foreach($languages as $language) {
      if(!$page->viewable($language)) continue; // check if page not published for this language
      $class = "$language" == "$user->language" ? " class='active'" : "";
      $url = wire('page')->localUrl($language);
      $title = $language->getUnformatted('title')->getLanguagevalue($language->id);
      echo "<li$class><a href='$url'>$title</a></li>";
    }

I can solve the problem in this way

$url = (wire('page')->template == 'http404') ? wire('page')->localUrl($language) : wire('pages')->get('/')->url; 

but how to obtain the correct homepage urls? (domain.it for default, domain.it/en for english, ..)

If you try novasiria.it/TEST/ you can see only novasiria.it in ITA and ENG menu.

Thanks,

Marco

Link to comment
Share on other sites

I understood the slash issue. But for the rest of it, I still don't have a full picture of the issue here or how to duplicate it locally. Does the method referred to here in the "or this" part not work? When you say the localUrl() breaks the menu, what do you mean specifically– still the "localUrl does not exist" error message? 

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...