Admin
3 years ago
Assets
3 years ago
Pages
3 years ago
PostTypes
3 years ago
Shortcodes
3 years ago
Users
3 years ago
ActionsService.php
3 years ago
PluginService.php
3 years ago
PluginServiceProvider.php
3 years ago
RecaptchaValidationService.php
3 years ago
ThemeServiceProvider.php
3 years ago
TranslationsServiceProvider.php
3 years ago
TranslationsServiceProvider.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress; |
| 4 | |
| 5 | use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 6 | |
| 7 | /** |
| 8 | * Register translations. |
| 9 | */ |
| 10 | class TranslationsServiceProvider implements ServiceProviderInterface { |
| 11 | /** |
| 12 | * Register all dependencies in the IoC container. |
| 13 | * |
| 14 | * @param \Pimple\Container $container Service container. |
| 15 | * @return void |
| 16 | */ |
| 17 | public function register( $container ) { |
| 18 | // Nothing to register. |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Bootstrap the service. |
| 23 | * |
| 24 | * @param \Pimple\Container $container Service container. |
| 25 | * @return void |
| 26 | */ |
| 27 | public function bootstrap( $container ) { |
| 28 | add_filter( 'loco_compile_single_json', [ $this, 'compileSingleJSON' ], 999, 2 ); |
| 29 | add_filter( 'load_script_translation_file', [ $this, 'loadSingleTranslationFile' ], 999, 3 ); |
| 30 | add_action( 'init', [ $this, 'loadPluginTextDomain' ], 0 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Compile javascript translations as a single file. |
| 35 | * We need to do this since we lazy load a lot of our scripts. |
| 36 | * |
| 37 | * @param string $path Path for the json file. |
| 38 | * @param string $po_path Path of the po. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function compileSingleJSON( $path, $po_path ) { |
| 43 | $info = pathinfo( $po_path ); |
| 44 | if ( 'surecart' === substr( $info['filename'], 0, 8 ) ) { |
| 45 | $path = $info['dirname'] . '/' . $info['filename'] . '.json'; |
| 46 | } |
| 47 | return $path; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Load the single translation file when the domain loads. |
| 52 | * |
| 53 | * @param string $file The file. |
| 54 | * @param string $handle The script handle. |
| 55 | * @param string $domain The domain. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function loadSingleTranslationFile( $file, $handle, $domain ) { |
| 60 | if ( 'surecart' === $domain && is_string( $file ) ) { |
| 61 | $first_part = substr( $file, 0, strpos( $file, 'plugins/surecart' ) ); |
| 62 | $file = $first_part . 'plugins/surecart-' . get_locale() . '.json'; |
| 63 | } |
| 64 | return $file; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This is needed for Loco translate to work properly. |
| 69 | */ |
| 70 | public function loadPluginTextDomain() { |
| 71 | load_plugin_textdomain( 'surecart', false, dirname( plugin_basename( SURECART_PLUGIN_FILE ) ) . '/languages/' ); |
| 72 | } |
| 73 | } |
| 74 |