• I have this HTML <nav aria-label="{{ __('Main menu', 'lbo') }}">
    I would like to extract and translate the main menu string, 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 <?php to your entire .twig file 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 in en_CA.po, confirming Loco can’t locate them.

    By contrast, __() calls that sit in plain text content (not inside a attribute="..." 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.