Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| I18nService | |
0.00% |
0 / 44 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| add_php_i18n | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| add_js_i18n | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
| load_script_translation_file | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Solutions; |
| 4 | |
| 5 | /** |
| 6 | * Class for handling internationalization. |
| 7 | */ |
| 8 | class I18nService { |
| 9 | |
| 10 | /** |
| 11 | * Init the i18n service |
| 12 | * |
| 13 | * @param Container $container the container |
| 14 | */ |
| 15 | public function __construct( $container ) { |
| 16 | add_action( 'init', array( $this, 'add_php_i18n' ), 100 ); |
| 17 | add_action( 'admin_enqueue_scripts', array( $this, 'add_js_i18n' ), 100 ); |
| 18 | add_filter( |
| 19 | 'load_script_translation_file', |
| 20 | array( $this, 'load_script_translation_file' ), |
| 21 | 10, |
| 22 | 3 |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Load module text domain |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function add_php_i18n() { |
| 32 | load_plugin_textdomain( |
| 33 | 'wp-module-solutions', |
| 34 | false, |
| 35 | NFD_SOLUTIONS_DIR . '/languages' |
| 36 | ); |
| 37 | // Load the PHP translations from .l10n.php files in the languages dir. |
| 38 | load_textdomain( |
| 39 | 'wp-module-solutions', |
| 40 | NFD_SOLUTIONS_DIR . '/languages/wp-module-solutions-' . get_locale() . '.l10n.php' |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Enqueue js/script for translations of the solutions app |
| 46 | */ |
| 47 | public function add_js_i18n() { |
| 48 | // solutions page |
| 49 | wp_set_script_translations( |
| 50 | 'solutions-page', |
| 51 | 'wp-module-solutions', |
| 52 | NFD_SOLUTIONS_DIR . '/languages' |
| 53 | ); |
| 54 | load_script_textdomain( |
| 55 | 'solutions-page', |
| 56 | 'wp-module-solutions', |
| 57 | NFD_SOLUTIONS_DIR . '/languages' |
| 58 | ); |
| 59 | |
| 60 | // addnew tools page |
| 61 | wp_set_script_translations( |
| 62 | 'solutions-add-new-tools', |
| 63 | 'wp-module-solutions', |
| 64 | NFD_SOLUTIONS_DIR . '/languages' |
| 65 | ); |
| 66 | load_script_textdomain( |
| 67 | 'solutions-add-new-tools', |
| 68 | 'wp-module-solutions', |
| 69 | NFD_SOLUTIONS_DIR . '/languages' |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Filters the file path for the JS translation JSON. |
| 75 | * |
| 76 | * If the script handle matches the module's handle, builds a custom path using |
| 77 | * the languages directory, current locale, text domain, and a hash of the script. |
| 78 | * |
| 79 | * @param string $file Default translation file path. |
| 80 | * @param string $handle Script handle. |
| 81 | * @param string $domain Text domain. |
| 82 | * @return string Modified file path for the translation JSON. |
| 83 | */ |
| 84 | public function load_script_translation_file( $file, $handle, $domain ) { |
| 85 | if ( 'solutions-page' === $handle ) { |
| 86 | $file_base = $domain . '-' . determine_locale(); |
| 87 | // Build the file path using the languages directory and the hash of the script. |
| 88 | $file = NFD_SOLUTIONS_DIR . '/languages/' . $file_base . '-' . md5( 'build/solutions-page/bundle.js' ) . '.json'; |
| 89 | } |
| 90 | if ( 'solutions-add-new-tools' === $handle ) { |
| 91 | $file_base = $domain . '-' . determine_locale(); |
| 92 | // Build the file path using the languages directory and the hash of the script. |
| 93 | $file = NFD_SOLUTIONS_DIR . '/languages/' . $file_base . '-' . md5( 'build/addnew/bundle.js' ) . '.json'; |
| 94 | } |
| 95 | return $file; |
| 96 | } |
| 97 | } |