Hi and sorry, but I can't find a way to translate the custom link text. Where can it be translated?

To make it more complex, the field is used in layout builder ;)

Comments

Anybody created an issue. See original summary.

anybody’s picture

danflanagan8’s picture

Hi @Anybody,
This is something that I've thought about before. If using tokens, I *think* the link text will be translated.

If using a static value for the link text, it will not be translated. However, I know it's possible to do that through Config Translation, even though there's not a special UI for field formatter config.

I think we should change this to a feature request and make this work. Here's an issue for the "Fixed text link formatter" module that might help as a very close model: #3089691: Translate configured link text through Config translation

My understanding is that translation in Layout Builder only becomes a problem when you start using custom blocks. As long as this formatter would be used for a field block in Layout Builder, translation should work "normally".

anybody’s picture

Category: Support request » Feature request

Hi and thank you very much for your quick reply!

I just had a look at #3089691: Translate configured link text through Config translation and it uses dirty interface translation as a workaround. We shouldn't do this.

I tried with latest patch from #2546212: Entity view/form mode formatter/widget settings have no translation UI but the button text is still untranslated while I translated it in the UI... do you think code modifications are required? This issue sounds like it's just the missing UI... but I don't know why it doesn't work for us then.

Here's the related getSetting() call:

// Make the render array.
      $elements[$delta] = [
        '#type' => 'link',
        '#title' => $this->getSetting('link_text'),
        '#url' => Url::fromUri(file_create_url($file->getFileUri())),
        '#options' => $options,
        '#cache' => [
          'tags' => $file->getCacheTags(),
        ],
      ];

Cache is cleared.

danflanagan8’s picture

@Anybody, thanks for your insightful comments on the approach taken in #3089691: Translate configured link text through Config translation. I totally agree now that we should instead try to be compatible with #2546212: Entity view/form mode formatter/widget settings have no translation UI.

I am uploading a patch that solves the most basic incompatibility. The patch changes the schema for the field formatters here such that the link text is marked as text instead of string.

Once I made that change, I was able to use https://www.drupal.org/files/issues/2020-02-05/2546212-119.patch to translate the link text through the manage display UI. It looks like it even works with Layout Builder!!! (My Drupal version is 8.9.3-dev.)

This approach seems to work especially well when tokens are not being used. If there are tokens in the link text, you likely need the file/image field to be translatable in order to get the correct language for the tokens.

Please let me know if this approach works for you. And thanks for your continued help with this module. I realized that you are the one who posted an earlier patch for translating tokens correctly.

danflanagan8’s picture

anybody’s picture

Hi and thanks a lot! Yes that looks GREAT! :)

I'd suggest to use "label" instead of text:

The two most important built-in translatable types are 'label' for one-line text input and 'text' for multiline text input.

https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-...

Also see https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-...

That makes perfect sense, great work!

Thank you :)

danflanagan8’s picture

Good call! Here's a new patch with label instead of text.

anybody’s picture

Status: Active » Reviewed & tested by the community

Wonderful! That works perfectly for us also in layout builder!

As I see absolutely no risk - RTBC! :) Thank you so much for the quick solution! Btw. the token issue should be a follow-up if there is a problem at all. Typically token should also use the current page context. I didn't test it, but this patch won't make things worse.

  • danflanagan8 committed f5e4638 on 8.x-1.x
    Issue #3159900 by danflanagan8, Anybody: How to translate link text?
    
anybody’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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