mrs Posted September 15, 2012 Share Posted September 15, 2012 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 More sharing options...
Michael Murphy Posted September 15, 2012 Share Posted September 15, 2012 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; 1 Link to comment Share on other sites More sharing options...
diogo Posted September 15, 2012 Share Posted September 15, 2012 You can try this module http://modules.processwire.com/modules/language-localized-url/ It's still in beta, but it seems to work very well. Link to comment Share on other sites More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 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? Link to comment Share on other sites More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 You can try this module http://modules.proce...-localized-url/ It's still in beta, but it seems to work very well. Hi diogo, I read that module before but I thought the function is changing the URL language from one to another which is what I don't need. Or am I misunderstand about feature of that module? Link to comment Share on other sites More sharing options...
Soma Posted September 15, 2012 Share Posted September 15, 2012 You're all right there mrs! The module is to create speaking language urls, which is best practice regarding SEO. BTW Are you female? Not that it matters. Link to comment Share on other sites More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 Hi Soma, Yes, female. I read that module and it seems complicated to me. I will read again to try to understand it. Thanks. 1 Link to comment Share on other sites More sharing options...
apeisa Posted September 15, 2012 Share Posted September 15, 2012 Have you read this already: http://processwire.com/api/multi-language-support/multi-language-fields/ If you don't need localized urls, then no need for additional module. Link to comment Share on other sites More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 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 More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 Hi everyone, Thank you for your help. I implemented codes provided by Michael at the right place and have it works perfectly. Thank you very much! Link to comment Share on other sites More sharing options...
mrs Posted September 15, 2012 Author Share Posted September 15, 2012 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 More sharing options...
Soma Posted September 15, 2012 Share Posted September 15, 2012 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 More sharing options...
mrs Posted September 16, 2012 Author Share Posted September 16, 2012 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 More sharing options...
diogo Posted September 16, 2012 Share Posted September 16, 2012 BTW Are you female? Not that it matters. Refreshing, right? Link to comment Share on other sites More sharing options...
Soma Posted September 16, 2012 Share Posted September 16, 2012 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 More sharing options...
mrs Posted September 16, 2012 Author Share Posted September 16, 2012 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"? Soma, I think I PM you the site so you can take a look at it. Link to comment Share on other sites More sharing options...
mrs Posted September 16, 2012 Author Share Posted September 16, 2012 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. 1 Link to comment Share on other sites More sharing options...
ryan Posted September 16, 2012 Share Posted September 16, 2012 I think this is the first time I've seen ProcessWire (and the LanguageSupport module) used to deliver content in Chinese. Nice to see. Link to comment Share on other sites More sharing options...
mrs Posted October 4, 2012 Author Share Posted October 4, 2012 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 More sharing options...
Soma Posted October 4, 2012 Share Posted October 4, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now