Skip to content

Commit

Permalink
Merge pull request yireo-joomla#11 from hikashop-nicolas/master
Browse files Browse the repository at this point in the history
Properly reload ALL the language files already loaded before the language switch
  • Loading branch information
jissereitsma authored Nov 20, 2019
2 parents 0f7b620 + dcbb321 commit 3c99b25
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions source/plugins/system/languagedomains/languagedomains.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public function __construct(&$subject, $config)
*/
public function onAfterInitialise()
{
// Store the previous language tag in the plugin
$this->oldLanguageTag = JFactory::getLanguage()
->getTag();

// Remove the cookie if it exists
$this->cleanLanguageCookie();

Expand Down Expand Up @@ -224,6 +228,58 @@ public function onAfterInitialise()
$this->helper->resetDefaultLanguage();
}

/**
* Function to get the paths of all the language files loaded before the language switch
*/
function getPaths() {
$loadOverride = function($filename = null) {
// Get the path of all the translation files already loaded
return $this->paths;
};

// We use Closure here as we need to access private attributes of JLanguage
$lang = JFactory::getLanguage();
$loadOverrideCB = $loadOverride->bindTo($lang, 'JLanguage');
$this->paths = $loadOverrideCB();
}

/**
* Function to relaod all the language files in the new language after the language switch
*/
function reloadPaths($oldtag, $tag) {
$loadOverride = function($paths, $oldcode, $code) {
foreach($paths as $extension => $extensionPaths) {
foreach($extensionPaths as $fileName => $oldResult) {

// Replace the language code in the translation file path
$fileName = str_replace($oldcode, $code, $fileName);

// Parse the language file
$strings = $this->parse($fileName);
$result = false;

// Add the translations to JLanguage
if ($strings !== array()) {
$this->strings = array_replace($this->strings, $strings, $this->override);
$result = true;
}
// Record the result of loading the extension's file
if (!isset($this->paths[$extension]))
{
$this->paths[$extension] = array();
}

$this->paths[$extension][$fileName] = $result;
}
}
};

// We use Closure here as we need to access private attributes of JLanguage
$lang = JFactory::getLanguage();
$loadOverrideCB = $loadOverride->bindTo($lang, 'JLanguage');
$loadOverrideCB($this->paths, $oldtag, $tag);
}

/**
* Event onAfterRoute
*/
Expand Down Expand Up @@ -254,6 +310,9 @@ public function onAfterRoute()
{
return;
}

// Get the paths of all the language files loaded before the language switch
$this->getPaths();

// If this language is not included in this plugins configuration, set it as current
if (!$this->isLanguageBound($languageTag))
Expand All @@ -268,9 +327,8 @@ public function onAfterRoute()

$this->debug('Current language tag: ' . $languageTag);

$language = JFactory::getLanguage();
$option = $this->app->input->getCmd('option');
$language->load($option, JPATH_SITE, $languageTag, true);
// Relaod all the language files in the new language after the language switch
$this->reloadPaths($this->oldLanguageTag, $languageTag);

if (empty($languageTag))
{
Expand Down

0 comments on commit 3c99b25

Please sign in to comment.