function bogo_locale($locale)
{
    global $wp_rewrite, $wp_query;
    if (!did_action('plugins_loaded')) {
        return $locale;
    }
    if (is_admin()) {
        return bogo_get_user_locale();
    }
    $default_locale = bogo_get_default_locale();
    if (!empty($wp_query->query_vars)) {
        if (($lang = get_query_var('lang')) && ($closest = bogo_get_closest_locale($lang))) {
            return $closest;
        } else {
            return $default_locale;
        }
    }
    if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) {
        $url = is_ssl() ? 'https://' : 'http://';
        $url .= $_SERVER['HTTP_HOST'];
        $url .= $_SERVER['REQUEST_URI'];
        $home = set_url_scheme(get_option('home'));
        $home = trailingslashit($home);
        $available_languages = bogo_available_languages();
        $available_languages = array_map('bogo_lang_slug', array_keys($available_languages));
        $available_languages = implode('|', $available_languages);
        $pattern = '#^' . preg_quote($home) . '(' . $available_languages . ')(/|$)#';
        if (preg_match($pattern, $url, $matches) && ($closest = bogo_get_closest_locale($matches[1]))) {
            return $closest;
        }
    }
    $lang = bogo_get_lang_from_url();
    if ($lang && ($closest = bogo_get_closest_locale($lang))) {
        return $closest;
    }
    $locale = $default_locale;
    return $locale;
}
function bogo_parse_query($query)
{
    $qv =& $query->query_vars;
    if (!empty($qv['bogo_suppress_locale_query'])) {
        return;
    }
    if (isset($qv['post_type']) && 'any' != $qv['post_type']) {
        $localizable = array_filter((array) $qv['post_type'], 'bogo_is_localizable_post_type');
        if (empty($localizable)) {
            $qv['bogo_suppress_locale_query'] = true;
            return;
        }
    }
    $lang = isset($qv['lang']) ? $qv['lang'] : '';
    if (is_admin()) {
        $locale = $lang;
    } else {
        if ($lang) {
            $locale = bogo_get_closest_locale($lang);
        } else {
            $locale = get_locale();
        }
        if (empty($locale)) {
            $locale = bogo_get_default_locale();
        }
    }
    if (empty($locale) || !bogo_is_available_locale($locale)) {
        $qv['bogo_suppress_locale_query'] = true;
        return;
    }
    $qv['lang'] = $locale;
    if (is_admin()) {
        return;
    }
    if ($query->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front')) {
        $query_keys = array_keys(wp_parse_args($query->query));
        $query_keys = array_diff($query_keys, array('preview', 'page', 'paged', 'cpage', 'lang'));
        if (empty($query_keys)) {
            $query->is_page = true;
            $query->is_singular = true;
            $query->is_home = false;
            $qv['page_id'] = get_option('page_on_front');
            if (!empty($qv['paged'])) {
                $qv['page'] = $qv['paged'];
                unset($qv['paged']);
            }
        }
    }
    if ('' != $qv['pagename']) {
        $query->queried_object = bogo_get_page_by_path($qv['pagename'], $locale);
        if (!empty($query->queried_object)) {
            $query->queried_object_id = (int) $query->queried_object->ID;
        } else {
            unset($query->queried_object);
        }
        if ('page' == get_option('show_on_front') && isset($query->queried_object_id) && $query->queried_object_id == get_option('page_for_posts')) {
            $query->is_page = false;
            $query->is_home = true;
            $query->is_posts_page = true;
        }
    }
}