Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#45425 closed defect (bug) (fixed)

Add a filter to the translation file location for script localisation

Reported by: johnbillion's profile johnbillion Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.0.2 Priority: normal
Severity: normal Version: 5.0
Component: I18N Keywords: has-patch commit fixed-5.0
Focuses: javascript Cc:

Description

In [43825] (#45103), the load_script_textdomain() function was introduced as part of the changes that allow JavaScript files to register and use translated strings.

This function doesn't pass its return value through a filter, which means it's not possible to override the paths used for translation files, nor is it possible to inspect its behaviour for debugging purposes. The latter is particularly noteworthy because this is brand new functionality that plugin and theme authors will be attempting to implement, and being able to debug what translation files are being loaded is valuable.

I propose that this function is altered so it operates a little more similarly to the existing load_plugin_textdomain() and load_textdomain() functions for gettext translation files.

cc @omarreiss @herregroen

Attachments (5)

45425.patch (2.7 KB) - added by johnbillion 6 years ago.
45425.2.patch (3.4 KB) - added by strategio 6 years ago.
Improved patch to introduce the filter pre_load_script_translation
45425.diff (3.9 KB) - added by swissspidy 6 years ago.
wp-php-filters-for-js-l10n.php (1.3 KB) - added by dimadin 6 years ago.
45425.2.diff (4.8 KB) - added by ocean90 6 years ago.

Download all attachments as: .zip

Change History (27)

@johnbillion
6 years ago

#1 @johnbillion
6 years ago

  • Keywords has-patch added

45425.patch adds a load_script_translation() function which allows the script translation file location to be filtered and then loads and returns the file's contents, similarly to how load_textdomain() operates.

This allows, for example, the Query Monitor plugin to add support for debugging script translation file loading.

#2 @herregroen
6 years ago

@johnbillion sounds like a good idea. Patch looks good as well.

I'm all in favour.

#3 @swissspidy
6 years ago

You're totally right that people should be able to debug this.

Patch looks good to me as well.

#4 @omarreiss
6 years ago

Seems like a safe and good commit to me. I've asked @pento for signoff.

#5 @johnbillion
6 years ago

This actually isn't as optimal as it could be because the potential translation file paths aren't exposed, but it works the same way that load_textdomain() does so let's get it into 5.0 and iterate on it in a point release.

#6 @swissspidy
6 years ago

#45320 was marked as a duplicate.

@strategio
6 years ago

Improved patch to introduce the filter pre_load_script_translation

#7 @strategio
6 years ago

Following suggestion from @swissspidy in #45320, I improved the patch to include a filter pre_load_script_translation which would allow to preload the translations (not necessarily from a file).

@swissspidy
6 years ago

#8 @swissspidy
6 years ago

In 45425.diff

  • Improve inline docs
  • Add filter at the end of the function

#9 @pento
6 years ago

  • Milestone changed from 5.0 to 5.0.1

As this patch is still evolving, and I'm not wild about adding functionality during RC, I'm bumping this to 5.0.1.

#10 @dimadin
6 years ago

I am not sure should this be in this separate ticket or handled here. Before JavaScript translations and WP 5.0 (since [3425] and WP 2.0.1), every string was passed through some of gettext filters. This enabled manipulations of translations but also of originals in English (e.g. change some text). There are over 500 plugins in WP.org repository that use them. They are also used by Serbian localization to ship both Serbian Cyrillic and Serbian Latin version in one package.

We don't have anything like this with JavaScript translations. They aren't filterable at all (this is the reason there is no WP 5.0 in Serbian Latin). One option is to filter strings on JavaScript side. But plugins that already use those filters will not work there, they need to port their filters to JavaScript version (and some implementations can't be ported at all) and have both PHP and JavaScript versions.

Since we already load JavaScript translations in PHP, can we loop through those translations and then apply gettext filters? One problem I see already is that for plural strings since we can't use ngettext filter.

This ticket was mentioned in Slack in #core by dimadin. View the logs.


6 years ago

#12 @pento
6 years ago

  • Milestone changed from 5.0.1 to 5.0.2

#13 follow-up: @swissspidy
6 years ago

@dimadin Correct, because of plural forms we cannot do this on the PHP side. That's one of the reasons we have this new JS functionality in the first place.

Instead, I suggested filters for these on the JS side in https://github.com/WordPress/gutenberg/pull/12517

#14 @strategio
6 years ago

Hi,

Having filters on the JS side (as suggested by @swissspidy and @dimadin) does not exclude having filters in PHP.

We have extended our solution to translate JS strings by overriding the JED files, and we will rely on the new filters added here (mostly load_script_textdomain_file). Could we have a confirmation that it will be included?

Thanks!

#15 @swissspidy
6 years ago

We could probably rename load_script_textdomain_file to load_script_translation_file for consistency, but otherwise 45425.diff is good to go IMO.

@ocean90 @johnbillion @pento If anyone wants to do a final review, that'd be great :-)


Having filters on the JS side does not exclude having filters in PHP.

Of course not, that's why this ticket and the patches exist :-)

Could we have a confirmation that it will be included?

I think it's fair to say that there aren't any guarantees with open source projects :-)

This ticket is currently slated for the 5.0.2 milestone and is something that just makes sense, so it's definitely likely to be included in that release.

#16 in reply to: ↑ 13 @dimadin
6 years ago

Replying to swissspidy:

@dimadin Correct, because of plural forms we cannot do this on the PHP side. That's one of the reasons we have this new JS functionality in the first place.

I did research. There are less than 60 plugins in WP.org repository that use ngettext filter. Even smaller number of those actually accept fourth parameter, number used for determining what plural form to use. Of those that accept it, not all use it. So basically many are filtering plural strings the same way they filter standard strings, without paying attention is that singular or plural form (they even use the same callback functions).

I'm attaching function I made that is callback on proposed load_script_translation filter (from attachment:45425.diff) and that loops through all strings from JavaScript translations and applies gettext or gettext_with_context filter on them. If string has plural form, it applies gettext/gettext_with_context filter on each plural form. This will restore most uses of existing gettext filters, only those that really need number for plural strings will need JS implementation.

#17 @ocean90
6 years ago

  • Keywords needs-refresh added

Feedback for 45425.diff:

  • Let's rename load_script_translation() to load_script_translations() (plural).
  • Also, load_script_textdomain_file to load_script_translation_file.
  • The @since tags need an update
  • "textdomain" => "text domain"
Last edited 6 years ago by ocean90 (previous) (diff)

#18 @swissspidy
6 years ago

  • Keywords commit added

@ocean90
6 years ago

#19 @ocean90
6 years ago

  • Keywords needs-refresh removed

#20 @ocean90
6 years ago

In 44232:

I18N: Introduce load_script_translations() as a wrapper for loading and filtering translation data for JavaScript files.

  • Introduces pre_load_script_translations to short-circuit the function.
  • Introduces load_script_translation_file to filter the file path for loading script translations.
  • Introduces load_script_translations to filter the JSON-encoded translation data.

Props johnbillion, strategio, swissspidy, dimadin, ocean90.
See #45425.

#21 @ocean90
6 years ago

  • Keywords fixed-5.0 added

#22 @SergeyBiryukov
6 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 44316:

I18N: Introduce load_script_translations() as a wrapper for loading and filtering translation data for JavaScript files.

  • Introduces pre_load_script_translations to short-circuit the function.
  • Introduces load_script_translation_file to filter the file path for loading script translations.
  • Introduces load_script_translations to filter the JSON-encoded translation data.

Props johnbillion, strategio, swissspidy, dimadin, ocean90.
Merges [44232] to trunk.
Fixes #45425.

Note: See TracTickets for help on using tickets.