| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Assets_Translation_Loader { |
| 10 |
|
| 11 |
public static function for_handles( array $handles, $domain = null, $replace_callback = null ) { |
| 12 |
self::set_domain( $handles, $domain ); |
| 13 |
self::replace_translation_path( $handles, $replace_callback ); |
| 14 |
} |
| 15 |
|
| 16 |
private static function set_domain( array $handles, $domain = null ) { |
| 17 |
if ( empty( $domain ) || ! is_string( $domain ) ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
foreach ( $handles as $handle ) { |
| 22 |
wp_set_script_translations( $handle, $domain ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* The purpose of this function is to replace the requested translation file |
| 28 |
* with a file that contains all the translations for specific scripts. |
| 29 |
* |
| 30 |
* When developing a module and using Webpack's dynamic load feature, the script will be split into multiple chunks. |
| 31 |
* As a result, the WordPress translations expressions will also be split into multiple files. |
| 32 |
* Therefore, we replace the requested translation file with another file (generated in the build process) |
| 33 |
* that contains all the translations for the specific script (including dynamically loaded chunks). |
| 34 |
* |
| 35 |
* Want to go deeper? Read the following article: |
| 36 |
* |
| 37 |
* @see https://developer.wordpress.com/2022/01/06/wordpress-plugin-i18n-webpack-and-composer/ |
| 38 |
* |
| 39 |
* @param array $handles |
| 40 |
* @param callable|null $replace_callback |
| 41 |
*/ |
| 42 |
private static function replace_translation_path( array $handles, $replace_callback = null ) { |
| 43 |
$sources = self::map_handles_to_src( $handles ); |
| 44 |
|
| 45 |
add_filter( 'load_script_textdomain_relative_path', function ( $relative_path, $src ) use ( $sources, $replace_callback ) { |
| 46 |
if ( ! in_array( $src, $sources, true ) ) { |
| 47 |
return $relative_path; |
| 48 |
} |
| 49 |
|
| 50 |
if ( is_callable( $replace_callback ) ) { |
| 51 |
return $replace_callback( $relative_path, $src ); |
| 52 |
} |
| 53 |
|
| 54 |
return self::default_replace_translation( $relative_path ); |
| 55 |
}, 10, 2 ); |
| 56 |
} |
| 57 |
|
| 58 |
private static function map_handles_to_src( array $handles ) { |
| 59 |
return array_map( function ( $handle ) { |
| 60 |
return wp_scripts()->registered[ $handle ]->src; |
| 61 |
}, $handles ); |
| 62 |
} |
| 63 |
|
| 64 |
private static function default_replace_translation( $relative_path ) { |
| 65 |
// Translations are always based on the non-minified filename. |
| 66 |
$relative_path_without_ext = preg_replace( '/(\.min)?\.js$/i', '', $relative_path ); |
| 67 |
|
| 68 |
// By default, we suffix the file with `.strings` (e.g 'assets/js/editor.js' => 'assets/js/editor.strings.js'). |
| 69 |
return implode( '.', [ |
| 70 |
$relative_path_without_ext, |
| 71 |
'strings', |
| 72 |
'js', |
| 73 |
] ); |
| 74 |
} |
| 75 |
} |
| 76 |
|