• Resolved Aldo

    (@ab_lu)


    Hello

    How can we extract strings from blade template files?

    I.e. 404.blade.php contains the following code:

    @extends('layouts.app')
    @section('content')
    @include('partials.page-header')
    @if (! have_posts())<x-alert type="warning">
    {!! __('Sorry, but the page you are trying to view does not exist.', 'my-domain') !!}
    </x-alert>
    {!! get_search_form(false) !!}
    @endif
    @endsection

    The file is being picket up. I checked this here

    As for your documentation, as long the string is in __(); it should work.
    If I change the line to this, it works:

    <?php __('Sorry, but the page you are trying to view does not exist.', 'my-domain'); ?>

    Is there any way to tell Loco Translate to pickup strings like in my situation to?

    Thanks,
    Aldo

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

    (@timwhitlock)

    The short answer is “no”, but there may be a workaround.

    I’m not familiar with Blade templates, but I have a vague recollection of being asked this before. I can’t remember the outcome, but it’ll be on this forum somewhere.

    Loco Translate’s string extraction uses the PHP tokenizer directly on the source code. It has no capability to parse a custom template syntax. However, all the tokenizer needs is an opening <?php symbol and any __ will be picked up as a function call, even if the syntax before and after is totally invalid, and even if the opening tag is in a comment. I’ve done this in the past with twig templates. Just adding #<?php was enough for force the tokenizer to extract the function calls. Twig however doesn’t use a .php extension, so there is no risk of the web server executing the code.

    Thread Starter Aldo

    (@ab_lu)

    Thanks!
    About the other Blade related question – it was me 😉

    Is there a way to hook into the tokenizer so it picks up other openings?

    Plugin Author Tim W

    (@timwhitlock)

    You can’t hook into PHP’s tokenizer, other than to prefix the input string, and there are no hooks in my plugin for you to do that externally.

    The only solution would be for me to do what I do for .twig files, but for .blade.php files. Is this the convention for blade templates to always be *.blade.php?

    Thread Starter Aldo

    (@ab_lu)

    Yes, it is *.blade.php

    Thanks!

    Plugin Author Tim W

    (@timwhitlock)

    Have a go with the current dev version and see if it works for you.

    This adds the same hack as works for twig, which is just to prepend the source with an opening tag.

    Looking at your code, I could have replaced the {!! tags instead, but I don’t know enough about the templating system to know if this would be a better route.

    Thread Starter Aldo

    (@ab_lu)

    Thanks a lot. Works perfectly.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Extract strings from blade templates’ is closed to new replies.