maba Posted May 30, 2013 Share Posted May 30, 2013 Hi, with this foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); echo "<li$class><a href='$url'>$language->title</a></li>"; } can we have switch menu not "fully" translated? Not Italian English French but Italiano English Français for all languages.. Thanks, Marco Sorry for the formatting, but seems forum has some problems with Safari and preview.. 1 Link to comment Share on other sites More sharing options...
Soma Posted May 30, 2013 Share Posted May 30, 2013 You define them yourself in the languages you created. Link to comment Share on other sites More sharing options...
maba Posted May 30, 2013 Author Share Posted May 30, 2013 Do you suggest to translate: IT Title Italiano->Italiano Title English->Italiano Title French->Italiano and not IT Title Italiano->Italiano Title English->Italian Title French->Italien ? Ok for menu but in this way language are "never" translated.. Maybe.. Thanks Soma Link to comment Share on other sites More sharing options...
Soma Posted May 30, 2013 Share Posted May 30, 2013 Yeah I think a german will have real problem with your second method Link to comment Share on other sites More sharing options...
maba Posted May 30, 2013 Author Share Posted May 30, 2013 Ok, try this solution. But better if we can add something like this echo "<li$class><a href='$url'>$language->titleInThisLanguage</a></li>"; or not? Link to comment Share on other sites More sharing options...
Soma Posted May 30, 2013 Share Posted May 30, 2013 You mean like this? If you set the "title" field to PageTitleLanguage you can already. Link to comment Share on other sites More sharing options...
maba Posted May 31, 2013 Author Share Posted May 31, 2013 Yes, this is my previous configuration. But when you see any EN page you see "German" in switch menu.. Link to comment Share on other sites More sharing options...
Wanze Posted May 31, 2013 Share Posted May 31, 2013 @moreweb If you need another translation just for the switcher, add a new text field to the language template e.g. 'titleSwitcher': foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); echo "<li$class><a href='$url'>$language->titleSwitcher</a></li>"; } Link to comment Share on other sites More sharing options...
maba Posted May 31, 2013 Author Share Posted May 31, 2013 @wanze Not needed in my opinion.. I need something like: foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); $title = wire('page')->localTitle($language); echo "<li$class><a href='$url'>$title</a></li>"; } Link to comment Share on other sites More sharing options...
Wanze Posted May 31, 2013 Share Posted May 31, 2013 Well, the method localTitle doesn't exist. So you have at least 2 easy solutions: 1) Like I said: Add the textfield to language template and insert the title you want to appear in every language. 2) Code something, e.g. // Keys are IDs of languages... you could also use name $localTitles = array( 1234 => 'Italiano', 1235 => 'English', 3147 => 'Francais', ); foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); $title = $localTitles[$language->id]; echo "<li$class><a href='$url'>$title</a></li>"; } Link to comment Share on other sites More sharing options...
Soma Posted May 31, 2013 Share Posted May 31, 2013 No need to add another field, you already have PageTItleLanguage on the language template (see my screenshot). If not change the "title" field to language text field. There you can translate the language titles, you don't need to change anything in your code or setup. Yes, this is my previous configuration. But when you see any EN page you see "German" in switch menu.. Ok, but that what you want!? Translated language titles, so in english it will show all language titles in english. Link to comment Share on other sites More sharing options...
maba Posted May 31, 2013 Author Share Posted May 31, 2013 In frontend, I want this menu for each language.. always the same: Languages: - Italiano - English - Deutsch But i don't want edit language page in this way (probably I needed the translation in other section of the site) so I want maintain this but in the menu I want se the language title localized. Last wanze's code do the job but why use the array when we just have all data? Link to comment Share on other sites More sharing options...
Wanze Posted May 31, 2013 Share Posted May 31, 2013 @Soma As far as I understand, he has this setup already but wants to output always the same titles in every language. Meaning: Leaving the titles translated BUT not use the translated title in the language switcher ;-) Try this: foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); $title = $language->title->getLanguageValue($language); echo "<li$class><a href='$url'>$title</a></li>"; } 2 Link to comment Share on other sites More sharing options...
Soma Posted May 31, 2013 Share Posted May 31, 2013 Well then just enter the same for each language.. no need to go fancy.. Link to comment Share on other sites More sharing options...
Wanze Posted May 31, 2013 Share Posted May 31, 2013 Of course, but I guess the translated titles are used in another place of the site, just not in the switcher. Link to comment Share on other sites More sharing options...
Soma Posted May 31, 2013 Share Posted May 31, 2013 I dont get it really... Link to comment Share on other sites More sharing options...
maba Posted May 31, 2013 Author Share Posted May 31, 2013 @Soma As far as I understand, he has this setup already but wants to output always the same titles in every language. Meaning: Leaving the titles translated BUT not use the translated title in the language switcher ;-) Try this: foreach($languages as $language) { if(!$page->viewable($language)) continue; // check if page not published for this language $url = wire('page')->localUrl($language); $title = $language->title->getLanguageValue($language); echo "<li$class><a href='$url'>$title</a></li>"; } @Wanze Error: Call to a member function getLanguageValue() on a non-object @Soma Required by customer. Link to comment Share on other sites More sharing options...
Soma Posted May 31, 2013 Share Posted May 31, 2013 No problem, I just confused by terminology I guess. This should get you there: $language->getUnformatted('title')->getLanguagevalue($language->id) 5 Link to comment Share on other sites More sharing options...
maba Posted May 31, 2013 Author Share Posted May 31, 2013 It works well! Thanks guys 1 Link to comment Share on other sites More sharing options...
adrian Posted September 18, 2016 Share Posted September 18, 2016 I know this topic is old, but I came across the exact same issue in my first ML site. I am surprised that what @maba was looking to do is not more common or talked about. It seems like the only logical option in my opinion. Anyway, soma's solution did the trick - it's all about that getUnformatted method so that getLanguageValue has an object to work with. I did want to clarify though that it is in fact getLanguageValue rather than getLanguagevalue - note the case of the "V". It doesn't prevent it from working because methods in PHP are not case sensitive. Also can of course just use the $language object instead of the id. $language->getUnformatted('title')->getLanguageValue($language); It does seem a little unnecessarily complicated though. 1 Link to comment Share on other sites More sharing options...
BitPoet Posted September 18, 2016 Share Posted September 18, 2016 @adrian: that's why there's a Page::getLanguageValue method as well, and since $language is an descendant of Page, you can say: $langtitle = $language->getLanguageValue($language, "title"); 1 Link to comment Share on other sites More sharing options...
adrian Posted September 18, 2016 Share Posted September 18, 2016 Thanks @BitPoet - that is much cleaner. Another question along these lines. Is it OK to re-title the "default" language as "English" ? I assume I should keep the name as "default", but I feel like it would be nice to title it properly and even localize it when the user is viewing the site in Portuguese. Is that something you guys do, or should I stick with doing this for the frontend language switcher: $langtitle = $language->name == 'default' ? 'English' : $language->getLanguageValue($language, "title"); Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 18, 2016 Share Posted September 18, 2016 Only the name for the default language does need to stay. The title can be freely changed to the title of the language which actually is your default. 1 Link to comment Share on other sites More sharing options...
adrian Posted September 18, 2016 Share Posted September 18, 2016 Thanks guys! So in case anyone else new to all this ML stuff is interested, this is what I ended up with which shows the title of the current user's language localized to their language and all of the other available languages with the title localized to their language. For displaying the currently selected language: $user->language->getLanguageValue($user->language, "title"); Then for showing the other languages in the switcher: foreach($languages as $language) { if(!$page->viewable($language)) continue; // is page viewable in this language? if($language !== $user->language) echo '<li><a hreflang="'.$language->iso.'" href="'.$page->localUrl($language).'">'.$language->getLanguageValue($language, "title").'</a></li>'; } I obviously left out the <ul> container and the <li> class details in this example, but you get the idea. 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