Jump to content

Seperate news for each language - some problems...


doolak
 Share

Recommended Posts

Hi there,

i am trying to display seperate news for two languages - and my attempt to to this is as follows:

To render the multilanguage navigation i use the following code:

<?php
if($user->language->name != 'default') $segment = $user->language->name.'/'; 
    else $segment = '';
?>

So i have just one news-page "News", which is displayed as /news/ and news/en/ (German is the default language). That's why i had to use "english" in the page tree, because otherwise i am getting a 404 error when trying to switch to the english newspage.

The articles could be directly under this page, but i would like to have them sorted in english and german - so I am using this pagetree:

/news/
       deutsch/ <= page ID 1131
               newsarticle
               newsarticle
               newsarticle
       english/ <= page ID 1132
               newsarticle
               newsarticle

In the news template i use the following code for the selection of the different news:

if($user->language->name == 'default') {
$news = $pages->get("id=1131");
$stories = $news->children("limit=3"); 
}
else {
$news = $pages->get("id=1132");
$stories = $news->children("limit=3");
}

To cycle through the stories as follows:

foreach($stories as $story) {

// markup for displaying news...

}

// output pagination links:
echo $stories->renderPager(array(
    'nextItemLabel' => "»",
    'previousItemLabel' => "«",
));

So far it works fine - the different news are listed on the two different language versions of the news page (/news/ and /news/de/) - the articles are shown under /news/ and /news/de/ - but now i have two problems:

1. As the news articles are displayed under /news/deutsch/ and news/english/ the navigation always switches back to the default language, i guess because the url segment /en/ is missing then.

2. Another problem is the fact that the pagination does not work in this scenario. It is shown correctly but it does not work - the next page does not show up when using the navigation.

I can imagine that i have to define "baseUrl" but i cannot get it to work.

Maybe there is a much better solution for displaying seperate news for each language - if you have an idea, i would appreciate your help.

If my approach is ok in general, it would be great if you help me to fix the two problems i mentioned above.

Link to comment
Share on other sites

i had to use "english" in the page tree, because otherwise i am getting a 404 error when trying to switch to the english newspage.

You may need to enable URL segments for your template where this is being used. Setup > Templates > [your-template] > URLs 

1. As the news articles are displayed under /news/deutsch/ and news/english/ the navigation always switches back to the default language, i guess because the url segment /en/ is missing then.

If you've got code checking the user language, you want to make sure you set it somewhere. I am assuming that the $user is a guest user and not a logged in one (where they could have a predefined language setting). 

if($page->name == 'english') $user->language = $languages->get('en'); 

Also note that when you set $user->language, it's only going to be retained for the request. So if you want it to be remembered between requests, you'd want to track it with a session:

if($page->name == 'english' || $page->name == 'en') $session->language = 'en';
if($session->language) $user->language = $languages->get($session->language); 
Another problem is the fact that the pagination does not work in this scenario. It is shown correctly but it does not work - the next page does not show up when using the navigation.

I can imagine that i have to define "baseUrl" but i cannot get it to work.

baseUrl will only add confusion, so I would avoid that. It sounds to me like you need to enable page numbers for your template. Setup > Templates > [your-template] > URLs > page numbers

Link to comment
Share on other sites

baseUrl will only add confusion, so I would avoid that. It sounds to me like you need to enable page numbers for your template. Setup > Templates > [your-template] > URLs > page numbers

Yes, I had just enabled page numbers for the template which is used for the pages /deutsch/ and /english/ - i had to enable it for the page /news/ - so pagination works fine now.

You may need to enable URL segments for your template where this is being used. Setup > Templates > [your-template] > URLs 

I have this enabled, so for the general newspage /news/ this works fine - the default language shows /news/ and the english version shows /news/en/

It's just the problem that i have used those subpages for dividing in english and german newsarticles - the url of the article will be something like this: /news/deutsch/lorem-ipsum-dolor-sit or /news/english/lorem-ipsum - so maybe i have to add the url segment to the news-template to generate language-specific urls like /news/en/english - or i just have to add the url segment "english" to this code:

<?php
if($input->urlSegment1 == 'en') {
    $user->language = $languages->get("en"); 
}
?>

I have to try this out...

Link to comment
Share on other sites

Now i have tried this:

if(($input->urlSegment1 == 'en') || ($input->urlSegment1 == 'english')) {
    $user->language = $languages->get("en"); 
}

... but that does not work.

The URL is: /news/english/lorem-ipsum-dolor-sit-amet/  so this should be the first URL segment, but it does not keep the language. Any idea why this does not work?

I have tried the possibility with session, but then the language is not more changed when the user klicks the language switcher (Maybe the session should be destroyed then, but i don't know how to manage this...).

Link to comment
Share on other sites

It doesnt work because english is not a url segment but a real page.

Yes, you are right -  i just discovered this too and now I am trying this:

if($input->urlSegment1 == 'en') || ($page->parent->name == 'english') {
    $user->language = $languages->get("en"); 
}
 
It just seems that nevertheless i have to include the $segment part in the news template - will try it out and let you know if it works.
Link to comment
Share on other sites

Hm, no - this does not work either.

But i just recognized another point which also speaks against using the subpages /english/ and /deutsch/ for the different newsarticles:

I am using the pagetitle field with the type "PageTitleLanguage" - so on every page the optional second language is displayed. This would be a little confusing if i am using two seperate areas for english newsarticles and german newsarticles, because there would be no english title needed.

If i see it right, there is no possibility to add two types of the pagetitle to use  PageTitleLanguage on some pages and just PageTitle on others. So maybe it will be the best solution to have all newsarticles simple under /news/

This would make it possible to have one newsarticle in both languages - or just in one of them - simply by ading values to the multilanguage fields or leaving them empty. Would just be great if one could display somehow in the pagelist if this article is english or german - or both...

I will try if that could work...

Link to comment
Share on other sites

Well, right now before i want to try out the different approach with all articles together below /news/ - seperating them just by displaying DE, EN or DE/EN in front of the article list, i managed the other approach to work:

There is no need to use something like:

if($input->urlSegment1 == 'en') || ($page->parent->name == 'english') {
    $user->language = $languages->get("en"); 
}

 (which did not work anyway)

I just had to add the url segment to the article links in the news template like this:

<a href='{$story->url}$segment'>{$story->title}</a>

The url segment is already defined (for the navigation and other internal links) in the head.inc:

if($user->language->name != 'default') $segment = $user->language->name.'/'; 
    else $segment = '';

One just have to enable url segments for the newsarticle template, too...

So as far i can see this works - the only drawbacks i found  until now are:

1. If you are on the newspage of one language, where just those articles are listed and you choose one article to read more - the article is shown now correctly but if you change the language through the language switcher, the article is still displayed.

2. The "title" thing i described above - but if i understood apeisa right one could the normal title field plus a language alternate field instead of the multilanguage title field.

Nevertheless i will try out the other approach - having all newsarticles under /news/ - this would have some advantages like described in my previous post - and as you will usually would retreive a 404 error if the article is not existant in both languages when switching the language while watching an article, one could add a redirection to the newspage in this case.

I just have to figure out how one could handle it to display DE, EN or DE/EN in front of the articles in the administration...

Link to comment
Share on other sites

Yes, you are right -  i just discovered this too and now I am trying this:

if($input->urlSegment1 == 'en') || ($page->parent->name == 'english') {
    $user->language = $languages->get("en"); 
}
It just seems that nevertheless i have to include the $segment part in the news template - will try it out and let you know if it works.

It doesn't work because the page you're requesting it the /news/ page and not the article itself. Depends where this code lays, but assuming $page is the news page with urls segments, $page->parent->name would be?

Also it's a good practice to use if() else checks to see if a url segments is really found if(count($page->urlSegments)) or if($page->urlSegment1). Also it help if you output the vars, segments or page->id to debug and see what values you get and where you actually are.

Link to comment
Share on other sites

The current page where the code would have been useful was the newsarticle, so the url was /news/english/lorem-ipsum-dolor-sit 

I had added <?php echo $page->parent->name ?> to the newsarticle template to verify if the parent is "english" and it was - but nevertheless it did not work for changing the language. But as i described in my previous post at last it was not necessary to verify if "english" is the parent of the article - it was possible to add the url segment "en" at the end.

Link to comment
Share on other sites

It's somehow hard to follow you and there always seems to be some last important info missing. Also there is like 3 mixed. :D

- This doesn't work (code) with no further details

- How would it be done better, alternatives

- How do I use language fields and approaches

This would be a whole book about those subjects alone :)

I think you're on the right track to try both approaches. Separate tree for each language or one page multiple languages; Both is possible (quite easily) and each has it's various pros and cons. It's just it can get confusing at times, not only trying to help. It also depends on how you setup the rest of the site apart from /news/. Is the rest of the site multilanguage?

Multilanguage fields or alternate language fields. With the latter you could put them in separate "de","en" tabs and with the former you'd have them coupled together. 

If you have languages on one pages, you can't for example use the published features of PW and would have to implement something to define it and have additional code in templates. With separate trees you would have to somehow link them together using a page reference and it may makes it hard to edit and keep track, but its also possible.

Also always remember you can use checkboxes to do something like setting a pages available languages. Using this, you could then set the label displayed in the tree (template advanced settings) to show their title I think. Instead of "title" you'd have something like "title de_pub.label en_pub.label". Where de_pub would be the checkbox field for german.

Link to comment
Share on other sites

But title can never be empty can it?

Yes, that's right - but as i am using PageTitleLanguage the second language title can be empty and should not be displayed then.

So i want to verify first if the users language is different from default language and then display all articles which have a title in that language, somehow like this:

if($user->language->name != 'default') {
$stories = $page->children("title!= , limit=3");
}
Link to comment
Share on other sites

@ Soma: Sorry for the confusion and thank you for your suggestions!

Of course, i don't expect anybody to post a detailed "alternative" - i was just unsure in the beginning if i am absolutely on the wrong way.

After having posted my first post and had some answers i wanted to keep you informed about the things i found out myself - hoped this makes it easier to help me finding a solution.

At last there really seem to be both possibilities for the news: both languages in a seperate tree or both on one page. I managed it to get the first approach working but found some drawbacks for the "page tree approach", regarding a newspage.

So now i am trying the other approach...

For the main website i am using "one page multiple languages" - no seperate page tree.

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