Back to LanguageHelper class

Method getInstalledLanguages

public static array
getInstalledLanguages
(mixed $clientId = null, mixed $processMetaData = false, mixed $processManifest = false, mixed $pivot = 'element', mixed $orderField = null, mixed $orderDirection = null)
Get a list of installed languages.
Parameters
  • int $clientId The client app id.
  • bool $processMetaData Fetch Language metadata.
  • bool $processManifest Fetch Language manifest.
  • string $pivot The pivot of the returning array.
  • string $orderField Field to order the results.
  • string $orderDirection Direction to order the results.
Returns
  • array Array with the installed languages.
Since
  • 3.7.0

Method getInstalledLanguages - Source code

/**
 * Get a list of installed languages.
 *
 * @param   integer  $clientId         The client app id.
 * @param   boolean  $processMetaData  Fetch Language metadata.
 * @param   boolean  $processManifest  Fetch Language manifest.
 * @param   string   $pivot            The pivot of the returning array.
 * @param   string   $orderField       Field to order the results.
 * @param   string   $orderDirection   Direction to order the results.
 *
 * @return  array  Array with the installed languages.
 *
 * @since   3.7.0
 */
public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processManifest = false, $pivot = 'element', $orderField = null, $orderDirection = null)
{
    static $installedLanguages = null;
    if ($installedLanguages === null) {
        /** @var OutputController $cache */
        $cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController('output', ['defaultgroup' => 'com_languages']);
        if ($cache->contains('installedlanguages')) {
            $installedLanguages = $cache->get('installedlanguages');
        } else {
            $db = Factory::getDbo();
            $query = $db->getQuery(true)->select([$db->quoteName('element'), $db->quoteName('name'), $db->quoteName('client_id'), $db->quoteName('extension_id')])->from($db->quoteName('#__extensions'))->where([$db->quoteName('type') . ' = ' . $db->quote('language'), $db->quoteName('state') . ' = 0', $db->quoteName('enabled') . ' = 1']);
            $installedLanguages = $db->setQuery($query)->loadObjectList();
            $cache->store($installedLanguages, 'installedlanguages');
        }
    }
    $clients = $clientId === null ? array(0, 1) : array((int) $clientId);
    $languages = array(0 => array(), 1 => array());
    foreach ($installedLanguages as $language) {
        // If the language client is not needed continue cycle. Drop for performance.
        if (!\in_array((int) $language->client_id, $clients)) {
            continue;
        }
        $lang = $language;
        if ($processMetaData || $processManifest) {
            $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR;
            $metafile = self::getLanguagePath($clientPath, $language->element) . '/langmetadata.xml';
            if (!is_file($metafile)) {
                $metafile = self::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml';
            }
            // Process the language metadata.
            if ($processMetaData) {
                try {
                    $lang->metadata = self::parseXMLLanguageFile($metafile);
                } catch (\Exception $e) {
                    Log::add(Text::sprintf('JLIB_LANGUAGE_ERROR_CANNOT_LOAD_METAFILE', $language->element, $metafile), Log::WARNING, 'language');
                    continue;
                }
                // No metadata found, not a valid language. Fail silently.
                if (!\is_array($lang->metadata)) {
                    Log::add(Text::sprintf('JLIB_LANGUAGE_ERROR_CANNOT_LOAD_METADATA', $language->element, $metafile), Log::WARNING, 'language');
                    continue;
                }
            }
            // Process the language manifest.
            if ($processManifest) {
                try {
                    $lang->manifest = Installer::parseXMLInstallFile($metafile);
                } catch (\Exception $e) {
                    Log::add(Text::sprintf('JLIB_LANGUAGE_ERROR_CANNOT_LOAD_METAFILE', $language->element, $metafile), Log::WARNING, 'language');
                    continue;
                }
                // No metadata found, not a valid language. Fail silently.
                if (!\is_array($lang->manifest)) {
                    Log::add(Text::sprintf('JLIB_LANGUAGE_ERROR_CANNOT_LOAD_METADATA', $language->element, $metafile), Log::WARNING, 'language');
                    continue;
                }
            }
        }
        $languages[$language->client_id][] = $lang;
    }
    // Order the list, if needed.
    if ($orderField !== null && $orderDirection !== null) {
        $orderDirection = strtolower($orderDirection) === 'desc' ? -1 : 1;
        foreach ($languages as $cId => $language) {
            // If the language client is not needed continue cycle. Drop for performance.
            if (!\in_array($cId, $clients)) {
                continue;
            }
            $languages[$cId] = ArrayHelper::sortObjects($languages[$cId], $orderField, $orderDirection, true, true);
        }
    }
    // Add the pivot, if needed.
    if ($pivot !== null) {
        foreach ($languages as $cId => $language) {
            // If the language client is not needed continue cycle. Drop for performance.
            if (!\in_array($cId, $clients)) {
                continue;
            }
            $languages[$cId] = ArrayHelper::pivot($languages[$cId], $pivot);
        }
    }
    return $clientId !== null ? $languages[$clientId] : $languages;
}