• Resolved Hypolythe

    (@hypolythe)


    Hi!


    I am a theme developer, and I ran into an issue with the file scan, specifically regarding twig files.
    I have some translations that are not scanned when in the middle of some html, but they are scanned if placed at the beginning of the file.

    As you will see, the first line is a string scanned.
    This same string is also present on line 8, but is not scanned if I remove the first one.

    Here is the content of the file :

    {{ _x("Search anything", 'Search placeholder', 'text-domain') }}
    <div id="search_form" class="panel" data-url-slug="{{ search.url_slug }}" data-panel-id="search_form" data-component="search_form">
    	<div class="panel__wrapper search_form__wrapper">
    		<div class="panel__inner search_form__inner">
    			<form class="search_form__form" action="{{ SITE_URL }}" method="get">
    				<button class="panel__close" type="button" data-panel-toggle="search_form" aria-label="{{ _x("Close the search", 'Search', "text-domain") }}">{{ svg('icon-close') }}</button>
    				<div class="search_form__input_wrapper">
    					<label class="sr_only" for="search_form_input">{{ __("Search", 'text-domain') }}</label>
    					<input class="search_form__input" type="search" id="search_form_input" name="s" placeholder="{{ _x("Search anything", 'Search placeholder', 'text-domain') }}">
    					<button class="search_form__button" type="submit">
    						<span class="loader"></span>
    						{{ __("search", 'text-domain') }}
    						{{ svg('icon-search') }}
    					</button>
    				</div>
    			</form>
    		</div>
    	</div>
    	<button class="panel__overlay" aria-label="{{ _x("Close the search", 'Search', "text-domain") }}" data-panel-toggle="search_form"></button>
    </div>

    Do you have any idea what might be the issue here?


    Thank you very much for your help.
    Have a very nice day.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tim W

    (@timwhitlock)

    Hello.

    The string on line 9 (or 8 when first line removed) is actually not scanned at all. This a limitation of the parser which cannot see strings inside HTML attributes. The same issue applies to the string on line 19.

    This is actually the same limitation as documented by Timber with regard to using POEdit 1.x. The reason is that Loco Translate’s string extractor doesn’t include a proper Twig parser. It’s a hack that uses the PHP tokenizer to find function calls, and the opening " of the attribute breaks this syntax.

    You can make the function to be visible to Loco Translate by placing a Twig comment that forces the end of the string literal, but I don’t know how this affects the actual Twig rendering of your template.

    Example hack: <a href="{#"#}{{ __('foo') }}">....

    It’s probably better to assign the translation to a variable instead. I don’t know the syntax off the top of my head.

    I hope this helps. It’s unlikely that you’ll see a proper Twig parser in the plugin any time soon as this is very rarely requested feature. As a theme developer, you’re better off using other, better tools to extract your strings as part of your packaging processes.

    Thread Starter Hypolythe

    (@hypolythe)

    Thanks a lot @timwhitlock, this is very clear!

    Thanks for the detailed answer and the suggestions, I think my way to go will be to used a dedicated variable at the top of the files then, as suggested in the timber documentation.

    Have a very nice day and good job on the plugin, as it is very well made.

    • This reply was modified 1 year, 11 months ago by Hypolythe.
    Plugin Author Tim W

    (@timwhitlock)

    Thank you.

    I could potentially try a different hack, which would be to replace all {{...}} patterns with <?php...?> before parsing. I’m unsure though if this would create more points of failure. If I have any success I’ll post back.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Scanning twig files’ is closed to new replies.