Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong md5 hash generated by i18n-command #177

Open
stuartshields opened this issue Jul 31, 2019 · 7 comments
Open

Wrong md5 hash generated by i18n-command #177

stuartshields opened this issue Jul 31, 2019 · 7 comments

Comments

@stuartshields
Copy link

@stuartshields stuartshields commented Jul 31, 2019

So I believe this is an issue with how the wp cli command generates the md5 file. I'm going to best describe what happens:

So originally this ticket was created on core - https://core.trac.wordpress.org/ticket/45488 and another ticket in this repo: #120

While the original core ticket has "works for me", I've witnessed this issue first hand over the past week and below is the steps I took to:

  • We have our custom plugins inside of mu-plugins i.e: mu-plugins/gallery as an example of a name
  • wp_set_script_translations( 'msa-gallery', 'msa-backend' ); is also set
  • We also use the domain in other plugins so we'll also have:
    wp_set_script_translations( 'msa-blocks', 'msa-backend' );
    wp_set_script_translations( 'msa-search', 'msa-backend' );
  • Our file structure for the JS file is: assets/dist/msa-gallery.js inside the msa-gallery folder
  • When we generate the json file inside of WP CLI using the following command:
  • Site language is Japanese
    Note: msa-gallery and msa-backend aren't our real naming conventions as I've changed these to protect the client project we are working on, however I'm happy to discuss in private the exact naming conventions etc. to help debug this issue.
wp i18n make-json msa-backend-ja.po . --no-purge

The output file will be something like:

msa-backend-ja-d8a574af56fa83923f9f56e1bb562d79.json

However inside of WP, the language file isn't being loaded at all, Query Monitor shows us that the json file name is:
msa-backend-ja-54e4313a94cbfa7a67f5ea381d70f187.json

An MD5 check on what is generated in WP is correct which is assets/dist/msa-gallery.js. However the file generated by wp i18n make-json is not correct.

A way to get around this currently is to rename the json file generated by wp i18n make-json to the one that WP expects. Not ideal since the generation of the file should match what is expected in WP itself.

Let me know if you need anything else, happy to discuss in private on the names we are using on project.

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Aug 1, 2019

Hi there

Thanks for raising this. When opening the PO file, what is the path to the JS file that is shown in the string comments?

@stuartshields
Copy link
Author

@stuartshields stuartshields commented Aug 2, 2019

@swissspidy So here are a few:

  • msa-blocks/assets/src/msa-blocks.js
  • msa-gallery/assets/js/msa-gallery.js

When looking at the path for WP, it's expecting it to be: assets/js/msa-gallery.js
This is what WP is doing - https://github.com/WordPress/WordPress/blob/8059b362e83b980be06a32098a0f7168fc939de9/wp-includes/l10n.php#L955

@stuartshields
Copy link
Author

@stuartshields stuartshields commented Aug 17, 2019

@swissspidy Any progress on this?

@matzeeable
Copy link
Contributor

@matzeeable matzeeable commented Aug 22, 2019

Hi @stuartshields,

I came across this issue because I had the same problem a time ago. I think the bug is, that WordPress generates the hash from the relative path to your plugin, so:

https://github.com/WordPress/WordPress/blob/8059b362e83b980be06a32098a0f7168fc939de9/wp-includes/l10n.php#L1019

$md5_filename = $file_base . '-' . md5( $relative ) . '.json';

assets/dist/msa-gallery.js will result in (the correct) form: 54e4313a94cbfa7a67f5ea381d70f187

... and wp i18n make-json generates the hash like this (I do not know where $file is generated but it's not correct):

$hash = md5( $file );
$destination_file = "${destination}/{$base_file_name}-{$hash}.json";

I have solved it by hook into the filter load_script_translation_file:

https://github.com/matzeeable/wp-reactjs-starter/blob/67947933446091f42546b57eb33a7293d508cb79/inc/general/Localization.class.php#L29-L49

    /**
     * Allow language overrides so for example de_AT uses de_DE to avoid duplicate
     * .po files and management. This is for JavaScript files!
     */
    public function load_script_translation_file($file, $handle, $domain) {
        $locale = determine_locale();
        $pluginFolder = path_join(WPRJSS_PATH, base\Assets::PUBLIC_JSON_I18N);
        if ($domain === WPRJSS_TD && !is_readable($file) && substr($file, 0, strlen($pluginFolder)) === $pluginFolder) {
            // Collect data
            $folder = dirname($file);
            $use = $this->override($locale);
            $wp_scripts = wp_scripts();
            $src = $wp_scripts->registered[$handle]->src;
            // Generate new file
            $file_base = $domain . '-' . $locale . '-' . md5(basename($src)) . '.json';
            return path_join($folder, $file_base);
        }
        return $file;
    }

You can not copy&paste the above snippet because it is very related to my boilerplate plugin but you see the idea behind. I hope that helps.

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Aug 22, 2019

Let‘s fix the hash then instead of having to use a filter :-)

No progress so far because I haven‘t had time to look into this.

@kraftner
Copy link

@kraftner kraftner commented Sep 12, 2019

The problem seems to be that wp-cli is using the path of the source files where it found the strings, while WP is using the path of the built files which is of course different. Not sure how to fix this though.

@simonhammes
Copy link

@simonhammes simonhammes commented Sep 12, 2019

@kraftner This is a hacky solution, but it seems to work:
You can prevent this from happening by not minifying your .js files at first, then generating the .po file based on this non-minified, built file; then using make-json to create your .json file and then overwriting your non-minified .js file with its minified version.

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

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.