Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

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
}
Posted

Yay! Glad it works :)

You can change $page->setOutputformatting(true|false) to $page->of(true|false) to make it cleaner

  • 2 weeks later...
Posted

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?

Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...