Troubleshooting JavaScript Translation Issues in Loco Translate
Content
Translating JavaScript strings in WordPress plugins and themes is a common task, but it can sometimes lead to confusion when translations don't appear on the frontend. This guide will help you diagnose and resolve the most frequent issues related to JavaScript translation files in Loco Translate.
Why JavaScript Translations Are Different
Unlike PHP strings, which are translated using .po and .mo files, JavaScript strings require an additional step. WordPress uses a system of JSON-based translation files that are loaded via the wp_set_script_translations() function. Loco Translate can generate these .json files, but several factors can prevent them from working correctly.
Common Problems and Their Solutions
1. JSON Files Are Not Generated or Have the Wrong Name
Symptoms: Your PHP translations work, but JavaScript strings remain in the original language. You might not see any .json files in your language folder, or their names might not match what WordPress expects.
Why it happens: The naming convention for JSON files is strict. WordPress expects a filename in the format {text-domain}-{locale}-{md5}.json. However, Loco Translate generates the filename based on the source .po file's name. If your theme or plugin follows the old convention of not including the text domain in the .po filename, the generated .json file will also be missing it, causing a mismatch.
Solution: Manually check the name of the generated .json file. If it is missing the text domain (e.g., de_DE-md5.json), you may need to rename it to include the domain (e.g., mytextdomain-de_DE-md5.json).
2. The MD5 Hash in the Filename Is Incorrect
Symptoms: JSON files are present but are not being loaded by WordPress.
Why it happens: The MD5 hash in the filename is generated from the path to the source JavaScript file. If your development workflow uses a build process (e.g., Webpack) that changes the file path from source to production, the hash generated by Loco Translate (based on the source path) will differ from the hash WordPress calculates (based on the production path).
Solution: This is a known limitation. The Loco Translate team has acknowledged that the plugin uses file references from the .po file to generate the hash, and if those paths are wrong, the resulting hash will be too. You may need to manually rename the .json files to match the hash WordPress expects or adjust your build process so the source paths match the production paths.
3. File Extraction Settings Are Misconfigured
Symptoms: Strings from your .js files are not being found during the sync process.
Why it happens: By default, Loco Translate may not scan all JavaScript file types, or it may be scanning unnecessary directories like node_modules, which can slow the process down immensely.
Solution:
- Ensure you have enabled JavaScript scanning in Loco Translate → Settings → File Extensions.
- If you use custom file extensions for JavaScript (like
.tag), add them to the "Scan JavaScript files with extensions" setting. Note: A user-reported workaround suggests there may be a hardcoded check for.jsfiles in the plugin's code, so this may not work perfectly for all custom extensions. - To prevent slow scans, add
node_modulesto your bundle's excluded paths in the Advanced configuration tab.
4. The JSON File Is Not Being Loaded by WordPress
Symptoms: Everything seems correct, but translations still don't load.
Why it happens: The issue might not be with the file itself but with how it's being enqueued in WordPress.
Solution: Triple-check your script enqueueing code. You must:
- Set
wp-i18nas a dependency for your script. - Call
wp_set_script_translations()with the correct script handle, text domain, and path. - Use the correct i18n functions in your JavaScript code (e.g.,
__()from@wordpress/i18n).
Conclusion
JavaScript translation issues often boil down to a mismatch between what Loco Translate generates and what WordPress expects to find. The most reliable approach is to methodically check the JSON filename, its MD5 hash, and your WordPress enqueue logic. While some workarounds involve manual file renaming, understanding the root cause is the key to a lasting solution.
Related Support Threads Support
-
Is there a hook to add custom strings to .pot file?https://wordpress.org/support/topic/is-there-a-hook-to-add-custom-strings-to-pot-file/
-
.json translation files are generated without text-domain in child-theme folderhttps://wordpress.org/support/topic/json-translation-files-are-generated-without-text-domain-in-child-theme-folder/
-
Bug : Loco deletes source path portion thinking it is the same sourcehttps://wordpress.org/support/topic/bug-loco-deletes-source-path-portion-thinking-it-is-the-same-source/
-
JSX filehttps://wordpress.org/support/topic/jsx-file/
-
Strings from Javascript not translatedhttps://wordpress.org/support/topic/strings-from-javascript-not-translated/
-
Suggestions to reduce plugin weighthttps://wordpress.org/support/topic/suggestions-to-reduce-plugin-weight/
-
How can I translate block.json and theme.json entries?https://wordpress.org/support/topic/how-can-i-translate-block-json-and-theme-json-entries/
-
// not related to this Pluginhttps://wordpress.org/support/topic/doent-load-values-from-acf-fields/
-
MU-Plugins: No strings can be extracted from source codehttps://wordpress.org/support/topic/mu-plugins-no-strings-can-be-extracted-from-source-code/
-
JSON Language Packshttps://wordpress.org/support/topic/json-language-packs/
-
Request: Disable file existence check to generate JSON files.https://wordpress.org/support/topic/request-disable-file-existence-check-to-generate-json-files/
-
JSON Filehttps://wordpress.org/support/topic/json-file-2/
-
PHP strings detected by WordPress but not by Loco Translatehttps://wordpress.org/support/topic/php-strings-detected-by-wordpress-but-not-by-loco-translate/
-
Issue with Translatable Strings in Gutenberg Block using __ Functionhttps://wordpress.org/support/topic/issue-with-translatable-strings-in-gutenberg-block-using-__-function/
-
Allowing custom handling of JSON translationshttps://wordpress.org/support/topic/allowing-custom-handling-of-json-translations/
-
.po file needed or is .mo enough? 30% of translation missing in .mo filehttps://wordpress.org/support/topic/po-file-needed-or-is-mo-enough-30-of-translation-missing-in-mo-file/
-
Prevent scanning node_modules folderhttps://wordpress.org/support/topic/prevent-scanning-node_modules-folder/
-
Can’t translate JavaScript with specific extentionhttps://wordpress.org/support/topic/cant-translate-javascript-with-specific-extention/
-
JSON translation filehttps://wordpress.org/support/topic/json-translation-file/
-
Can loco generate the POT file ?https://wordpress.org/support/topic/can-loco-generate-the-pot-file/
-
Cannot scan block.json fileshttps://wordpress.org/support/topic/cannot-scan-block-json-files/
-
Scanning twig fileshttps://wordpress.org/support/topic/scanning-twig-files/
-
how does the translation funciton work in js filehttps://wordpress.org/support/topic/how-does-the-translation-funciton-work-in-js-file/
-
Generated JSON files don’t have the correct md5 hashhttps://wordpress.org/support/topic/generated-json-files-dont-have-the-correct-md5-hash/
-
Translated data in JSON files are not showing or getting losthttps://wordpress.org/support/topic/translated-data-in-json-files-are-not-showing-or-getting-lost/