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
5 days 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
5 days 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
5 days 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 |