Skip to content

Instantly share code, notes, and snippets.

@bikubi
Last active March 3, 2021 15:04
Show Gist options
  • Save bikubi/2cde05d02b3f407452848ce0e83b614e to your computer and use it in GitHub Desktop.
Save bikubi/2cde05d02b3f407452848ce0e83b614e to your computer and use it in GitHub Desktop.
Helper script for Roots' Sage starter theme: make WPML's String Translation see WordPress's l10n gettext function in PHP Blade templates, like {{ __('Foo', 'domain') }}
#!/bin/bash
# Scans blade templates for WordPress's gettext functions
# so WPML will pick them up for its String Translation.
# (WPML doesn't see something like {{ __('Hello World', 'sage') }}
# Put me into THEME/resources/assets/build
# so $fn will point to THEME/resouces and WPML can see it
# (and it's out of sage's way)
fn="../../gettexts-fake.php"
# prevent execution; doesn't matter for WPML
echo "<?php die;" > $fn
#NOTE the regex expects somewhat wellformed gettexts with a \w+ domain
# like __('Hello', 'domain')
find ../../.. -name '*.blade.php' ! -path '*/vendor/*' \
-exec grep -Po "\\b(__|_x|_e|_ex|_n|_nx|esc_html_[_ex]|esc_attr_[_ex])\\(.*?,\\s*[\"\']\w+[\"\']\\s*\\)" \{\} \; \
| sed -e 's/$/;/' >> $fn
cat "$fn"
@broskees
Copy link

broskees commented Mar 7, 2019

How do I execute this?

@koentjuh1
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment