Commit 2bb7f3f
I18N: Polyfill script module translations for WordPress < 7.0 (#77214)
* I18N: Add translation support for script modules
Add polyfill for `wp_set_script_module_translations()` and update build
templates to call it for script modules that depend on `wp-i18n`.
Script modules have no mechanism to load i18n translation data, leaving
strings in ES modules untranslated regardless of site language. This
affects admin pages built as script modules like Connectors and Fonts.
Changes:
- Add polyfill in lib/compat/wordpress-7.1/script-modules.php with
wp_set_script_module_translations(), load_script_module_textdomain(),
and gutenberg_print_script_module_translations()
- Update routes-registration.php.template to set translations for route
content and route modules that use wp-i18n
- Update module-registration.php.template to set translations for
@wordpress/* script module packages that use wp-i18n
All calls guarded with function_exists() for forward compatibility with
the Core implementation.
See https://core.trac.wordpress.org/ticket/65015.
* Add backport changelog entry for script module translations
Links WordPress/wordpress-develop#11543 to this PR.
* Retarget script module translations compat to WordPress 7.0.
Move the script modules translation polyfill from the wordpress-7.1
compat directory to wordpress-7.0 so the fix targets the 7.0 release,
which is where the new admin pages (Connectors, Fonts) using script
modules were introduced.
Also moves the backport changelog entry accordingly.
See https://core.trac.wordpress.org/ticket/65015.
* Script Modules: Align null return type in translation polyfill
Update gutenberg_get_script_module_src() and its callers in the
script module translation polyfill to return/check for null instead
of false, matching the ?string return type of the Core
WP_Script_Modules::get_registered_src() method.
* Script Modules: Pass $is_module=true to load_script_textdomain_relative_path filter
Pass the new $is_module argument (introduced in WordPress 7.0) to the
load_script_textdomain_relative_path filter from the script module
translation polyfill, so callers of the filter can distinguish script
modules from classic scripts.
* Script Modules: Sync translation polyfill with upstream Core changes
Mirror two upstream changes from the Core PR:
1. Rename get_registered_src() to get_registered() — Core renamed this
method and changed it to return the full module data array instead
of just the src. Update gutenberg_get_script_module_src() to call
the new method and read $module['src'] from the returned array,
keeping the reflection fallback for older WP versions.
2. Adopt the ES6 setLocaleData JS function and sprintf-based output
format introduced in Core for print_script_module_translations(),
so the polyfill stays consistent with Core.
* Script Modules: Auto-detect translations for script modules
Sync with upstream Core change that removes the explicit
wp_set_script_module_translations() registration requirement:
- Drop the wp_set_script_module_translations() calls in
routes-registration.php.template and module-registration.php.template.
Core now iterates all enqueued modules and auto-loads translations
from the default text domain.
- Rework gutenberg_print_script_module_translations() in the polyfill
to do the same auto-iteration. Keep wp_set_script_module_translations()
as an explicit override API for modules using non-default text
domains or custom translation paths.
* Script Modules: Align polyfill override storage with Core field names
Update the script module translation polyfill to use 'textdomain' and
'translations_path' as the field names inside its override storage,
matching the names adopted on the Core side (and used by WP_Dependency
for classic scripts).
Internal change only; the wp_set_script_module_translations() signature
is unchanged.
* Script Modules: Namespace translation inline script IDs.
* Script Modules: Drop redundant translations_ prefix from polyfill path key.
* Script Modules: Hoist locale-data JS heredoc out of the translations loop.
* Script Modules: Print wp-i18n just-in-time before translation inline scripts.
* Script Modules: Simplify polyfill translation override lookup with null-coalescing.
* Script Modules: Guard polyfill setLocaleData call against missing wp.i18n.
* Script Modules: Force-print wp-i18n before translation inline scripts in polyfill.
* Script Modules: Slim polyfill to translation printing only.
Co-authored-by: manzoorwanijk <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: jsnajdr <[email protected]>1 parent 616b958 commit 2bb7f3f
3 files changed
Lines changed: 229 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
| |||
0 commit comments