Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| I18nService | |
0.00% |
0 / 42 |
|
0.00% |
0 / 5 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| load_text_domain | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| prepare_and_load_js_translations | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| load_php_translations | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| load_js_translations | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Services; |
| 4 | |
| 5 | /** |
| 6 | * Class for handling internationalization. |
| 7 | */ |
| 8 | class I18nService { |
| 9 | /** |
| 10 | * Version of plugin for versioning the scripts. |
| 11 | * |
| 12 | * @var version |
| 13 | */ |
| 14 | protected $version; |
| 15 | |
| 16 | /** |
| 17 | * Init the i18n service |
| 18 | * |
| 19 | * @param Container $container the container |
| 20 | */ |
| 21 | public function __construct( $container ) { |
| 22 | $this->version = $container->plugin()->version; |
| 23 | // add_action( 'newfold/performance/load_default_translations', array( $this, 'prepare_and_load_js_translations' ) ); |
| 24 | add_action( 'load-toplevel_page_' . $container->plugin()->id, array( $this, 'prepare_and_load_js_translations' ), 1 ); |
| 25 | add_action( 'init', array( $this, 'load_text_domain' ) ); |
| 26 | add_action( 'load-tools_page_nfd-performance', array( $this, 'prepare_and_load_js_translations' ), 1 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Load module text domain |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function load_text_domain() { |
| 35 | $this::load_php_translations( |
| 36 | 'wp-module-performance', |
| 37 | NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Enqueue script for translations of the performance panel settings |
| 43 | */ |
| 44 | public function prepare_and_load_js_translations() { |
| 45 | |
| 46 | add_action( |
| 47 | 'admin_enqueue_scripts', |
| 48 | function () { |
| 49 | $this::load_js_translations( |
| 50 | 'wp-module-performance', |
| 51 | 'nfd-performance', |
| 52 | NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR |
| 53 | ); |
| 54 | |
| 55 | $this::load_js_translations( |
| 56 | 'wp-module-performance', |
| 57 | 'nfd-performance-bulk-optimizer', |
| 58 | NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR |
| 59 | ); |
| 60 | |
| 61 | $this::load_js_translations( |
| 62 | 'wp-module-performance', |
| 63 | 'nfd-performance-image-bulk-optimizer', |
| 64 | NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR |
| 65 | ); |
| 66 | }, |
| 67 | 100 |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Loads the PHP translations from .mo files in the languages dir. |
| 73 | * The .mo file must be named $textdomain-$locale.mo |
| 74 | * |
| 75 | * @param [string] $textdomain The text domain. |
| 76 | * @param [string] $languages_dir The directory containing the .mo files. |
| 77 | * @return boolean |
| 78 | */ |
| 79 | public static function load_php_translations( $textdomain, $languages_dir ) { |
| 80 | $loaded_ptd = load_plugin_textdomain( |
| 81 | $textdomain, |
| 82 | false, |
| 83 | $languages_dir |
| 84 | ); |
| 85 | |
| 86 | $current_language = get_locale(); |
| 87 | $loaded_td = load_textdomain( 'wp-module-performance', $languages_dir . '/' . $textdomain . '-' . $current_language . '.mo' ); |
| 88 | |
| 89 | return $loaded_ptd && $loaded_td; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Localizes a particular script using a JSON file present in the languages dir. |
| 94 | * The JSON file must be named $domain-$locale-$script_slug.json. |
| 95 | * Note: The script must be registered before this function is called. |
| 96 | * |
| 97 | * @param [string] $textdomain The text domain. |
| 98 | * @param [string] $script_handle The handle of the registered script. |
| 99 | * @param [string] $languages_dir The directory containing the .json file for the script. |
| 100 | * @return boolean |
| 101 | */ |
| 102 | public static function load_js_translations( $textdomain, $script_handle, $languages_dir ) { |
| 103 | return wp_set_script_translations( |
| 104 | $script_handle, |
| 105 | $textdomain, |
| 106 | $languages_dir |
| 107 | ); |
| 108 | } |
| 109 | } |