PluginProbe
Elementor Website Builder – more than just a page builder / 3.21.4
Elementor Website Builder – more than just a page builder v3.21.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / assets-translation-loader.php

assets-translation-loader.php in Elementor Website Builder – more than just a page builder 3.21.4, at core/utils/assets-translation-loader.php

75 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @see https://developer.wordpress.com/2022/01/06/wordpress-plugin-i18n-webpack-and-composer/
37 *
38 * @param array $handles
39 * @param callable|null $replace_callback
40 */
41 private static function replace_translation_path( array $handles, $replace_callback = null ) {
42 $sources = self::map_handles_to_src( $handles );
43
44 add_filter( 'load_script_textdomain_relative_path', function ( $relative_path, $src ) use ( $sources, $replace_callback ) {
45 if ( ! in_array( $src, $sources, true ) ) {
46 return $relative_path;
47 }
48
49 if ( is_callable( $replace_callback ) ) {
50 return $replace_callback( $relative_path, $src );
51 }
52
53 return self::default_replace_translation( $relative_path );
54 }, 10, 2 );
55 }
56
57 private static function map_handles_to_src( array $handles ) {
58 return array_map( function ( $handle ) {
59 return wp_scripts()->registered[ $handle ]->src;
60 }, $handles );
61 }
62
63 private static function default_replace_translation( $relative_path ) {
64 // Translations are always based on the non-minified filename.
65 $relative_path_without_ext = preg_replace( '/(\.min)?\.js$/i', '', $relative_path );
66
67 // By default, we suffix the file with `.strings` (e.g 'assets/js/editor.js' => 'assets/js/editor.strings.js').
68 return implode( '.', [
69 $relative_path_without_ext,
70 'strings',
71 'js',
72 ] );
73 }
74 }
75