Nico Knoll Posted October 14, 2012 Share Posted October 14, 2012 Hi, is there a way to check if a translation exists? I want to display the languages flag as switcher only if a translation is available... Greets, Nico Link to comment Share on other sites More sharing options...
diogo Posted October 14, 2012 Share Posted October 14, 2012 What do you mean? if a language exists, or if a particular translation exists? Link to comment Share on other sites More sharing options...
Nico Knoll Posted October 14, 2012 Author Share Posted October 14, 2012 If a specific page is translated (the page you're on). Like: $page->hasTranslation('en'); Link to comment Share on other sites More sharing options...
diogo Posted October 14, 2012 Share Posted October 14, 2012 hm, I can be wrong here, but the problem is that pages are not translated, only fields are. You could check if one field is translated and assume that it means that the others are also. You can't check that directly because it will give you the default language value, it it doesn't exist. But you can do this (taken from here): $page->setOutputformatting(false); // turn off outputFormatting (if it's not already) $dutch = $languages->get('dutch'); // get the language we want $body = $page->body->getLanguageValue($dutch); // get the unformatted value in Dutch If $body is not empty, the translation exists... should work, no? 2 Link to comment Share on other sites More sharing options...
Nico Knoll Posted October 14, 2012 Author Share Posted October 14, 2012 OK, I'll have a try. Link to comment Share on other sites More sharing options...
Nico Knoll Posted October 14, 2012 Author Share Posted October 14, 2012 So here's the working code: $page->setOutputformatting(false); $en = $languages->get('en'); $title = $page->title->getLanguageValue($en); $page->setOutputformatting(true); if($title != '') { // do something } Link to comment Share on other sites More sharing options...
diogo Posted October 14, 2012 Share Posted October 14, 2012 Yay! Glad it works You can change $page->setOutputformatting(true|false) to $page->of(true|false) to make it cleaner Link to comment Share on other sites More sharing options...
landitus Posted October 29, 2012 Share Posted October 29, 2012 I'm trying a similar logic but with Alternate language fields. I have a technical PDF per language and would like to test out if a translation for that file exists and display de language of the PDF with a badge. How should I approach this? I don't want to test a specific field, cause with multiple languages, I don't know how many languages the client will add. I just need to know if the field "pdf_file" is in a certain language and which language is being displayed in. Does this makes sense? Link to comment Share on other sites More sharing options...
ryan Posted October 31, 2012 Share Posted October 31, 2012 The nice thing about language alternate fields is that there isn't really much going on behind the scenes, so it's pretty straightforward to perform the kind of detection you are asking about. Since each language is represented by a different field, you just have to check those fields: I just need to know if the field "pdf_file" is in a certain language and which language is being displayed in. if($page->pdf_file_french) { echo "PDF file is available in French "; if($user->language->name == 'french') echo "and pdf_file is the French version."; } 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