Jump to content

General process for a multi-language website


Recommended Posts

Hi everyone :)

I'm new to PW, discovered it yesterday and playing with it today and I have to admit that I amazed, It powerfull but still so simple, I don't why I had never heard of it !

Anyway, I'm wondering how you would create an international website (2 languages, french and english). My only requirement is that the language bust be present in the URL.

At first I thought I could create 1 child to Home for each language and "duplicate" each page in both languages but this seems a bit too much, I'm pretty sure you could tell me how I could add a variable at the beginning of the uri or something like that. Or maybe another solution :)

Anyway great CMS/Framework here, I'm pretty sure I'll use it for a lot of websites (my portfolio is already on the way).

Link to comment
Share on other sites

Hi HammField,

welcome to the forums.

I did exactly what you are thinking about: keeping two structures, one for each language. It may seem like much more work than having one structure with multiple languages, but ultimately, it isn't.

If I created larger multilanguage site with PW, I would do it the same (actually, I did it on my portfolio) but with minor addition: use API to create the second (and ultimately, any other) language. Create one side of tree for your main language, with everything in place, then create one 'tools-duplicate' page with special template, where you'll just cross-examine your 'finished language' branch and duplicate it into root – so you don't have to create it manually.

Possible addition to this is to have different templates for each language's pages. I did this with my portfolio items: since I'm lazy to add the very same pictures to pages in both languages, my secondary language just takes images from primary language pages.

However, when creating your way of tackling this, keep in mind, that cross-referencing pages is mostly done in one or two lines, so don't be afraid of it (I believe we had some posters here having really hard time, because they didn't realize this). It's the real power of PW – having any page on the site and/or any of it's fields just two lines away.

Link to comment
Share on other sites

Thanks for your reply (didn't expected it to be that fast).

Actually I'm not worried much, I'm just thinking about the end admin (won't be me) he'll still have to create the pages twice or duplicate them instead of filling 2 fields, one for each language. If there is no other way, I'll do that but I'd rather not have to use a duplicated tree.

Is there, by any chance a way to add a variable in the beginning of the uri like (fr|en). For short is there a way to add something at the begining of the uri that PW won't "watch" to match the routing ? I think this would be the simplest solution, maybe not to code but for the end user/admin...

Link to comment
Share on other sites

Another option for you (although I'm not sure about all of this :D)

create your page structure like this:

- fr
- en
- site

And set 'en' and 'fr' pages to template called e.g. 'language-pages' with 'Allow URL segments' on (you can find this in your template settings). Then you'll hide your 'site' branch from the users not logged in the PW (so, everyone :D), which will contain all the pages with fields for both languages.

and then, something like this (heave pseudocode) will go into your 'language-pages':

  $language = $page->url;
  $urlSegments = array();
  for($i=1;$i<=3;$i++)
    if($input->{'urlSegment'.$i}) { $urlSegments[]=$input->{'urlSegment'.$i}; }
  $url_to_look_for = '/'.implode($urlSegments).'/';
  $lookup_page = $pages->get('/site/'.$url_to_look_for);
  if($lookup_page){
    //go ahead and show the page
    $lookup_page->field('body_'.$language); //this will get e.g. body_en
  }else{
    //goto 404
  }

Also, I'm not sure how to go to 404 just like this (I would have to look it up) and until Ryan creates some function, that will give you url-segmented part, you have to use that ugly code posted above.

disclaimer: URL Segments work only for 3 segments, therefore you can have only 4 levels of pages! (index, children of index, subchildren and sub-sub-children). Anything deeper must be retrieved via API only (can't be used in URL)

Link to comment
Share on other sites

I'll need to read that carefully once again because I didn't understand everything, but will !

In the meantime I had an idea : I could add (fr|en) in the htaccess :

RewriteRule ^(fr|en)/(.*)$ index.php?it=$2 [L,QSA]

This way, PW's should work fine since it'll get what it would normally have (the uri) and I can get the language from the url.

I'm not familiar with everything (thought it's really simple, amazing) so I'll have to think about it again...

Link to comment
Share on other sites

I'm not really that used to .htaccess – it seems to follow some mysterious rules that are different for each server I work with, together with different regex flavors – I'm not capable of master it, so it's always a little fight for me, that's why I didn't give you that advice.

Also, in your example there is no way PW knows what language it should use [it should be index.php?it=$2&l=$1]

Link to comment
Share on other sites

This thread might have some more useful stuff too:

http://processwire.com/talk/index.php/topic,37.0.html

disclaimer: URL Segments work only for 3 segments, therefore you can have only 4 levels of pages! (index, children of index, subchildren and sub-sub-children). Anything deeper must be retrieved via API only (can't be used in URL)

I didn't realize there was a need for more, but I can definitely add more. I can see the value with what you are mentioning here.

Another possible way to handle a /en/ or /fr/ at the beginning would be to install ProcessWire in a directory named /en/. Then create a symlink on your file system called /fr/ that points to /en/. Your template code could examine the URL it's being loaded at to determine which version of content to show:

<?php
if(strpos($_SERVER['REQUEST_URI'], '/fr/') === 0) {
    echo $page->body_fr; 
} else {
    echo $page->body_en; 
}

I haven't actually tried it, but I think it would work.

Link to comment
Share on other sites

Glad you like ProcessWire! Nice work on the site.

The duplicated tree does seem like an easy to manage approach. Though if your pages are simple, and you only needed to support a couple languages, I would think that a duplicated field approach might also be good, because you could edit the two translations together on the same page. So you might have these fields in your template:

headline_en

headline_fr

body_en

body_fr

And then just display the one that corresponded to the language you wanted to show. To determine the language, you could use one of the approaches Adam mentioned, or the approach I mentioned in the previous message. Or, if you didn't mind having the en/fr segment at the end of the URL, you could just look at the first url segment:

/some/url/fr/

For this URL, the page is actually /some/url/ and the 'fr' part is what you added to the URL when generating it or linking to it.

<?php
if($input->urlSegment1 == 'fr') {
    $headline = $page->headline_fr; 
    $body = $page->body_fr; 
} else {
    $headline = $page->headline_en;
    $body = $page->body_en;
}

echo "<h1>$headline</h1>";
echo $body; 

Link to comment
Share on other sites

I didn't realize there was a need for more, but I can definitely add more. I can see the value with what you are mentioning here.

oooor, instead of hacking the core we can push the UI/multilinguality update a little (=instead of 6 months be ready in two months) :D

Another possible way to handle a /en/ or /fr/ at the beginning would be to install ProcessWire in a directory named /en/. Then create a symlink on your file system called /fr/ that points to /en/. Your template code could examine the URL it's being loaded at to determine which version of content to show:

This sounds so simple </irony>

Link to comment
Share on other sites

  • 2 months later...

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