- 🇩🇰Denmark nicklasmf
'#title' => $this->$prefix_text, ['%terms' => $link],
on line 148 doesn't seem to be correct. - First commit to issue fork.
- @kkalashnikov opened merge request.
- Status changed to Needs review
almost 2 years ago 9:12am 3 March 2023 - Status changed to Needs work
almost 2 years ago 3:24pm 31 March 2023 - 🇮🇹Italy apaderno Brescia, 🇮🇹
Also,
$this->t('@link_text', ['@link_text' => $this->configuration['link_text']]);
does not make sense, as it does not translate$this->configuration['link_text']
. What translators would see as string to translate is @link_text, which will be left as it is. I see now that is true of this:
$prefix_text ? $this->t('@prefix_text', ['%terms' => $link], ['@prefix_text' => $this->configuration['prefix_text']]) : $link,
But this seems to work?:
$this->t('@link_text', ['@link_text' => $this->configuration['link_text']]);
see: https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/...
- Status changed to Needs review
almost 2 years ago 8:16pm 31 March 2023 Whoops. needed the title set to this:
'#title' => !empty($this->configuration['prefix_text']) ? $combined_text : $link,
I'll use inter-diffs moving forward.
- Status changed to Needs work
almost 2 years ago 8:13am 1 April 2023 - 🇮🇹Italy apaderno Brescia, 🇮🇹
$this->t('@label', ['@label' => $field_definition->getLabel()])
does not translate the field label either, for the same reason. - Status changed to Needs review
almost 2 years ago 8:17am 1 April 2023 Can you please point to documentation that explains why it won't be translated, and what a better alternative is?
It's simply placeholder being passed as an argument.
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Rend...
- 🇮🇹Italy apaderno Brescia, 🇮🇹
The reason placeholders are used is to avoid translators need to translate many strings. For example, Hello, @username! avoids that translators need to translate Hello, apaderno! Hello, tonytheferg!, where they would anyway not translate the username.
On localize.drupal.org, translators just see the placeholders, not what will replace the placeholders.
- Status changed to Needs work
almost 2 years ago 7:26pm 1 April 2023 - 🇮🇹Italy apaderno Brescia, 🇮🇹
- $link_text = $this->configuration['link_text']; + $link_text = $this->t('@link_text', ['@link_text' => $this->configuration['link_text']]);
The existing line is already correct.
- '#title' => $this->t($prefix_text, ['%terms' => $link]), + '#title' => $this->t('@prefix_text', ['%terms' => $link], ['@prefix_text' => $this->configuration['prefix_text']]),
Placeholders are passed as second parameter of
$this->t()
, not as two array and two different parameters.
The placeholder is @prefix_text, not %terms.
Also, translators cannot translate a string containing a single placeholder, since translators are not supported to touch the used placeholders, except eventually to change their order in the string, but that requires two placeholders in the string. This line doesn't conform with drupal standards.
'#title' => $this->t($prefix_text, ['%terms' => $link]),
139 | WARNING | [ ] Only string literals should be passed to t() where | | possible 148 | WARNING | [ ] Only string literals should be passed to t() where | | possible
- 🇮🇹Italy apaderno Brescia, 🇮🇹
$link_text = $this->configuration['link_text'];
is already correct.t()
and$this->t()
cannot translate dynamic content. So for the sake of understanding better, why would creating functions make those fields translatable like done in this issue ✨ Translate text for multi-language stores Fixed ?
- Status changed to Needs review
almost 2 years ago 11:25pm 2 April 2023 reroll off the latest dev. Let me know what you think. :)
- 🇮🇹Italy apaderno Brescia, 🇮🇹
Yes, configuration objects are another matter. They are translatable, as long as each value is marked as such.
Sounds good. I'll commit this and handle the translation in that issue. Thanks for helping me wrap my head around this!
-
tonytheferg →
committed 1ec4cb9c on 8.x-1.x
Issue #3331489 by tonytheferg, Jitendra_Kumar, apaderno, NicklasMF: Fix...
-
tonytheferg →
committed 1ec4cb9c on 8.x-1.x
- Status changed to Fixed
almost 2 years ago 6:59pm 3 April 2023 - 🇮🇹Italy apaderno Brescia, 🇮🇹
As a side note, for a string containing placeholders, that does not need to be translated, there is the
FormattableMarkup
class.$formattable = new FormattableMarkup($string_with_placeholders, $placeholders_values_array); $string_with_replaced_placeholders = (string) $formattable;
- Status changed to Fixed
almost 2 years ago 9:37pm 3 April 2023