This is a parallel patch to #367595: Translatable fields. The idea in that patch is to load all translations of a $thing, then decide which one to display at render time. So for fields, we'd load all values like any other multiple value field, except keyed by language, and then in rendering only render $node->field_name[$language]['view'] as compared to the standard $node->field_name[0]['view'].

I'm not yet sure where this decision should live, but here's a possible option which adds support for a #translate property to $elements in drupal_render_children(), and allows only the child in the current language to be rendered if that's set.

Doing it exactly like this would be quite inflexible - we wouldn't have a chance to implement a fallback solution there for untranslated content or whatever. So there might be a need for drupal_alter('children', $element) in there instead - so that locale/translation module could handle anything more complex themselves.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

- Changed to #translatable

- Allow multiple fields.

sun’s picture

- Wrong key order.

sun’s picture

Title: Add 'translate' support to content rendering » Add #translatable support to content rendering
dmitrig01’s picture

Let's use this somewhere. How about nodes?

catch’s picture

dmitrig01 - see the fields patch for where it'd be used first, although they need syncing up.

The main remaining issue here is we don't have a way to do fallbacks (i.e., if a field isn't translated into Slovak, show it in Czech, if there's no Czech translation, show it in English). I don't think this needs to be handled by core necessarily, but it ought to be possible for contrib to hook in. I think it could probably be brute-forced in hook_node_build_alter() or somewhere else, but ideas welcome. Notwithstanding that, the main thing missing here is tests, will get started on that today.

yched’s picture

One drawback - possibly not major - is that you build and keep in memory all the render arrays for all languages. On pages that list 10s of nodes, that could make a noticeable memory occupation.

Then only one language actually gets themed, but for formatted text fields, for instance, the check_markup step happens *before* theming ('sanitize' step), for all fields (and all languages, in the 'translatable fields' approach) when the $object is viewed.

Granted, that last point can also be seen as a drawback in current D7 fields (and CCK D6) : if a field is 'hidden', we still run check_markup on it...

plach’s picture

subscribe

catch’s picture

@yched, yes there's a risk of more memory issues on sites with either a large number of translations, or long texts to be translated, however I think it's a minor issue compared to either rewritten or additional database queries to fetch translations themselves. It's also a problem that's only going to appear on sites with really heavy translated content, whereas extra joins/queries would hit any site with translation enabled even if the quantity of translated content itself is quite light.

With check_markup() I've got one possible approach which might cut a lot of calls to it down - by stuffing the sanitized data in the node/field cache attached to the loaded fields/nodes instead of in the filter cache - opened an issue for this here #369011: Performance: do check_markup() in text_field_load().

catch’s picture

Status: Needs work » Needs review
FileSize
8.6 KB

Here's a slightly modified patch, with added tests for both single and multiple fields. Works fine. So the remaining question is whether drupal_render() is the right place to do this, or if we need to be slicing out unwanted translations earlier on (i.e. at a stage before check_markup() might get called at all). If we do want to move things earlier in the process, IMO we still need a generic place to do this, even if that's just a hook.

But minus that discussion, this is working OK now, so CNR.

PS. Who sneaked windows line breaks into Fields in Core? :(

catch’s picture

FileSize
4.19 KB

Minus the field patch.

moshe weitzman’s picture

If we wanted to keep drupal_render() ignorant about translations, we could add a #pre_render callback during array building and that callback tosses out all the stuff we don't intend to render. Perhaps not a significant difference either way.

catch’s picture

Hi Moshe, spoke with sun and chx, and they said the same thing, so I'm working on that as we speak.

catch’s picture

Status: Needs review » Postponed

For now I'm going to merge this into #367595: Translatable fields since the issues are too entangled to track separately.

sun’s picture

Status: Postponed » Closed (duplicate)

I agree. This issue was good for initial distinction and brainstorming, but neither of both issues can go in without the other. Thus, marking as duplicate.