Jump to content

Language Selector


mrs
 Share

Recommended Posts

Hi all,

Referring to this post, website for Setai.

At that site, let's say when you viewing the English Contact page, when you click the 中文 (Chinese), the content of the Contact page would change to Chinese immediately, and vice versa.

I have multilingual set up on my site and it works properly, but I like this effect instead of giving a EN | ZH link to go back to the homepage each time.

Anyone care to help a noob who don't know much about coding?

Link to comment
Share on other sites

Hi mrs,

Are you using a language gateway approach like on the example website? e.g. /en/contact or /de/contact where /en and /de are gateways that change the language.

If you have a language gateway setup you could use something like this :


//what is the current language
$lang = $user->language;

//this line sets the language to sites default. 
//"default" is the name of the sites primary language, in this case it's English
$user->language = $languages->get("default");

echo "<li><a href='/en{$page->url}'>EN</a></li>";

//this sets the language to German, so you can get the pages URL in German
$user->language = $languages->get("de");

echo "<li><a href='/de{$page->url}'>DE</a></li>";

//this sets the language back to current one again
$user->language = $lang;
  • Like 1
Link to comment
Share on other sites

Hi mrs,

Are you using a language gateway approach like on the example website? e.g. /en/contact or /de/contact where /en and /de are gateways that change the language.

If you have a language gateway setup you could use something like this :

Hi Michael,

Yes, I am using the language gateway as described by you. But where should I put the code in? I mean which file? :unsure:

Link to comment
Share on other sites

Hi epeisa,

Exactly I was using the Example 3 to successfully implement the multilingual site.

Now what I want, as stated in my first post, is that I need the change of a page from the current language to another language for the same page. If you click the language in that website you would understand what I mean.

I do think that I do not need additional module, so I thought Michael's suggestion is a good one. But I just do not know where to apply those code.

Link to comment
Share on other sites

May I know how could I apply the "highlight" of the top navigator (class='on' in the code below) for the language selector also?

<?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>";

}
Link to comment
Share on other sites

Forgot you were asking where to put it, sorry. But glad you found it out.

Now to make the current active try this:


//what is the current language
$lang = $user->language;

//this line sets the language to sites default.
//"default" is the name of the sites primary language, in this case it's English
$user->language = $languages->get("default");
$activeClass = $lang->name == 'default' ? ' class="on"' : '';
echo "<li><a$activeClass href='/en{$page->url}'>EN</a></li>";

//this sets the language to German, so you can get the pages URL in German
$user->language = $languages->get("de");
$activeClass = $lang->name == 'de' ? ' class="on"' : '';
echo "<li><a$activeClass href='/de{$page->url}'>DE</a></li>";

//this sets the language back to current one again
$user->language = $lang;
Link to comment
Share on other sites

Forgot you were asking where to put it, sorry. But glad you found it out.

Now to make the current active try this:


//what is the current language
$lang = $user->language;

//this line sets the language to sites default.
//"default" is the name of the sites primary language, in this case it's English
$user->language = $languages->get("default");
$activeClass = $lang->name == 'default' ? ' class="on"' : '';
echo "<li><a$activeClass href='/en{$page->url}'>EN</a></li>";

//this sets the language to German, so you can get the pages URL in German
$user->language = $languages->get("de");
$activeClass = $lang->name == 'de' ? ' class="on"' : '';
echo "<li><a$activeClass href='/de{$page->url}'>DE</a></li>";

//this sets the language back to current one again
$user->language = $lang;

It works partially.

Hmm... it works for "de", but does not work for "default". Anyone know why?

Link to comment
Share on other sites

It works partially.

Hmm... it works for "de", but does not work for "default". Anyone know why?

Hmm, it does work for the default.

You might have to tell a little more about your languages. You might have to compare it against "en"?

Link to comment
Share on other sites

Hi Soma,

Please check the site [note: link removed].

When you are in the Chinese site, take note that the top navigation would highlight both the page and the language name.

However in English (default), it won't highlight both.

Regards

mrs

If eng is the default, it should work. SInce you can't change the default language name, there's no reason why it shouldn't work.

You can try to output the language name instead of the title to verify.

...
$user->language = $languages->get("default");
echo "<li>LangName:{$lang->name}</li>";
$activeClass = $lang->name == 'default' ? ' class="on"' : '';
echo "<li><a$activeClass href='/en{$page->url}'>EN</a></li>";
...

And see what is in LangName: ...

Or you could post the code you're using.

hmm...

<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>";

}
//what is the current language
$lang = $user->language;
//this line sets the language to sites default.
//"default" is the name of the sites primary language, in this case it's English
$user->language = $languages->get("default");
$activeClass = $lang->name == 'default' ? ' class="on"' : '';
echo "<li><a$activeClass href='/en{$page->url}'>(ENG)</a></li>";
//this sets the language to German, so you can get the pages URL in German
$user->language = $languages->get("hans");
$activeClass = $lang->name == 'hans' ? ' class="on"' : '';
echo "<li><a$activeClass href='/hans{$page->url}'>(中文)</a></li>";

//this sets the language back to current one again
$user->language = $lang;

?></ul>

Since I follow the Example 3:.... of the Multi-language field, I only created a page called "en" and used the language-gateway.php as the template.

Above is the code that I am using.

Thanks for your help.

You can try to output the language name instead of the title to verify.

...
echo "<li>LangName:{$lang->name}</li>";
...

And see what is in LangName: ...

I did that.

Result is:

a) when in Chinese page, LangName:hans

b) when in English page, LangName:[empty]

Any idea?

Ok, looking at example 3 from the documentation.

I think the default language gateway page in your case should be named "default" in the name and not "en". This is why the language isn't found and it works because if no language is defined it will show the default language anyway. So it's a little tricky to find out.

You could do this in the gateway to avoid it :


// check what gateway page is accessed, $page->name being the name of the gateway page
if($page->name == 'en') $langname = "default"; // if default language
else $langname = $page->name; // if not default, we get the actual page name

$user->language = $languages->get($langname);

$path = '/';
foreach($input->urlSegments as $segment) {
$path .= $segment . '/';
}
$mypage = $pages->get($path);
if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();
echo $mypage->render();

This should make it work.

Thank you Soma for spending the time to find out this tricky problem and solving it for me. I am also appreciate everyone of you for spending time reading and answering to my questions. :)

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Hi all,

I come back again on the same topic with another problem that I just recovered.

If a page's status is "Hidden: Excluded from lists and searches", the url in breadcrumb , sidebar etc. would not automatically attached the /en/ or /hans/ to it. It only shows default link without the "language". I did not test if it works for other status. May I know what should I do to make it work under such situation?

Also, it is impossible to "view" the page except in default language when a page is unpublished. Is that intentionally?

Link to comment
Share on other sites

Hi all,

I come back again on the same topic with another problem that I just recovered.

If a page's status is "Hidden: Excluded from lists and searches", the url in breadcrumb , sidebar etc. would not automatically attached the /en/ or /hans/ to it. It only shows default link without the "language". I did not test if it works for other status. May I know what should I do to make it work under such situation?

Not sure I understand.

Since there's quite a couple ways to make multilanguage sites, one would need to know how you're using multilanguage, what is the setup, any modules you're using....

Generally PW doesn't automatic add language segments to your urls (depending on what you use). Not sure what's about "hidden" pages you experience.

Also, it is impossible to "view" the page except in default language when a page is unpublished. Is that intentionally?

You only see unpublished pages in the frontend when logged in as superuser.

"view", do you mean the button when editing a page? Usual multi-language setup PW does only know the default url of a page, and loads that when clicking "view". So you would have to add /en/ manually when loading the page, or click on your language switch to see other languages.

--

PS: Since PW multi-language implementation is meant for the backend primarily, it does lack some multi-language features for frontend that is not covered.

That's why the most flexible and recommended setup is to use separate page tree's to create multi-language websites.

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