first commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* @author OnTheGo Systems
|
||||
*/
|
||||
|
||||
namespace WPML\ST\MO\JustInTime;
|
||||
|
||||
use WPML\ST\MO\LoadedMODictionary;
|
||||
|
||||
class DefaultMO extends MO {
|
||||
|
||||
public function __construct( LoadedMODictionary $loaded_mo_dictionary, $locale ) {
|
||||
parent::__construct( $loaded_mo_dictionary, $locale, 'default' );
|
||||
}
|
||||
|
||||
protected function loadTextDomain() {
|
||||
load_default_textdomain( $this->locale );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\JustInTime;
|
||||
|
||||
use NOOP_Translations;
|
||||
use WPML\ST\MO\LoadedMODictionary;
|
||||
|
||||
class MO extends \MO {
|
||||
|
||||
/** @var LoadedMODictionary $loaded_mo_dictionary */
|
||||
private $loaded_mo_dictionary;
|
||||
|
||||
/** @var string $locale */
|
||||
protected $locale;
|
||||
|
||||
/** @var string $domain */
|
||||
private $domain;
|
||||
|
||||
/** @var bool $isLoading */
|
||||
private $isLoading = false;
|
||||
|
||||
/**
|
||||
* @param LoadedMODictionary $loaded_mo_dictionary
|
||||
* @param string $locale
|
||||
* @param string $domain
|
||||
*/
|
||||
public function __construct(
|
||||
LoadedMODictionary $loaded_mo_dictionary,
|
||||
$locale,
|
||||
$domain
|
||||
) {
|
||||
$this->loaded_mo_dictionary = $loaded_mo_dictionary;
|
||||
$this->locale = $locale;
|
||||
$this->domain = $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $singular
|
||||
* @param string $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function translate( $singular, $context = null ) {
|
||||
if ( $this->isLoading ) {
|
||||
return $singular;
|
||||
}
|
||||
|
||||
$this->load();
|
||||
return _x( $singular, $context, $this->domain );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $singular
|
||||
* @param string $plural
|
||||
* @param int $count
|
||||
* @param string $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function translate_plural( $singular, $plural, $count, $context = null ) {
|
||||
if ( $this->isLoading ) {
|
||||
return $count > 1 ? $plural : $singular;
|
||||
}
|
||||
|
||||
$this->load();
|
||||
return _nx( $singular, $plural, $count, $context, $this->domain );
|
||||
}
|
||||
|
||||
private function load() {
|
||||
$this->isLoading = true;
|
||||
$this->loadTextDomain();
|
||||
|
||||
if ( ! $this->isLoaded() ) {
|
||||
/**
|
||||
* If we could not load at least one MO file,
|
||||
* we need to assign the domain with a `NOOP_Translations`
|
||||
* object on the 'l10n' global.
|
||||
* This will prevent recursive loop on the current object.
|
||||
*/
|
||||
$GLOBALS['l10n'][ $this->domain ] = new NOOP_Translations();
|
||||
}
|
||||
|
||||
$this->isLoading = false;
|
||||
}
|
||||
|
||||
protected function loadTextDomain() {
|
||||
$this->loaded_mo_dictionary
|
||||
->getFiles( $this->domain, $this->locale )
|
||||
->each( function( $mofile ) {
|
||||
load_textdomain( $this->domain, $mofile );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* In some cases, themes or plugins are hooking on
|
||||
* `override_load_textdomain` so that the function
|
||||
* `load_textdomain` always returns `true` even
|
||||
* if the domain is not set on the global `$l10n`.
|
||||
*
|
||||
* That's why we need to check on the global `$l10n`.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isLoaded() {
|
||||
return isset( $GLOBALS['l10n'][ $this->domain ] )
|
||||
&& ! $GLOBALS['l10n'][ $this->domain ] instanceof self;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\JustInTime;
|
||||
|
||||
use WPML\ST\MO\LoadedMODictionary;
|
||||
|
||||
class MOFactory {
|
||||
|
||||
/** @var LoadedMODictionary $loaded_mo_dictionary */
|
||||
private $loaded_mo_dictionary;
|
||||
|
||||
public function __construct( LoadedMODictionary $loaded_mo_dictionary ) {
|
||||
$this->loaded_mo_dictionary = $loaded_mo_dictionary;
|
||||
}
|
||||
|
||||
/**
|
||||
* We need to rely on the loaded dictionary rather than `$GLOBALS['l10n]`
|
||||
* because a domain could have been loaded in a language that
|
||||
* does not have a MO file and so it won't be added to the `$GLOBALS['l10n]`.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param array $excluded_domains
|
||||
* @param array $cachedMoObjects
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get( $locale, array $excluded_domains, array $cachedMoObjects ) {
|
||||
$mo_objects = [
|
||||
'default' => isset( $cachedMoObjects['default'] )
|
||||
? $cachedMoObjects['default']
|
||||
: new DefaultMO( $this->loaded_mo_dictionary, $locale ),
|
||||
];
|
||||
|
||||
$excluded_domains[] = 'default';
|
||||
|
||||
foreach ( $this->loaded_mo_dictionary->getDomains( $excluded_domains ) as $domain ) {
|
||||
$mo_objects[ $domain ] = isset( $cachedMoObjects[ $domain ] )
|
||||
? $cachedMoObjects[ $domain ]
|
||||
: new MO( $this->loaded_mo_dictionary, $locale, $domain );
|
||||
}
|
||||
|
||||
return $mo_objects;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user