aria-label cannot be translated
-
I have this HTML
<nav aria-label="{{ __('Main menu', 'lbo') }}">
I would like to extract and translate themain menustring, but it doesn’t work.
If I place{{ __('Main menu', 'lbo') }}directly in the HTML, it works.
Here is what I found when asking Claude:Root cause: Loco Translate’s Twig extractor works by prepending
<?phpto your entire.twigfile and running it through PHP’s real tokenizer (token_get_all()) — see gettext.php:334-335. It then looks for a bare__token followed by(.The problem: PHP’s tokenizer doesn’t know about Twig or HTML — it only knows PHP string syntax. When you write:
aria-label="{{ __('Menu principal', 'lbo') }}"the
"that opens the HTML attribute is indistinguishable, to the tokenizer, from a PHP string literal delimiter. So the whole"{{ __('Menu principal', 'lbo') }}"gets consumed as one single string token —__(never appears as a separate function-call token, so Loco never sees it. I verified this directly:T_CONSTANT_ENCAPSED_STRING: '"{{ __(\'Menu principal\', \'lbo\') }}"'This matches what’s in your working tree: nav-mobile.twig:27,95, pagination.twig:10, footer.twig:66, sidebar-nav.twig:2, template-hub.twig:8 — none of these have a
#:source reference inen_CA.po, confirming Loco can’t locate them.By contrast,
__()calls that sit in plain text content (not inside aattribute="..."HTML attribute) tokenize fine and are correctly extracted elsewhere in these same files.Fix: pull the translation call out of the attribute quotes with a
{% set %}, then just print the variable:{% set nav_label = __('Menu principal', 'lbo') %} <nav class="nav-primary-desktop" aria-label="{{ nav_label }}">This keeps the
__()call outside any HTML double quotes so the tokenizer sees__as a bare token again, and Loco extracts it normally.
You must be logged in to reply to this topic.