Skip to content

Commit

Permalink
Add Unpublished Content Language on Language Package Install (#11867)
Browse files Browse the repository at this point in the history
* initial commit

* Update language.php

* Update en-GB.lib_joomla.ini

* Update en-GB.lib_joomla.ini

* ups

* Update language.php

* Update language.php

* Update language.php

* change native to $siteLanguageManifest['name']

* allow to install published content languages on install 1

* allow to install published content languages on install 2

* solve remaining issues

* cs

* fix language names and simplify

* cs

* improve code and deprecate unneed code

* fix title issues (as possible with current xml scenario)

* remove non existent metadata values

* shoudl not be row, it should be tableLanguage

* don't fallback to en-GB native title
  • Loading branch information
andrepereiradasilva authored and rdeutz committed Oct 1, 2016
1 parent 57af617 commit c2645d5
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 21 deletions.
1 change: 1 addition & 0 deletions administrator/language/en-GB/en-GB.lib_joomla.ini
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ JLIB_INSTALLER_ERROR_EXTENSION_INVALID_CLIENT_IDENTIFIER="Invalid client identif
JLIB_INSTALLER_ERROR_PACK_UNINSTALL_UNKNOWN_EXTENSION="Attempting to uninstall unknown extension from package. This extension may have already been removed earlier."
JLIB_INSTALLER_NOT_ERROR="If the error is related to the installation of TinyMCE language files it has no effect on the installation of the language(s). Some language packs created prior to Joomla! 3.2.0 may try to install separate TinyMCE language files. As these are now included in the core they no longer need to be installed."
JLIB_INSTALLER_UPDATE_LOG_QUERY="Ran query from file %1$s. Query text: %2$s."
JLIB_INSTALLER_WARNING_UNABLE_TO_INSTALL_CONTENT_LANGUAGE="Unable to create a content language for %s language: %s"

JLIB_JS_AJAX_ERROR_CONNECTION_ABORT="A connection abort has occurred while fetching the JSON data."
JLIB_JS_AJAX_ERROR_NO_CONTENT="No content was returned."
Expand Down
4 changes: 4 additions & 0 deletions installation/controller/install/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function execute()
{
// Install selected languages
$model->install($lids);

// Publish the Content Languages.
$model->publishContentLanguages();

$app->enqueueMessage(JText::_('INSTL_LANGUAGES_MORE_LANGUAGES'), 'notice');
}

Expand Down
18 changes: 0 additions & 18 deletions installation/controller/setdefaultlanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,6 @@ public function execute()

foreach ($siteLanguages as $siteLang)
{
// Add Language Manager: Content Languages
$tableLanguage = JTable::getInstance('Language');

// Search if just added
$return = $tableLanguage->load(array('lang_code' => $siteLang->language));

if ($return === false)
{
$sefLangString = $model->getSefString($siteLang, $siteLanguages);

if (!$model->addLanguage($siteLang, $sefLangString))
{
$app->enqueueMessage(JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_CREATE_CONTENT_LANGUAGE', $siteLang->name));

continue;
}
}

if (!$model->addMenuGroup($siteLang))
{
$app->enqueueMessage(JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_CREATE_MENU', $siteLang->name), 'warning');
Expand Down
35 changes: 35 additions & 0 deletions installation/model/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,39 @@ public function addModuleInModuleMenu($moduleId)
return true;
}

/**
* Publish the Installed Content Languages.
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function publishContentLanguages()
{
$app = JFactory::getApplication();

// Publish the Content Languages.
$tableLanguage = JTable::getInstance('Language');

$siteLanguages = $this->getInstalledlangs('site');

// For each content language.
foreach ($siteLanguages as $siteLang)
{
if ($tableLanguage->load(array('lang_code' => $siteLang->language, 'published' => 0)))
{
if (!$tableLanguage->publish())
{
$app->enqueueMessage(JText::sprintf('INSTL_DEFAULTLANGUAGE_COULD_NOT_CREATE_CONTENT_LANGUAGE', $siteLang->name), 'warning');

continue;
}
}
}

return true;
}

/**
* Gets a unique language SEF string.
*
Expand All @@ -701,6 +734,7 @@ public function addModuleInModuleMenu($moduleId)
* @return string
*
* @since 3.2
* @depreacted 4.0 Not used anymore.
*/
public function getSefString($itemLanguage, $siteLanguages)
{
Expand Down Expand Up @@ -737,6 +771,7 @@ public function getSefString($itemLanguage, $siteLanguages)
* @return boolean
*
* @since 3.2
* @depreacted 4.0 Not used anymore.
*/
public function addLanguage($itemLanguage, $sefLangString)
{
Expand Down
1 change: 1 addition & 0 deletions language/en-GB/en-GB.lib_joomla.ini
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ JLIB_INSTALLER_ERROR_EXTENSION_INVALID_CLIENT_IDENTIFIER="Invalid client identif
JLIB_INSTALLER_ERROR_PACK_UNINSTALL_UNKNOWN_EXTENSION="Attempting to uninstall unknown extension from package. This extension may have already been removed earlier."
JLIB_INSTALLER_NOT_ERROR="If the error is related to the installation of TinyMCE language files it has no effect on the installation of the language(s). Some language packs created prior to Joomla! 3.2.0 may try to install separate TinyMCE language files. As these are now included in the core they no longer need to be installed."
JLIB_INSTALLER_UPDATE_LOG_QUERY="Ran query from file %1$s. Query text: %2$s."
JLIB_INSTALLER_WARNING_UNABLE_TO_INSTALL_CONTENT_LANGUAGE="Unable to create a content language for %s language: %s"

JLIB_JS_AJAX_ERROR_CONNECTION_ABORT="A connection abort has occurred while fetching the JSON data."
JLIB_JS_AJAX_ERROR_NO_CONTENT="No content was returned."
Expand Down
109 changes: 106 additions & 3 deletions libraries/cms/installer/adapter/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ public function install()

$basePath = $client->path;
$clientId = $client->id;
$element = $this->getManifest()->files;
$element = $this->getManifest()->files;

return $this->_install($cname, $basePath, $clientId, $element);
}
else
{
// No client attribute was found so we assume the site as the client
$cname = 'site';
$cname = 'site';
$basePath = JPATH_SITE;
$clientId = 0;
$element = $this->getManifest()->files;
$element = $this->getManifest()->files;

return $this->_install($cname, $basePath, $clientId, $element);
}
Expand Down Expand Up @@ -300,6 +300,68 @@ protected function _install($cname, $basePath, $clientId, &$element)
return false;
}

// Create an unpublished content language.
if ((int) $clientId === 0)
{
// Load the site language manifest.
$siteLanguageManifest = JLanguage::parseXMLLanguageFile(JPATH_SITE . '/language/' . $this->tag . '/' . $this->tag . '.xml');

// Set the content language title as name in site xx-XX.ini.
$contentLanguageTitle = $siteLanguageManifest['name'];

// Set the content language as installation native title language variable, fallback to content language title.
$contentLanguageNativeTitle = $contentLanguageTitle;

if (file_exists(JPATH_INSTALLATION . '/language/' . $this->tag . '/' . $this->tag . '.xml'))
{
$installationLanguage = new JLanguage($this->tag);
$installationLanguage->load('', JPATH_INSTALLATION);

if ($installationLanguage->hasKey('INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME'))
{
// Make sure it will not use the en-GB fallback.
$defaultLanguage = new JLanguage('en-GB');
$defaultLanguage->load('', JPATH_INSTALLATION);

$defaultLanguageNativeTitle = $defaultLanguage->_('INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME');
$installationLanguageNativeTitle = $installationLanguage->_('INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME');

if ($defaultLanguageNativeTitle != $installationLanguageNativeTitle)
{
$contentLanguageNativeTitle = $installationLanguage->_('INSTL_DEFAULTLANGUAGE_NATIVE_LANGUAGE_NAME');
}
}
}

// Prepare language data for store.
$languageData = array(
'lang_id' => 0,
'lang_code' => $this->tag,
'title' => $contentLanguageTitle,
'title_native' => $contentLanguageNativeTitle,
'sef' => $this->getSefString($this->tag),
'image' => strtolower(str_replace('-', '_', $this->tag)),
'published' => 0,
'ordering' => 0,
'access' => (int) JFactory::getConfig()->get('access', 1),
'description' => '',
'metakey' => '',
'metadesc' => '',
'sitename' => '',
);

$tableLanguage = JTable::getInstance('language');

if (!$tableLanguage->bind($languageData) || !$tableLanguage->check() || !$tableLanguage->store() || !$tableLanguage->reorder())
{
JLog::add(
JText::sprintf('JLIB_INSTALLER_WARNING_UNABLE_TO_INSTALL_CONTENT_LANGUAGE', $siteLanguageManifest['name'], $tableLanguage->getError()),
JLog::WARNING,
'jerror'
);
}
}

// Clobber any possible pending updates
$update = JTable::getInstance('update');
$uid = $update->find(array('element' => $this->get('tag'), 'type' => 'language', 'folder' => ''));
Expand All @@ -312,6 +374,47 @@ protected function _install($cname, $basePath, $clientId, &$element)
return $row->get('extension_id');
}


/**
* Gets a unique language SEF string.
*
* This function checks other existing language with the same code, if they exist provides a unique SEF name.
* For instance: en-GB, en-US and en-AU will share the same SEF code by default: www.mywebsite.com/en/
* To avoid this conflict, this function creates an specific SEF in case of existing conflict:
* For example: www.mywebsite.com/en-au/
*
* @param string $itemLanguageTag Language Tag.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function getSefString($itemLanguageTag)
{
$langs = explode('-', $itemLanguageTag);
$prefixToFind = $langs[0];
$numberPrefixesFound = 0;

// Get the sef value of all current content languages.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->qn('sef'))
->from($db->qn('#__languages'));
$db->setQuery($query);

$siteLanguages = $db->loadObjectList();

foreach ($siteLanguages as $siteLang)
{
if ($siteLang->sef === $prefixToFind)
{
$numberPrefixesFound++;
}
}

return $numberPrefixesFound === 0 ? $prefixToFind : strtolower($itemLanguageTag);
}

/**
* Custom update method
*
Expand Down

0 comments on commit c2645d5

Please sign in to comment.