| 1 |
<?php |
| 2 |
namespace YayMail; |
| 3 |
|
| 4 |
use YayMail\Utils\SingletonTrait; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; |
| 7 |
/** |
| 8 |
* I18n Logic |
| 9 |
* |
| 10 |
* @method static I18n get_instance() |
| 11 |
*/ |
| 12 |
class I18n { |
| 13 |
|
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
private function __construct() { |
| 17 |
add_action( 'init', [ $this, 'load_plugin_text_domain' ] ); |
| 18 |
add_filter( 'yaymail_translations', [ $this, 'get_translations' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public static function load_plugin_text_domain() { |
| 22 |
if ( function_exists( 'determine_locale' ) ) { |
| 23 |
$locale = determine_locale(); |
| 24 |
} else { |
| 25 |
$locale = is_admin() ? get_user_locale() : get_locale(); |
| 26 |
} |
| 27 |
|
| 28 |
unload_textdomain( 'yaymail' ); |
| 29 |
load_textdomain( 'yaymail', YAYMAIL_PLUGIN_PATH . 'i18n/languages/yaymail-' . $locale . '.mo' ); |
| 30 |
|
| 31 |
load_plugin_textdomain( 'yaymail', false, YAYMAIL_PLUGIN_PATH . 'i18n/languages/' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_translations() { |
| 35 |
$translations = get_translations_for_domain( 'yaymail' ); |
| 36 |
$messages = []; |
| 37 |
|
| 38 |
$entries = $translations->entries; |
| 39 |
foreach ( $entries as $key => $entry ) { |
| 40 |
$messages[ $entry->singular ] = $entry->translations; |
| 41 |
} |
| 42 |
|
| 43 |
return [ |
| 44 |
'locale_data' => [ |
| 45 |
'messages' => $messages, |
| 46 |
], |
| 47 |
]; |
| 48 |
} |
| 49 |
} |
| 50 |
|