Jump to content

Blank language in Image Description


Recommended Posts

I've setup a multi language site where the default behaviour is to not show a field if there is no translation available. For normal fields like text or textarea there  is an option under "details" how to address empty translations.
But I also use image descriptions, and there this option is missing. So the image description always shows the main language, if there is no secondary.
Does anyone know how to avoid that?

Link to comment
Share on other sites

Ok. I figured it out, but that was a tough one. Processwire adds languages to image description in a very unexpected way to say the least. If you have two languages the image array has three possibilities:

1.both languages description are filled.

( 
[description1031] => Description for LanguageID 1031
[description] => Description for main Language 
) 

2. only the secondary language is filled: 

( 
[description1031] => Description for LanguageID 1031
[description] => 
) 

3. only the main language is filled: 

( 
[description1031] => Description for main Language
[description] => Description for main Language
)

So in the third case an fallback is created which automatically fills the secondary language with the main language description field if empty. Neat if you want to show the main language if no description exists but not so handy if it is supposed to stay empty.

The only way I found to bypass that behaviour is a quite bumpy function, but at least it works:



function getLocalizedDescription($image, $user) {
    $lang = $user->language;

    // Safely get all available keys as an array
    $imageData = $image->getArray();

    $descDefault = $imageData['description'] ?? '';

    if ($lang->isDefault()) {
        return trim($descDefault);
    }

    $descKey = 'description' . $lang->id;
    $descTranslated = $imageData[$descKey] ?? null;

    // Only return translated description if it's truly set and different
    if ($descTranslated !== null && trim($descTranslated) !== '' && trim($descTranslated) !== trim($descDefault)) {
        return trim($descTranslated);
    }

    return '';
}

 

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