Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The new translatable text syntax in twig templates is custom (different from the regular t('String') that was in phptemplate themes).
Drupal 7:
<?php if ($logo): ?>
<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home" id="logo">
<img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" />
</a>
<?php endif; ?>
Drupal 8:
{% if logo %}
<a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home" id="logo">
<img src="{{ logo }}" alt="{{ 'Home'|t }}"/>
</a>
{% endif %}
The strings to parse for are 'String'|t.
Comment | File | Size | Author |
---|---|---|---|
#11 | 2011442-11.twig-translatables.patch | 6.06 KB | s_leu |
#8 | 2011442-8.twig-translatables.patch | 5.78 KB | coltrane |
#6 | 2011442-6.twig-translatables.patch | 6.13 KB | ksenzee |
#2 | 2011442-2.twig-translatables.patch | 4.13 KB | ksenzee |
Comments
Comment #1
ksenzeeMaking progress on this. It works fine except that the regex doesn't support escaped quotes ('Here\'s an apostrophe'). Will post a patch when that works.
Comment #2
ksenzeeHere's a working patch. In my testing it handles all of the following correctly:
Comment #3
Gábor HojtsyThis looks fabulous! Can you also include a sample twig file with some proper and improper uses? The tests directory has such modules and JS as samples. Other than that, amazing :)
Comment #4
Gábor HojtsyComment #5
Gábor HojtsyComment #6
ksenzeeAdded a test file and some tests, which pass locally.
Comment #8
coltraneRe-roll #6 without D8 API support following #2040115: Initial Drupal 8 API support to exclude old things getting in
Comment #9
Gábor HojtsyYay, looks great, committed!
Comment #10
Gábor HojtsyComment #11
s_leu CreditAttribution: s_leu commentedAdding a reroll of the the patch for 7.x-2.x
Comment #12
Gábor HojtsyIs this applying the current naming scheme for functions in this branch?
Comment #15
SebCorbin CreditAttribution: SebCorbin commentedPorted to 7.x-3.x as it has been branched directly from 6.x-3.x