| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Translate implements HC3_ITranslate |
| 3 |
{ |
| 4 |
protected $domain = 'hitcode'; |
| 5 |
protected $locale = ''; |
| 6 |
protected $dir = ''; |
| 7 |
|
| 8 |
public function __construct( $domain, $pluginDir, $locale = '' ) |
| 9 |
{ |
| 10 |
$this->domain = $domain; |
| 11 |
$this->locale = $locale; |
| 12 |
|
| 13 |
$langDir = plugin_basename($pluginDir) . '/languages'; |
| 14 |
$langFullDir = $pluginDir . '/languages'; |
| 15 |
|
| 16 |
add_filter( 'locale', array($this, 'setWpLocale') ); |
| 17 |
// echo "LOAD TEXTDOMAIN '$domain', $langDir<br>\n"; |
| 18 |
|
| 19 |
// $load_result = load_plugin_textdomain( $domain, '', $langDir ); |
| 20 |
$locale = get_locale(); |
| 21 |
|
| 22 |
$userLocale = get_user_locale(); |
| 23 |
if( strlen($userLocale) ){ |
| 24 |
$locale = $userLocale; |
| 25 |
} |
| 26 |
|
| 27 |
$locale = apply_filters( 'plugin_locale', $locale, $domain ); |
| 28 |
|
| 29 |
$mofile = $domain . '-' . $locale . '.mo'; |
| 30 |
$fullMofile = $langFullDir . '/' . $mofile; |
| 31 |
$load_result = load_textdomain( $domain, $fullMofile ); |
| 32 |
|
| 33 |
if( ! $load_result ){ |
| 34 |
$load_result = load_plugin_textdomain( $domain, '', $langDir ); |
| 35 |
} |
| 36 |
|
| 37 |
// echo $load_result ? "SLOADOK<br>\n" : "SLOADFAIL<br>\n"; |
| 38 |
// exit; |
| 39 |
|
| 40 |
remove_filter( 'locale', array($this, 'setWpLocale') ); |
| 41 |
|
| 42 |
$this->dir = $langFullDir; |
| 43 |
} |
| 44 |
|
| 45 |
public function getLocale() |
| 46 |
{ |
| 47 |
return $this->locale; |
| 48 |
} |
| 49 |
|
| 50 |
public function setWpLocale( $locale ) |
| 51 |
{ |
| 52 |
if( $this->locale ){ |
| 53 |
$locale = $this->locale; |
| 54 |
} |
| 55 |
return $locale; |
| 56 |
} |
| 57 |
|
| 58 |
public function getOptions() |
| 59 |
{ |
| 60 |
$return = array(); |
| 61 |
|
| 62 |
$files = HC3_Functions::listFiles( $this->dir, 'file', '.mo' ); |
| 63 |
reset( $files ); |
| 64 |
foreach( $files as $f ){ |
| 65 |
if( substr($f, 0, strlen($this->domain) + 1) != $this->domain . '-' ){ |
| 66 |
continue; |
| 67 |
} |
| 68 |
$option = substr( $f, strlen($this->domain) + 1, -3 ); |
| 69 |
$return[] = $option; |
| 70 |
} |
| 71 |
|
| 72 |
return $return; |
| 73 |
} |
| 74 |
|
| 75 |
public function __( $str ) |
| 76 |
{ |
| 77 |
return __($str, $this->domain); |
| 78 |
} |
| 79 |
|
| 80 |
public function _x( $str, $context ) |
| 81 |
{ |
| 82 |
return _x($str, $context, $this->domain); |
| 83 |
} |
| 84 |
|
| 85 |
public function _n( $singular, $plural, $count ) |
| 86 |
{ |
| 87 |
return _n($singular, $plural, $count, $this->domain); |
| 88 |
} |
| 89 |
|
| 90 |
public function translate( $string ) |
| 91 |
{ |
| 92 |
$string = "" . $string; |
| 93 |
preg_match_all( '/__(.+)__/U', $string, $ma ); |
| 94 |
|
| 95 |
$replace = array(); |
| 96 |
$count = count($ma[0]); |
| 97 |
for( $ii = 0; $ii < $count; $ii++ ){ |
| 98 |
$what = $ma[0][$ii]; |
| 99 |
$replace[$what] = $what; |
| 100 |
} |
| 101 |
|
| 102 |
foreach( $replace as $what => $from ){ |
| 103 |
$from = substr( $what, 2, -2 ); |
| 104 |
$to = $this->__($from); |
| 105 |
$string = str_replace( $what, $to, $string ); |
| 106 |
} |
| 107 |
|
| 108 |
return $string; |
| 109 |
} |
| 110 |
} |