protected function _getRoute($query, $escape)
    {
        $current = JFactory::getApplication();

        if ($current->getName() !== $this->getApplication())
        {
            $application = JApplicationCms::getInstance($this->getConfig()->application);

            // Force route application during route build.
            JFactory::$application = $application;

            // Get the router.
            $router = $application->getRouter();

            $url = 'index.php?'.http_build_query($query, '', '&');

            // Build route.
            $route = $router->build($url);

            // Revert application change.
            JFactory::$application = $current;

            $route = $route->toString(array('path', 'query', 'fragment'));

            // Check if we need to remove "administrator" from the path
            if ($current->isAdmin() && $application->getName() == 'site')
            {
                $base = JUri::base('true');

                $replacement = explode('/', $base);

                array_pop($replacement);

                $replacement = implode('/', $replacement);

                $base = str_replace('/', '\/', $base);

                $route = preg_replace('/^' . $base . '/', $replacement, $route);
            }

            // Replace spaces.
            $route = preg_replace('/\s/u', '%20', $route);

            if ($escape) {
                $route = htmlspecialchars($route, ENT_COMPAT, 'UTF-8');
            }
        }
        else $route = JRoute::_('index.php?'.http_build_query($query, '', '&'), $escape);

        return $route;
    }