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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / Translations.php
woocommerce / src / Internal / Admin Last commit date
Agentic 4 months ago BlockTemplates 4 weeks ago EmailImprovements 1 year ago EmailPreview 4 weeks ago Emails 1 year ago ImportExport 1 year ago Logging 1 year ago Marketing 2 years ago Notes 4 weeks ago Onboarding 5 months ago Orders 4 weeks ago ProductForm 2 years ago ProductReviews 4 weeks ago RemoteFreeExtensions 4 weeks ago Schedulers 4 weeks ago Settings 2 weeks ago Suggestions 4 weeks ago WCPayPromotion 10 months ago ActivityPanels.php 3 years ago Analytics.php 4 weeks ago CategoryLookup.php 4 years ago Coupons.php 1 year ago CouponsMovedTrait.php 5 months ago CustomerEffortScoreTracks.php 7 months ago Events.php 2 months ago FeaturePlugin.php 4 months ago Homescreen.php 1 month ago Loader.php 4 weeks ago Marketing.php 1 year ago Marketplace.php 5 months ago MobileAppBanner.php 4 years ago OrderMilestoneEasterEgg.php 4 weeks ago RemoteInboxNotifications.php 3 years ago Settings.php 2 weeks ago ShippingLabelBanner.php 1 year ago ShippingLabelBannerDisplayRules.php 1 year ago SiteHealth.php 3 years ago Survey.php 4 years ago SystemStatusReport.php 1 year ago Translations.php 1 year ago WCAdminAssets.php 2 weeks ago WCAdminSharedSettings.php 1 year ago WCAdminUser.php 9 months ago WcPayWelcomePage.php 1 year ago
Translations.php
369 lines
1 <?php
2 /**
3 * Register the scripts, and handles items needed for managing translations within WooCommerce Admin.
4 */
5
6 namespace Automattic\WooCommerce\Internal\Admin;
7
8 use Automattic\WooCommerce\Admin\PageController;
9 use Automattic\WooCommerce\Internal\Admin\Loader;
10
11 /**
12 * Translations Class.
13 */
14 class Translations {
15
16 /**
17 * Class instance.
18 *
19 * @var Translations instance
20 */
21 protected static $instance = null;
22
23 /**
24 * Plugin domain.
25 *
26 * @var string
27 */
28 private static $plugin_domain = 'woocommerce';
29
30 /**
31 * Get class instance.
32 */
33 public static function get_instance() {
34 if ( ! self::$instance ) {
35 self::$instance = new self();
36 }
37 return self::$instance;
38 }
39
40 /**
41 * Constructor.
42 * Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
43 */
44 public function __construct() {
45 add_action( 'admin_enqueue_scripts', array( $this, 'potentially_load_translation_script_file' ), 15 );
46
47 // Combine JSON translation files (from chunks) when language packs are updated.
48 add_action( 'upgrader_process_complete', array( $this, 'combine_translation_chunk_files' ), 10, 2 );
49
50 // Handler for WooCommerce and WooCommerce Admin plugin activation.
51 add_action( 'woocommerce_activated_plugin', array( $this, 'potentially_generate_translation_strings' ) );
52 add_action( 'activated_plugin', array( $this, 'potentially_generate_translation_strings' ) );
53 }
54
55 /**
56 * Generate a filename to cache translations from JS chunks.
57 *
58 * @param string $domain Text domain.
59 * @param string $locale Locale being retrieved.
60 * @return string Filename.
61 */
62 private function get_combined_translation_filename( $domain, $locale ) {
63 $filename = implode( '-', array( $domain, $locale, WC_ADMIN_APP ) ) . '.json';
64
65 return $filename;
66 }
67
68 /**
69 * Combines data from translation chunk files based on officially downloaded file format.
70 *
71 * @param array $json_i18n_filenames List of JSON chunk files.
72 * @return array Combined translation chunk data.
73 */
74 private function combine_official_translation_chunks( $json_i18n_filenames ) {
75 // the filesystem object should be hooked up.
76 global $wp_filesystem;
77 $combined_translation_data = array();
78
79 foreach ( $json_i18n_filenames as $json_filename ) {
80 if ( ! $wp_filesystem->is_readable( $json_filename ) ) {
81 continue;
82 }
83
84 $file_contents = $wp_filesystem->get_contents( $json_filename );
85 $chunk_data = \json_decode( $file_contents, true );
86
87 if ( empty( $chunk_data ) ) {
88 continue;
89 }
90
91 if ( ! isset( $chunk_data['comment']['reference'] ) ) {
92 continue;
93 }
94
95 $reference_file = $chunk_data['comment']['reference'];
96
97 // Only combine "app" files (not scripts registered with WP).
98 if (
99 false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) &&
100 false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' )
101 ) {
102 continue;
103 }
104
105 if ( empty( $combined_translation_data ) ) {
106 // Use the first translation file as the base structure.
107 $combined_translation_data = $chunk_data;
108 } else {
109 // Combine all messages from all chunk files.
110 $combined_translation_data['locale_data']['messages'] = array_merge(
111 $combined_translation_data['locale_data']['messages'],
112 $chunk_data['locale_data']['messages']
113 );
114 }
115 }
116
117 // Remove inaccurate reference comment.
118 unset( $combined_translation_data['comment'] );
119 return $combined_translation_data;
120 }
121
122 /**
123 * Combines data from translation chunk files based on user-generated file formats,
124 * such as wp-cli tool or Loco Translate plugin.
125 *
126 * @param array $json_i18n_filenames List of JSON chunk files.
127 * @return array Combined translation chunk data.
128 */
129 private function combine_user_translation_chunks( $json_i18n_filenames ) {
130 // the filesystem object should be hooked up.
131 global $wp_filesystem;
132 $combined_translation_data = array();
133
134 foreach ( $json_i18n_filenames as $json_filename ) {
135 if ( ! $wp_filesystem->is_readable( $json_filename ) ) {
136 continue;
137 }
138
139 $file_contents = $wp_filesystem->get_contents( $json_filename );
140 $chunk_data = \json_decode( $file_contents, true );
141
142 if ( empty( $chunk_data ) ) {
143 continue;
144 }
145
146 $reference_file = $chunk_data['source'];
147
148 // Only combine "app" files (not scripts registered with WP).
149 if (
150 false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) &&
151 false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' )
152 ) {
153 continue;
154 }
155
156 if ( empty( $combined_translation_data ) ) {
157 // Use the first translation file as the base structure.
158 $combined_translation_data = $chunk_data;
159 } else {
160 // Combine all messages from all chunk files.
161 $combined_translation_data['locale_data']['woocommerce'] = array_merge(
162 $combined_translation_data['locale_data']['woocommerce'],
163 $chunk_data['locale_data']['woocommerce']
164 );
165 }
166 }
167
168 // Remove inaccurate reference comment.
169 unset( $combined_translation_data['source'] );
170 return $combined_translation_data;
171 }
172
173 /**
174 * Find and combine translation chunk files.
175 *
176 * Only targets files that aren't represented by a registered script (e.g. not passed to wp_register_script()).
177 *
178 * @param string $lang_dir Path to language files.
179 * @param string $domain Text domain.
180 * @param string $locale Locale being retrieved.
181 * @return array Combined translation chunk data.
182 */
183 private function get_translation_chunk_data( $lang_dir, $domain, $locale ) {
184 // So long as this function is called during the 'upgrader_process_complete' action,
185 // the filesystem object should be hooked up.
186 global $wp_filesystem;
187
188 // Grab all JSON files in the current language pack.
189 $json_i18n_filenames = glob( $lang_dir . $domain . '-' . $locale . '-*.json' );
190 $combined_translation_data = array();
191
192 if ( false === $json_i18n_filenames ) {
193 return $combined_translation_data;
194 }
195
196 // Use first JSON file to determine file format. This check is required due to
197 // file format difference between official language files and user translated files.
198 $format_determine_file = reset( $json_i18n_filenames );
199
200 if ( ! $wp_filesystem->is_readable( $format_determine_file ) ) {
201 return $combined_translation_data;
202 }
203
204 $file_contents = $wp_filesystem->get_contents( $format_determine_file );
205 $format_determine_data = \json_decode( $file_contents, true );
206
207 if ( empty( $format_determine_data ) ) {
208 return $combined_translation_data;
209 }
210
211 if ( isset( $format_determine_data['comment'] ) ) {
212 return $this->combine_official_translation_chunks( $json_i18n_filenames );
213 } elseif ( isset( $format_determine_data['source'] ) ) {
214 return $this->combine_user_translation_chunks( $json_i18n_filenames );
215 } else {
216 return $combined_translation_data;
217 }
218 }
219
220 /**
221 * Combine and save translations for a specific locale.
222 *
223 * Note that this assumes \WP_Filesystem is already initialized with write access.
224 *
225 * @param string $language_dir Path to language files.
226 * @param string $plugin_domain Text domain.
227 * @param string $locale Locale being retrieved.
228 */
229 private function build_and_save_translations( $language_dir, $plugin_domain, $locale ) {
230 global $wp_filesystem;
231 $translations_from_chunks = $this->get_translation_chunk_data( $language_dir, $plugin_domain, $locale );
232
233 if ( empty( $translations_from_chunks ) ) {
234 return;
235 }
236
237 $cache_filename = $this->get_combined_translation_filename( $plugin_domain, $locale );
238 $chunk_translations_json = wp_json_encode( $translations_from_chunks );
239
240 // Cache combined translations strings to a file.
241 $wp_filesystem->put_contents( $language_dir . $cache_filename, $chunk_translations_json );
242 }
243
244 /**
245 * Combine translation chunks when plugin is activated.
246 *
247 * This function combines JSON translation data auto-extracted by GlotPress
248 * from Webpack-generated JS chunks into a single file. This is necessary
249 * since the JS chunks are not known to WordPress via wp_register_script()
250 * and wp_set_script_translations().
251 */
252 private function generate_translation_strings() {
253 $locale = determine_locale();
254 $lang_dir = WP_LANG_DIR . '/plugins/';
255
256 // Bail early if not localized.
257 if ( 'en_US' === $locale ) {
258 return;
259 }
260
261 if ( ! function_exists( 'get_filesystem_method' ) ) {
262 require_once ABSPATH . 'wp-admin/includes/file.php';
263 }
264
265 $access_type = get_filesystem_method();
266 if ( 'direct' === $access_type ) {
267 \WP_Filesystem();
268 $this->build_and_save_translations( $lang_dir, self::$plugin_domain, $locale );
269 } else {
270 // I'm reluctant to add support for other filesystems here as it would require
271 // user's input on activating plugin - which I don't think is common.
272 return;
273 }
274 }
275
276 /**
277 * Loads the required translation scripts on the correct pages.
278 */
279 public function potentially_load_translation_script_file() {
280 if ( ! PageController::is_admin_or_embed_page() ) {
281 return;
282 }
283
284 // Grab translation strings from Webpack-generated chunks.
285 add_filter( 'load_script_translation_file', array( $this, 'load_script_translation_file' ), 10, 3 );
286 }
287
288 /**
289 * Load translation strings from language packs for dynamic imports.
290 *
291 * @param string $file File location for the script being translated.
292 * @param string $handle Script handle.
293 * @param string $domain Text domain.
294 *
295 * @return string New file location for the script being translated.
296 */
297 public function load_script_translation_file( $file, $handle, $domain ) {
298 // Make sure the main app script is being loaded.
299 if ( WC_ADMIN_APP !== $handle ) {
300 return $file;
301 }
302
303 // Make sure we're handing the correct domain.
304 if ( self::$plugin_domain !== $domain ) {
305 return $file;
306 }
307
308 $locale = determine_locale();
309 $cache_filename = $this->get_combined_translation_filename( $domain, $locale );
310
311 return WP_LANG_DIR . '/plugins/' . $cache_filename;
312 }
313
314 /**
315 * Run when plugin is activated (can be WooCommerce or WooCommerce Admin).
316 *
317 * @param string $filename Activated plugin filename.
318 */
319 public function potentially_generate_translation_strings( $filename ) {
320 $activated_plugin_domain = explode( '/', $filename )[0];
321
322 // Ensure we're only running only on activation hook that originates from our plugin.
323 if ( self::$plugin_domain === $activated_plugin_domain ) {
324 $this->generate_translation_strings();
325 }
326 }
327
328 /**
329 * Combine translation chunks when files are updated.
330 *
331 * This function combines JSON translation data auto-extracted by GlotPress
332 * from Webpack-generated JS chunks into a single file that can be used in
333 * subsequent requests. This is necessary since the JS chunks are not known
334 * to WordPress via wp_register_script() and wp_set_script_translations().
335 *
336 * @param Language_Pack_Upgrader $instance Upgrader instance.
337 * @param array $hook_extra Info about the upgraded language packs.
338 */
339 public function combine_translation_chunk_files( $instance, $hook_extra ) {
340 if (
341 ! is_a( $instance, 'Language_Pack_Upgrader' ) ||
342 ! isset( $hook_extra['translations'] ) ||
343 ! is_array( $hook_extra['translations'] )
344 ) {
345 return;
346 }
347
348 $locales = array();
349 $language_dir = WP_LANG_DIR . '/plugins/';
350
351 // Gather the locales that were updated in this operation.
352 foreach ( $hook_extra['translations'] as $translation ) {
353 if (
354 'plugin' === $translation['type'] &&
355 self::$plugin_domain === $translation['slug']
356 ) {
357 $locales[] = $translation['language'];
358 }
359 }
360
361 // Build combined translation files for all updated locales.
362 foreach ( $locales as $locale ) {
363 // So long as this function is hooked to the 'upgrader_process_complete' action,
364 // WP_Filesystem should be hooked up to be able to call build_and_save_translations.
365 $this->build_and_save_translations( $language_dir, self::$plugin_domain, $locale );
366 }
367 }
368 }
369