If you render a file in a language other than the current content language of the page (e.g. by passing in the $langcode parameter to file_view_file()), the alt and title text will display in the wrong language.

This is because the File Entity module sets $file->alt and $file->title based on the current page language in hook_file_load().

The simplest fix is just to override that when the file is being viewed, in the case where the languages differ.

Note: I'm experiencing this issue with an older version of the File Entity module. I haven't been able to reproduce it with the latest dev version since I currently don't see alt/title text rendering at all in the latest dev version. (Might be a configuration issue; I haven't dug into it.) But from looking at the code it looks like the same bug would exist there too.

Comments

David_Rothstein’s picture

Status: Active » Needs review
StatusFileSize
new1.63 KB

Here is a patch.

fabianx’s picture

Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community

RTBC - looks great to me.

joseph.olstad’s picture

I also noticed this issue. See the approach I took (a small, fairely straightforward core patch)
#2835135: image formatter needs to handle alt/title from file entities on images for multi language support

I'm thinking of committing either the patch to file_entity by @David_Rothstein
#2473621: Alt and title text don't appear in the correct language when viewing a file in a language other than the current page language

OR
the one by @das-peter :
#2806081: translated Title and Alt text fix, use optionally with entitycache and entity_translation

different approaches , but both look good. I'm leaning towards the @David_Rothstein approach because it is maybe slightly simpler? not sure.

joseph.olstad’s picture

Ok, I tested the patch and confirmed that it works (although I did need to do a cache clear, which is probably expected).

However, the das-peter patch also works and involves slightly less code. Both work, they're both legit, however I have to choose and I choose the das-peter patch (keeping in mind that I'm also rejecting my core patch which involves even less code but I'd rather not have to deal with core issues , so, das-peter it is (and I think is the best solution, but either way Rothstein patch or das-peter look ok, time will tell, maybe I'll have to revert and go with this later, but for now, das-peter patch is going >IN< as this needs to be fixed)).

again, for background info, see my issue summary here:
#2835135: image formatter needs to handle alt/title from file entities on images for multi language support

see das-peter issue for the commits.
#2806081: translated Title and Alt text fix, use optionally with entitycache and entity_translation

joseph.olstad’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
joseph.olstad’s picture

I will credit myself, David Rothstein as well on the fix in the other issue with das-peter as the author.

David_Rothstein’s picture

Status: Closed (duplicate) » Needs review
StatusFileSize
new2.24 KB

This issue doesn't have anything to do with Entity Cache, so the commit in the other issue didn't fix it.

It is possible to reproduce using the latest File Entity code, with the following steps:

  1. Enable the Locale module and add a new language (e.g. Spanish).
  2. Enable the Entity Translation module and configure it to allow files to be translatable, then go to Manage Fields on the file entity type and configure the alt/title fields to be translatable.
  3. Enable the Token module so that alt/title will actually be rendered (see #2114793: Alt and title tokens do not render when token module is not installed).
  4. Use the File Entity module to upload an image and add alt and title text (e.g. "english alt" and "english title"). Then add a Spanish translation for the file and set the alt/title for those to something different (e.g. "spanish alt" and "spanish title").
  5. From a page that is otherwise displayed in English, run code like the following (assuming the file you uploaded before has a file ID of 1):
    $fid = 1;
    $file = file_load($fid);
    $build = file_view_file($file, 'full', 'es');
    $output = drupal_render($build);
    

    Examine the output and notice that the alt/title is displayed in English, not Spanish as expected.

Here's a quick reroll of the earlier patch to take into account the changes in #2806081: translated Title and Alt text fix, use optionally with entitycache and entity_translation. I haven't looked at this in a long time, but I think it's probably still correct, and testing it out the patch works as expected (the above scenario now renders the alt/title in Spanish). It does result in file_view_file() modifying the $file object that was passed in to it, but I don't think that's necessarily a problem.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed quickly the above patch by @David_Rothstein, it looks good. Either way I tested both patches previously and they both worked for me so I cannot see how this would cause any regression.

A bit perplexing, it could be that we're using the entity_translation module when testing. It appears that David_Rothsteins patch has better coverage so lets take it.

joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

I'll tag a new release, because this is pretty important functionality for multilingual implementations, it should work out of the box.

joseph.olstad’s picture

tagged and released 7.x-2.10 that has this fix
Thanks again David!

David_Rothstein’s picture

Great, thanks!

To clarify, this probably isn't a bug that people ran into too often (even on multilingual sites) because normally if you are displaying a file in e.g. Spanish, it is on a page that is also in Spanish, and in that case it already worked correctly. We ran into the issue only when working on some pretty complex functionality that involved previewing translated content on an otherwise-English page. But either way, it's still nice to have a new release.

joseph.olstad’s picture

Hmm, this functionality isn't working for me in my test environment. in my english side I'm mostly getting french titles and in french sometimes getting english titles.
something is wonky.
I'm running a bunch of tests right now to figure this out.

Funny because previously I had tested both and they looked ok. This recent patch is slightly different though. going to run a bunch more tests.

joseph.olstad’s picture

might be due to some extra 'language' undefined data in my tables. investigating.

joseph.olstad’s picture

data looking ok
mysql> select entity_type, bundle, entity_id, language, field_file_image_alt_text_value from field_data_field_file_image_title_text where field_file_image_alt_text_value like '%table Bob%';

data:

| file        | image  |       0 |    140570 |      140570 | en       |     0 | The representable Bob the Builder
| file        | image  |       0 |    140570 |      140570 | fr       |     0 | Le représentable Bob le bricoleur

however result is french in both english and french pages.

joseph.olstad’s picture

nevermind, cache clear after updating code....

fixed, on my end.

joseph.olstad’s picture

oh wait, actually, cache clear now breaks french.

This is what I saw previously before I implemented my core patch for this.
#2835135: image formatter needs to handle alt/title from file entities on images for multi language support

joseph.olstad’s picture

ya sure enough, as soon as I patch core with this patch:
#2835135-1: image formatter needs to handle alt/title from file entities on images for multi language support
it fixes the problem.

joseph.olstad’s picture

Status: Fixed » Active
joseph.olstad’s picture

Status: Active » Fixed

Ok, while the use case that das-peter and David_Rothstein are dealing with is likely fixed, the use case that I am using is NOT fixed, and still requires the core patch that I wrote.

for this reason I am re-openning the related core patch issue. Ideally I'd prefer NOT to be using a core patch for this however I have no choice as I don't have a better working solution and the core patch IS working nicely for us.

I will open a new issue for file_entity for my use case to try to resolve the fix in file_entity instead of in core.

joseph.olstad’s picture

Rather than open a new issue for file_entity I just updated an existing issue, this has been a known issue for quite some time;
#2665960: Image Alt and Title displayed in the wrong language IF field format is not "rendered file"

if anyone experiences this issue they can use the core patch (works fine) until a better solution is found.
#2835135-1: image formatter needs to handle alt/title from file entities on images for multi language support

joseph.olstad’s picture

it is easy to get mixed up , with my use case a cache clear will make things 'appear' to be fixed, until you load the page in another language other than the language of the cache clear.

joseph.olstad’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.