Skip to content

Commit

Permalink
Fix switch to default language from site url
Browse files Browse the repository at this point in the history
  • Loading branch information
spleen1981 committed Mar 18, 2024
1 parent 046b8c6 commit 8c9fc82
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/modules/slugs/early_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
add_filter( 'qtranslate_language_detect_redirect', 'qtranxf_slugs_language_detect_redirect', 600, 3 );

/**
* Disables all redirections except when site url is requested, if default language is hidden. In this case url is redirected to default language site url.
* Disables default redirection when language is not detectable from url (e.g. default language url with hide_default_language activated).
* Exception is made for site_url(), for which language detection feature is preserved.
*
*
* @see qtranxf_check_url_maybe_redirect
* @param string $url_lang proposed target URL for the active language to redirect to.
Expand All @@ -14,23 +16,16 @@
*/
function qtranxf_slugs_language_detect_redirect($url_lang, $url_orig, $url_info): string {
global $q_config;

if ( ( site_url().'/' === $url_orig && $q_config['hide_default_language'] ) || empty( $url_info['lang_url'] ) ){

$url_res = qtranxf_convertURL( $url_orig, $q_config['default_language'],false,true );

if ( qtranxf_detect_language_front( $url_info ) != $q_config['default_language'] ) {

if ( $url_info['cookie_front_or_admin_found'] )
qtranxf_set_language_cookie( $q_config['default_language'] );

if ( $url_res == $url_orig )
$q_config['url_info'] = qtranxf_url_set_language( $urlinfo, $q_config['default_language'], $showLanguage );

}

} else
$url_res = $url_orig;

return $url_res;
/* Make sure urls with no lang info are treated as default language, unless it's site_url */
if ( untrailingslashit( site_url() ) != untrailingslashit( $url_orig ) && empty( $url_info['lang_url'] ) ){
return qtranxf_convertURL( $url_orig, $q_config['default_language'], false, true );
/* The following fixes the case when the browser removes language marker from default language site url (caching?) passing directly _$SERVER['REQUEST_URI'] with language info removed.
* In this case is not possible to switch to default language from site url.
* Application of this hack is narrowed down to the cases where referer is site url in current language, to preserve language detection feature as much as possible.
* @TODO: check if a cleaner fix is applicable. */
} else if ( $q_config['hide_default_language'] && untrailingslashit( site_url() ) == untrailingslashit( $url_orig ) && untrailingslashit($url_lang) === untrailingslashit( wp_get_raw_referer() ) ) {
return qtranxf_convertURL( $url_orig, $q_config['default_language'], false, true );
}
/* All other cases follow default behaviour */
return $url_lang;
}

0 comments on commit 8c9fc82

Please sign in to comment.