Jump to content

Multi language website with different structure


Faisal
 Share

Recommended Posts

Hello every body
 
I am starting my first project with ProcessWire for one of my websites newsflames.com But not only that!
Moving from simple code I have made to ProcessWire must also add value even this is only changing system step.
 
One of the new features I am planing to add in this step is to add multi language so the main categories structure wold be:
 
(English is default language in homepage and it's categories)
Home                                    /

-Politics                      /politics/

- Sports                      /sport/

- Arabic                      /ar/

-- Politics         /ar/politics/

-- Sports          /ar/sport/

- French                     /fr/

-- Politics         /fr/politics/

-- Sports          /fr/sport/

 
And so on..
 

All categories in website: (Politics Sports Business Health Technology Science) but in this example I use 2


I have download ProcessWire 2.3 from this site and after I install languages modules and tray all the ways I found in this site
 
*note. there is no check boxes!

But if I download the zip file from github it's all there plus some other stuff I think.
screen_shot_2013-07-26_at_8_52_07_am.png
 
 

However I still need to add content/pages in languages other than default (English) because some local news that in native language only!
So I can't have English as  mandatory!
 
simple Example:
 
Home

- Politics

-- Yahoo! News

-- Fox News

- Arabic

-- Politics

-- Yahoo! News

-- 123 News

- French

-- Politics

-- Yahoo! News

-- abc News

 
And so on..
 
So I came up with this and I wold say it's all there in PW, I did not invent any thing ;)
 
just for start duplicate the basic-page.php in to category.php and language-home.php and add theme from admin area and assign template with pages:
 
Home

-Politics                      category template

- Sports                      category template

- Arabic                      language-home template

-- Politics         category template

-- Sports          category template

- French                     language-home template

-- Politics         category template

-- Sports          category template

 
And so on..

I am using default installation with default profile in this example

In my case I want the main categories in the top of the page so lets change the topnav

Find in head.inc

            <ul id='topnav'><?php
            
                // Create the top navigation list by listing the children of the homepage.
                // If the section we are in is the current (identified by $page->rootParent)
                // then note it with <a class='on'> so we can style it differently in our CSS.
                // In this case we also want the homepage to be part of our top navigation,
                // so we prepend it to the pages we cycle through:
                
                $homepage = $pages->get("/");
                $children = $homepage->children;
                $children->prepend($homepage);
        
                foreach($children as $child) {
                    $class = $child === $page->rootParent ? " class='on'" : '';
                    echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
                }

            ?></ul>

Change to:

            <ul id='topnav'><?php
            
                // Create the top navigation list by listing the children of the homepage.
                // If the section we are in is the current (identified by $page->rootParent)
                // then note it with <a class='on'> so we can style it differently in our CSS.
                // In this case we also want the homepage to be part of our top navigation,
                // so we prepend it to the pages we cycle through:
                
                if ($page->rootParent  == "1006") { // Arabic page ID
                    $homepage = $pages->get("/ar/"); // Arabic page URL
                }
                elseif ($page->rootParent  == "1008") { // French page ID
                    $homepage = $pages->get("/fr/"); // French page URL
                }
                else {
                    $homepage = $pages->get("/"); // Homepage URL
                }
                $children = $homepage->children("template=category"); // show only pages using template category
        
                foreach($children as $child) {
                    $class = $page === $child ? " class='on'" : '';
                    echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
                }

            ?></ul>

Simple if condition but it's better to do something else like array or maybe add a field or maybe use the page URL to identity what and where? I don't know for now :)

Now a simple language list and we going to use the subnav style for this example but this time will copy it instead of changing it

Add this cod any where in the sidebar

                        $defaultlang = $pages->get("/"); // Homepage URL
                        $languages = $pages->find("template=language-home"); // show only pages using template language-home
                        echo "<ul id='subnav' class='nav'>";
                        $languages->prepend($defaultlang); // display default language in the list
                        foreach($languages as $language) {
                            $class = $language === $page->rootParent ? " class='on'" : '';
                            echo "<li><a$class href='{$language->url}'>{$language->title}</a></li>";
                        }
                        echo "</ul>";

It works just fine except that if you are in an English sub page like /sports the English link class will not be on (problem only in default language that if you want the css effects) // $class


This may not be good for you if you plan to go back later for the great language module especially for large data! I only have 6 pages for every language plus news sources and all the news is cached but not saved so I can go back to use languages modules any time I want in this website :P

Fell free to ask any thing about this

If you can help improve or have any ideas please do :)

Link to comment
Share on other sites

Hi, this is great. Thanks for taking the time to post, I'm sure this will be helpful to a lot of folks. 

Simple if condition but it's better to do something else like array or maybe add a field or maybe use the page URL to identity what and where?

I'm not sure I fully understand and I'm new to PW, so this may not be helpful. Maybe you can use different templates for different languages. For example...

make three templates with these names...

ar.php

fr.php

en.php

The template files mention above can display a table of contents or a blog style list of articles or a portal page (whatever you want). You can also create the template in the admin w/o the accompanying template file and it will disable access from the public to your urls. E.g. mysite.com/fr/ will return a 404, however mysite.com/fr/sports will be accessible. A module would work too.

Then you can use $page->rootParent->template to determine what lang you are using. This way you don't have to hard-code the ids. I would also turn the _init.php functionality in site/config.php and add the following switch there. Why? Because _init.php is called first before the template. You can get all the dirty work out of the way and ensure that something like a redirect happens before the overhead of loading up template files.

switch (str_lower($page->rootParent->template)) {
    case 'fr':
        $homepage = $pages->get("/fr/"); // French page URL
        break;
    case 'ar':
        $homepage = $pages->get("/ar/"); // Arabic page URL
        break;
   case 'en':
        $homepage = $pages->get("template=en"); // Homepage URL
        break;
   default:
        $session->redirect('/en/');
}
I'm not sure that even matters though. I don't think you'll need to identify the language if you are using root parent. You just need to print all of the links under the root parent. This will display a menu only for the current language. If you want to display a menu including all languages, use the switch statement without the breaks, put the foreach below into a function, then call the function that creates the links inside of each case. There are easier ways to print the full tree, but over time you may find a switch statement handy, because you can use logic specific to each language. 
foreach ($page->rootParent->children() as $child) {
$class = $page === $child ? " class='on'" : '';
    echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";
} 

I would also make my tree like this just to normalize the logic and it will take care of the class=on issue you were having:

Home
EN (en.php template)
  Politics (category template)
  Sports
AR (ar.php tempalte)
  Politics

  Sports
FR (fr.php)
  Politics
  Sports

Urls would look like:

mysite.com/EN/politics

mysite.com/AR/sports - for SEO I'd use the native language in the url though (which is probably what you are doing). E.g. mysite.com/ar/الرياضة 

The only caveat about this approach (regarding the page tree), you'll need to redirect the home page, which is probably fine. E.g. mysite.com must redirect to mysite.com/en. This is happening in the switch statement above. 

Link to comment
Share on other sites

Hi there, Thanks for sharing, And I am new to PW as well, and I am not a programer also, just want to share some code and ideas :)
 
What I meant is that I do not want to go back modify the code every time Site Admin or staff add a new language. (for others how use this way)

Using switch is much better than (if), also your site tree example is also much better for organizing.

Both ways still need to go back and edit the files to add new language, and the list will get longer every time adding new language, so I think we can say (Proof of Concept!) yet need to be improve
 
And the _init.php functionality is beautiful and I love it but I want to use default installation in this example.

In my (site map) case I don't have to worry about routing rules,  ;)  but that just me and what I need in my current project! :rolleyes:

Not forgetting the breadcrumb, search and the site map may needed to work with depending in what you need!
 
Again in my case I am not going to need the site map at lest for now, but I am going to use something similar to the Skyscrapers Site demo for the search. (in sshaw site tree example search will be easier)

I hope I covered your post for now :) and forgot to say that I will be working in the News Flames project few hoers at the weekends so I have setup a demo for this here http://a51.24at24.com/dev/pw/

maybe we can tray some thing else later.

Link to comment
Share on other sites

Hi there,

This week I have a flu so I did not do any thing any ware, but I try sshaw way in localhost and it works just fine

just the en redirect need do do something like

        $re = $config->urls->root.'en/';
        $session->redirect($re);
Link to comment
Share on other sites

OK, I just found the LanguageLocalizedURL module and gave it a try, and it's very cool and will do the same!

I liked it

$children = $homepage->children("language_published=$langpage");

more complex but also well covered and more support :)
If you are new to PW and read my topic you may want to consider LanguageLocalizedURL module.

Sorry for rushing up..

Link to comment
Share on other sites

Hi Ryan, Thanks for your interest.

Yes I did read that.
 
The only problem is the default language is always mandatory! (even in the dev branch)
 
What if we need to add page in other languages only without adding any thing in the default language
 
Another example (different from my current project) let say I am a tutor and I like to give course to (online course for every body and attended course for locals) so the site will look something like that:
 
 
Home (English for global)

- About

- Course

-- Online Course

- French (Let say French is my local language but I am using it in the web site as a second language)

-- About

-- Course

-- Online Course

-- Attended Course (since I will give attended course for people in my country only I don't want this page to be in English at all)

For now (in my localhost) I am using the LanguageLocalizedURL module plus template selector 

$children = $homepage->children("template=category, language_published=$langpage");

If I am in the right direction I will upload PW to my site soon (all the hard work is done and I am now finalizing and cleaning things up).

So do you have any advice?

Link to comment
Share on other sites

The only problem is the default language is always mandatory! (even in the dev branch)

You can treat it as a placeholder if you want to. There isn't anything that says you have to make it part of your site. But it generally makes sense to consider the default language the one that is most common and most populated in your site.

The nature of your statement makes me wonder if you might really be looking for a multi-tree language approach instead of multi-language pages? Using separate trees for each language is a fine way to go, and in fact the recommended way to go when you have different structural needs for each language. It's also the simplest one to develop, as it requires no multi-language modules to be installed. But it does mean maintaining each of your languages independently. 

For now (in my localhost) I am using the LanguageLocalizedURL module plus template selector 
$children = $homepage->children("template=category, language_published=$langpage");
If I am in the right direction I will upload PW to my site soon (all the hard work is done and I am now finalizing and cleaning things up).
So do you have any advice?

Unfortunately I don't have enough experience with the LanguageLocalizedURL module to offer any advice on it. I've not developed a site that uses it. I'm not sure if this module is still in active development?

Link to comment
Share on other sites

Hi, I appreciate taking the time reading and replaying my topic :)

Quote

The nature of your statement makes me wonder if you might really be looking for a multi-tree language approach instead of multi-language pages? Using separate trees for each language is a fine way to go, and in fact the recommended way to go when you have different structural needs for each language. It's also the simplest one to develop, as it requires no multi-language modules to be installed. But it does mean maintaining each of your languages independently.


It's true what you say, yet it is common to add pages in a secondary language only ;)
And I did what I did because I couldn't do it with in the core multi-language modules

Using the core language module "Languages Support - Page Names" much better than having multi-tree language!
Using multi-tree language = number of page x number of languages
Using core multi-language modules = number of page x 1

The only problem using core multi-language modules that there is no checkbox for the default language
* See the screenshot from dev branch ProcessWire 2.3.5

OK I under stand you can't do this for homepage and the "404 Page Not Found" page and maybe system pages also, but it is needed in all other public pages!

If the language checkbox is not checked even in the default language the page removed from navigation (PW already hide the page) and if somebody visit deactivated page will get 404 by default. (logically this page does not exist in this language)
We are talking here about pages under homepage but not the homepage it self


Ryan you are already my hero but if you do this you will be my multi-language hero :biggrin:

Thanks, and keep up the good work..

post-1825-0-48307700-1383072430_thumb.pn

Link to comment
Share on other sites

I use additional checkboxes for each language with a page field coming from the language pages. Then check for those in my code for navigation and init.php. works fine but agree that it would be nice feature. Although I don't know if even possible as default title and name are required when creating a page. This is by design of how language support works as its primary build for admin and not frontend where needs can vary allot and there will always be drawbacks and workarounds.

Link to comment
Share on other sites

Hi Soma, Apeisa

I use additional checkboxes for each language with a page field coming from the language pages. Then check for those in my code for navigation and init.php. works fine but agree that it would be nice feature. Although I don't know if even possible as default title and name are required when creating a page. This is by design of how language support works as its primary build for admin and not frontend where needs can vary allot and there will always be drawbacks and workarounds.

I didn't get your point exactly but I thought of making a checkbox filed manually with the "Languages Support - Fields" module but thin I fund it in LanguageLocalizedURL module!

So if this idea is good then the people who worked in LanguageLocalizedURL must did it better than me

what about using default language as a placeholder? always redirect to some other language if accessed.

oh, i am little slow today :)

I think this can be used in less cases :)

However the idea is simple

  1. Using "Languages Support - Page Names" module
  2. I still have to type the page title and URL for the page for the default language (placeholder) and can use default URL for secondary active language that don't have one
  3. It's must be possible to un check (deactivate) the default language for pages under Homepage
  4. If the language unchecked (deactivated) even in the default language the page link will be removed from navigation in that language
  5. if somebody visit a deactivated language page he/she will get 404 by default.

This is the behavior I expected from "Languages Support - Page Names" module at least in my point of view!

That's it, but implementing it would be something else ^_^

Link to comment
Share on other sites

LanguageLocalizedURL module isn't in active development anymore because we now have this functionality in core with LanguageSupportNames which is the way to go now. LanguageLocalizedURL was created because the lack of names per languages and created by mcmorry and me.

What I meant is using a page field that uses checkboxes as input to make languages active or not for a page. The input for the page field comes directly from the language pages found in admin (each language is a page). This enables you to turn off also the default language and at a small price. This page field then can be  used to not find or show pages which aren't active even the default language. It's just a matter of a few lines of code to show the 404 when a page is accessed that isn't active.

I get what you're saying but thelanguage system doesn't work like this and you always have to enter default title and name when creating a page. The way it is built I think it made the most sense for most use cases and it wasn't in from the start. It's a module that came to it and the default language is required because you can install and uninstall it. So there's so many things to consider and pros and cons, no language system is perfect and may not suits your requirements. But PW is enough flexible and customizable to work around it and give waht you need even when you have to enter the default lanugage title and name I think it's a small issue considering that PW gives you in term of multilanguage features. I and my client can live with it as it's not something of a problem that big.

I don't now what Ryan thinks and if it makes sense or is even easy possible to implement a "not have default" language require. Maybe it is easy and I'm not seeing it or there's a better workaround, but so far I have to use whats there and try to use it as best as possible.

If the project requirements fit, still the most flexible and best way to create multilanguage sites that can differ in structure and not are based on a default language should be built using separate branches for each language and connect them using page references. There was a module in development (Oliver) that would help managing sites like this but I don't know what happend to it as it doesn't seem to be active anymore since a long time. Building it this way enables you to have each language as it's own page and can be much more flexible and practical the way you can build your site and be free from restrictions coming from a language system that assumes things you might don't want.

Link to comment
Share on other sites

LanguageLocalizedURL module isn't in active development anymore because we now have this functionality in core with LanguageSupportNames which is the way to go now. LanguageLocalizedURL was created because the lack of names per languages and created by mcmorry and me.

Thank you for that, you and mcmorry just did what I am talking about

In the default language (the Title and URL) of the page is required.

Yet it is optional to activate the page or not in any language including default language 

Nice

* You may want to make homepage always having the default language always checked or

prevent deactivating it. but as you say it isn't in active development anymore

What I meant is using a page field that uses checkboxes as input to make languages active or not for a page. The input for the page field comes directly from the language pages found in admin (each language is a page). This enables you to turn off also the default language and at a small price. This page field then can be  used to not find or show pages which aren't active even the default language. It's just a matter of a few lines of code to show the 404 when a page is accessed that isn't active.

Actually I thought about something like that before thanks

I get what you're saying but thelanguage system doesn't work like this and you always have to enter default title and name when creating a page. The way it is built I think it made the most sense for most use cases and it wasn't in from the start. It's a module that came to it and the default language is required because you can install and uninstall it. So there's so many things to consider and pros and cons, no language system is perfect and may not suits your requirements. But PW is enough flexible and customizable to work around it and give waht you need even when you have to enter the default lanugage title and name I think it's a small issue considering that PW gives you in term of multilanguage features. I and my client can live with it as it's not something of a problem that big.

I love it when we come this far :)

yes indeed you always have to enter default title and name when creating a page.

What is the most use cases? yes Soma that is THE Question. and I have hundreds of scenarios to prove it, or I am just to ambitious.

Installing or uninstalling a major modules especially if it is involved with data structure must be came after careful study. you can't just decide no mater what method of the language behave for example, but not limited to:

  • multi-language interface with one language content (most SMCs do that by default) I never find that useful.
  • multi-language interface with dynamic multi-language content that can be managed from any language interface (like social networks)
  • evrey language interface display its language content (multi-language interface = multi-language content) and in this method we are also will have more than a way to do it.

Yes indeed, no language system is perfect yet I think that PW is a few steps away from been perfect in multi-language

As a hobby I usually take a peek in the source code  of the system I use but because my time is tight right now and Ryan make so easy to do your thing away from core files I didn't see what is under the hood of wire folder yet.

So I don't know if its possible or not, but from what I see from the UI I can tell how good it is.

I don't now what Ryan thinks and if it makes sense or is even easy possible to implement a "not have default" language require. Maybe it is easy and I'm not seeing it or there's a better workaround, but so far I have to use whats there and try to use it as best as possible.

If the project requirements fit, still the most flexible and best way to create multilanguage sites that can differ in structure and not are based on a default language should be built using separate branches for each language and connect them using page references. There was a module in development (Oliver) that would help managing sites like this but I don't know what happend to it as it doesn't seem to be active anymore since a long time. Building it this way enables you to have each language as it's own page and can be much more flexible and practical the way you can build your site and be free from restrictions coming from a language system that assumes things you might don't want.

Agree.

By the way for now I will be considering multi-tree language or what Soma referred to, this will take no time I think.

And have almost finish but I went to clean my html css code (it is a mess in the current website) and its done and add bootstrap and t is almost done.

Sorry I do 20 - 30 minutes in week end for Newsflames project that's why I cod not push it online yet.

10 minutes gos for global thinking making this idea works for evrey body not for my needs only.

I hope this topic stay active and be consider, not for my needs but for a lot of pebble

Thanks

Link to comment
Share on other sites

As I understand Soma point

  1. Install PW
  2. install Languages Support & Languages Support - Fields modules
  3. Add new languages ant update title body fields and other stuff...
  4. add new filed language_active
    1. Type page
    2. Label active?
    3. Dereference in API as Multiple pages (PageArray)
    4. Parent of selectable page(s) /admin/setup/ Languages
    5. Template of selectable page(s) language
    6. Input field type Checkboxes
  5. add "language_active" filed to templates

Now the coding part

Need to filter the navigation menu to show active pages in current language

And what you want if some one visit none active page (I wold give 404)

*language selector point always to language homepage

But I am sorry, I am stuck here so I will leave this part for some one professional if have the time or if I managed to do it later.

post-1825-0-75997100-1383431654_thumb.pn

Link to comment
Share on other sites

First off, I really think that what you are looking for is just multiple branches in your site tree, one for each language. You don't really need any language support modules installed for this. This is a pretty good way to go when you have the needs you've indicated. When your navigation needs are going to be significantly different across languages, multiple trees is probably the best way to go. 

But if you want to stick with language support and multi-language fields, then it looks to me like the LanguageSupportPageNames module (included with core dev branch) already does what you are trying to do. My understanding was that the main issue you ran into is that you have instances where you don't want the default language to be active. A couple ways to accomplish this would be 1) just consider the default language to be not in use (don't link to the default language version of any pages, and don't let the user choose that language); or 2) add your own "disable default language" checkbox to every page template and check for it when outputting navigation, etc. Basically, I think there are a lot of very simple solutions if you use the tools that are already there.

  • Like 2
Link to comment
Share on other sites

First off, I really think that what you are looking for is just multiple branches in your site tree, one for each language. You don't really need any language support modules installed for this. This is a pretty good way to go when you have the needs you've indicated. When your navigation needs are going to be significantly different across languages, multiple trees is probably the best way to go.

Yes of course, and it's also simple :)

Still knowing the alternatives help in making the right decision ;)

I may use this for News Flame (website with limited number of pages) and use another way for other website!

1) just consider the default language to be not in use (don't link to the default language version of any pages, and don't let the user choose that language);

Cool , I never thought of adding the primary language as any other languages!

I just test that out in clean installation

and this works fine except I still couldn't figure it out how to do something like throw the PW default 404 when visit none active page

maybe some code in header 

if current page language is not active throw new Wire404Exception();

I tray some code but nothing worked out.

2) add your own "disable default language" checkbox to every page template and check for it when outputting navigation, etc. Basically, I think there are a lot of very simple solutions if you use the tools that are already there.

Yep, that will do the trick, still need more coding that I wish I could offer for others but I will leave that for now since I am not pro code writer and I wasn't active even in code manipulation since i don't know maybe 2006. and to be honest at this time this is the only discussion board I am active in :-X

However I enjoyed and benefited from every post, and every opinion, and I hope any body reading this topic will too.

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