| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Merchant - Multi Language Translator. |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'Merchant_Translator' ) ) { |
| 11 |
class Merchant_Translator { |
| 12 |
|
| 13 |
/** |
| 14 |
* @var Merchant_Language_Strategy $language_strategy The language strategy class. |
| 15 |
*/ |
| 16 |
private static $language_strategy; |
| 17 |
|
| 18 |
/** |
| 19 |
* Merchant_Translator constructor. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
self::set_language_strategy(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Check if Polylang is active. |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public static function is_polylang_active() { |
| 31 |
return function_exists( 'pll_count_posts' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Check if WPML is active. |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public static function is_wpml_active() { |
| 40 |
return function_exists( 'icl_object_id' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set the translator class. |
| 45 |
*/ |
| 46 |
public static function set_language_strategy() { |
| 47 |
if ( self::is_polylang_active() ) { |
| 48 |
$translator_object = new Merchant_PolyLang_Support(); |
| 49 |
} elseif ( self::is_wpml_active() ) { |
| 50 |
$translator_object = new Merchant_WPML_Support(); |
| 51 |
} else { |
| 52 |
$translator_object = new Merchant_No_Plugin_Support(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Filter the translator class. |
| 57 |
* |
| 58 |
* @param Merchant_Language_Strategy $translator_object The translator class object. |
| 59 |
* |
| 60 |
* @since 1.8 |
| 61 |
*/ |
| 62 |
self::$language_strategy = apply_filters( 'merchant_translator_class_object', $translator_object ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Register a string for translation. |
| 67 |
* |
| 68 |
* @param string $string_to_register The string to translate. |
| 69 |
* @param string $context The context of the string. |
| 70 |
*/ |
| 71 |
public static function register_string( $string_to_register, $context = 'merchant' ) { |
| 72 |
self::set_language_strategy(); |
| 73 |
self::$language_strategy->register_string( $string_to_register, $context ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Translate a string. |
| 78 |
* |
| 79 |
* @param string $string_to_translate The string to translate. |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public static function translate( $string_to_translate ) { |
| 84 |
self::set_language_strategy(); |
| 85 |
$translated_string = self::$language_strategy->translate_string( $string_to_translate ); |
| 86 |
|
| 87 |
/** |
| 88 |
* Filter the translated string. |
| 89 |
* |
| 90 |
* @param string $string_to_translate The string to translate. |
| 91 |
* @param string $translated_string The translated string. |
| 92 |
* |
| 93 |
* @since 1.8 |
| 94 |
*/ |
| 95 |
return apply_filters( 'merchant_multi_lang_translated_string', $translated_string, $string_to_translate ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|