-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add filter to customize mapping of sources to translation entries. #170
Add filter to customize mapping of sources to translation entries. #170
Conversation
Codecov Report
@@ Coverage Diff @@
## master #170 +/- ##
============================================
+ Coverage 79.63% 79.65% +0.02%
Complexity 397 397
============================================
Files 24 24
Lines 928 929 +1
============================================
+ Hits 739 740 +1
Misses 189 189
Continue to review full report at Codecov.
|
I'm assuming that |
Yes, that’s correct. Because of that I used the »fix« to automatically compile the JS and push it to another Git repo via CI, to make the files available to Traduttore like they are on the webserver, but there I had problems with the i18n functions that got minified so that Traduttore did not recognize the strings. And for the source code links in GlotPress, it is nicer to have a link on the source file instead of a minified version :) |
Correct, the idea is to have a link to both files so that the JSON file can be generated for the dist file. I have to check it again, but I think the place of the filter will not work when |
With multiple files we would need multiple checks in the filter, something like that (worked in my test, the project I am using for testing has multiple JS source files that get imported into one
Or we could also check for the project if there is only one dist JS.
I’m not sure, if I understand it correctly, we would need to check the file or project there too, and then change the |
But that means only the strings from the last file will be in the final JSON file since the content doesn't get merged. |
Oh, yes, you are right… |
I will try to get it working with a filter in the |
I moved the filter to the |
Oops, there is a linter warning, checking it |
Checks are passing again :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter now applies to all sources, not only JavaScript files. That's fine, just need to remove the JS references from the docs and filter name.
Are you able to add a test for this?
I removed the JS references from doc and filter name. Regarding test: Not really, I have no experience in writing tests. I checked the existing tests for the |
Ah, maybe I got it, I will try to create a test :) |
I wrote a test but have problems setting up the testing environment. I created a Gist with the Thanks! |
This looks good at first glance. Feel free to commit it and let the build decide if it's good enough. 😀 |
:D okay! |
Hi,
component.js is imported from backend.js and frontend.js, so the strings of component would need to end up in two different json files. Not quite sure, but I think, I couldn't configure this with the filter. |
@websupporter Good question. For your case a filter for the final function filter_mapping( $mapping ) {
$mapping['backend.js'] = array_merge( $mapping['backend.js'], $mapping['component.js'] );
$mapping['frontend.js'] = array_merge( $mapping['frontend.js'], $mapping['component.js'] );
unset( $mapping['component.js'] ); // Remove since part of other sources.
return $mapping;
} Something like this? |
Hi @ocean90, Reading the code, I think such a filter would also enable you to overcome another shortcoming I think I just found: Source-Files do not necessarily have the
I could basically overwrite the whole outcome. //Edit: |
Good idea to make the filter more general and get it supporting more cases 👍 |
Do we need both filters or is one for filtering
WP-CLI only supports js and jsx by default. The make-pot command is used here. wp-cli/i18n-command#200 is also related. I think this should be handled in its own issue. |
The one proposed by @websupporter should be enough. |
I will update my PR |
Updated it, now it is using the filter @websupporter proposed |
@florianbrinkmann Thank you! Just noticed that the tests aren't passing due to a wrong filter name. |
Thanks for merging! And @websupporter for the idea with the better filter position :)
Oops, sorry for that, a little too much copy and pasting without thinking 🙈 Thanks for creating a fix-PR! |
@florianbrinkmann @websupporter @ocean90 There's now a PR in the WP-CLI repo to allow customizing this mapping right when parsing the source files: wp-cli/i18n-command#284 Would that be useful to you? If so, there could be another filter in Traduttore to leverage it. |
Description
I have the issue, that JS translations are not loaded correctly, because the path to the JS file in the Git repo that is used for the string extraction does not match the JS that is loaded on the website. For example, I have the
blocks/block-name/index.js
in my Repository, but it is compiled toassets/js/editor.blocks.js
.So when the
assets/js/editor.blocks.js
is loaded, WP does not find the translation, because the MD5 hash was generated fromblocks/block-name/index.js
.How has this been tested?
I created the following filter call to check the functionality. I added the project as a second parameter to the filter, for the case when one needs more information to decide if a replacement is needed or not (and with what to replace).
Types of changes
New feature
Checklist: