Jump to content

Check if translation exists


Nico Knoll
 Share

Recommended Posts

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?

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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

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

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