Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

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 ?

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

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
Posted

Thanks for the examples, this gives me something to work on dynamic code with api instead of using static links that I was used to.

Posted

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?

Posted

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.

  • 3 weeks later...
Posted

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

Posted
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>"; 
}
  • 4 weeks later...
Posted

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

Posted

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)
Posted

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?

Posted

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

Posted

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. 

Posted

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?

Posted
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?

Posted

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
Posted

Thanks, I understand now. I have corrected the issue so that now it should redirect to the slashed version when accessed without a slash (dev branch 2.3.1)

Posted

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

Posted

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? 

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.
×
×
  • Create New...