Main menu

Extracting translatable strings from source code.

Here are some notes on Loco Translate's extraction tool and some common pitfalls:

  • Loco can extract specific text domain strings
    Your bundle must declare its correct text domain, or the string extractor won't extract anything. See configuration settings.

  • Avoid dynamic text domain arguments in source code
    Function calls without a literal text domain argument will be ignored,
    e.g. Do this: __("Foo","my-domain") but NOT: __("Foo",MY_DOMAIN) or: __("Foo",$myDomain)

  • Force extraction of all text domains
    You may use the special text domain value "*" (asterisk) to force extraction of all strings regardless of their text domain.
    This is not the recommended approach, but may get around the dynamic argument problem mentioned above.

  • Check the file sizes
    Loco Translate skips files over a maximum size to avoid memory crashes. This can lead to missed strings, but you will be notified. If the largest file you want to extract from is above your current setting then consider raising it.

    Note that extracting templates for the WordPress core requires a setting of over 300K, but we recommend creating core translations by copying existing PO files as they are likely to be correct when installed by the WordPress the updater.

JavaScript support

Loco Translate has limited support for extracting strings from JavaScript. The function is not enabled by default because most projects don't require it and it can slow things down a lot. You can enable it in the plugin settings if you want to try it out.

Be aware that our extractor only looks for standard translation functions (such as __) and is not a full syntax parser. As such it's unlikely to work on obfuscated code, or code that dynamically assigns translation functions. Authors may get better results from complex JavaScript using the wp i18n make-pot command. See i18n-command.

Regardless of which tool you use, if you're using wp_set_script_translations there are particular ways you must prepare JavaScript sources for extraction:

Extracting strings from a file like src/modules/widget.js and deploying minified JavaScript to dist/prod.js will not work under this system. This is a characteristic of the WordPress mechanism and is not dictated by Loco Translate. If you have this type of deployment and wish to use wp_set_script_translations, you will have to ensure somehow that the PO file references point to the script that's actually loaded at run time. One method would be to concatenate sources to an unminified file dist/prod.js - extract your strings from that - then minify it to dist/proj.min.js.

See also

Last updated by