function xapp_setup_language_joomla($lang = null)
{
    if (!function_exists('XAPP_TEXT')) {
        $langToUse = $lang !== null ? $lang : JLanguage::getInstance(null)->getDefault();
        //;XApp_Service_Entry_Utils::getBrowserDefaultLanguage();
        XApp_Service_Entry_Utils::$languageClass = JLanguage::getInstance($langToUse);
        XApp_Service_Entry_Utils::$language = $langToUse;
        function XAPP_TEXT($string, $lang = null, $component = null, $arguments = null)
        {
            $langToUse = $lang !== null ? $lang : JLanguage::getInstance(null)->getDefault();
            //;XApp_Service_Entry_Utils::getBrowserDefaultLanguage();
            if ($lang == null) {
                $lang = '' . $langToUse;
            }
            $result = '' . $string;
            /***
             * Catch language change
             */
            if ($lang !== null && XApp_Service_Entry_Utils::$language != null) {
                if (XApp_Service_Entry_Utils::$language !== $lang) {
                    $newInstance = JLanguage::getInstance($lang);
                    if ($newInstance) {
                        XApp_Service_Entry_Utils::$languageClass = JLanguage::getInstance($lang);
                        JFactory::$language = XApp_Service_Entry_Utils::$languageClass;
                    }
                }
            }
            //no lang specified
            if (XApp_Service_Entry_Utils::$languageClass == null) {
                $newInstance = JLanguage::getInstance($langToUse);
                if ($newInstance != null) {
                    XApp_Service_Entry_Utils::$languageClass = $newInstance;
                }
            }
            if (XApp_Service_Entry_Utils::$languageClass !== null) {
                if ($component) {
                    XApp_Service_Entry_Utils::$languageClass->load($component);
                }
                $result = XApp_Service_Entry_Utils::$languageClass->_($string, true);
                if ($result !== null && $result !== $string && strlen($result) > 0) {
                    return $result;
                }
            }
            $result = JText::_($string, true);
            if (strlen($result) > 0) {
                return $result;
            }
            return $string;
        }
        if (!function_exists('XAPP_TEXT_FORMATTED')) {
            function XAPP_TEXT_FORMATTED($string, $args = array())
            {
                $toFormat = XAPP_TEXT($string, null, null, null);
                return vsprintf($toFormat, $args);
            }
        }
    }
}