• Hello there,

    Was having an issue with my dynamic text not being tranlated when going back and forth form one language to another. The problem that I had is that the translation files were loaded prior to Polylang having a chance to update the cookie and setting it the proper language.

    After a bit of troubleshooting and trying a few different options, I came up with the below to always set the locale from the url when loading the theme textdomain in function load_theme_textdomain() of I10n.

    Add the following filter before load_child_theme_textdomain is being exectuted or load_theme_textdomain if you are creating your own theme.

    add_filter('theme_locale', 'CHLD_FixLocale',1,2 );
    
    function CHLD_FixLocale($currentLocale,$theme){
    	$uri = rtrim($_SERVER['REQUEST_URI'],'/');
    	$urlparts = explode('/', $uri);
    	$lastPartOfUrl = $urlparts[count($urlparts)-1];
    
    	$upperLastPart = strtoupper($lastPartOfUrl);
    	if($upperLastPart == 'FR' || $upperLastPart == 'EN'){
    		$localeLang = substr($currentLocale, 0,2);
    		if($upperLastPart == strtoupper($localeLang)){
    			return $currentLocale;
    		} else {
    			return $upperLastPart == 'FR' ? 'fr_FR' : 'en_US';
    		}
    	}
    
    	$postDb = new PostDb();
    	$id = $postDb->GetPostIdByName($lastPartOfUrl);
    
    	if ( $id  != null){
    		$db = new TermsDb();
    		$serialized = $db->GetObjectLanguage($id);
    		$deserialized = unserialize($serialized);
    		return $deserialized['locale'];
    	}	
    
    	return $currentLocale;
    }

    Here are the objects being called by the above function:

    class TermsDb {
    	public function GetObjectLanguage($objectId){
    		global $wpdb;
    		$query = "SELECT tt.description
    					FROM wp_term_taxonomy tt
            	      INNER JOIN wp_term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
            				 AND tt.taxonomy = 'language'
    				   WHERE tr.object_id = " . $objectId;
    
    		$results = $wpdb->get_results($query);
    		return $results[0]->description;
    	}
    }
    class PostDb {
    	public function GetPostIdByName($postname){
    		global $wpdb;
    		$query = "SELECT ID
    					FROM wp_posts
            	       WHERE post_name = '" . $postname . "'";
    
    		$results = $wpdb->get_results($query);
    		if($results){
    			return $results[0]->ID;
    		} else {
    			return null;
    		}
    	}
    }

    Now, I am no WordPress expert and I know that I could have used get_posts() to get to the db as opposed to going straight to the db, but hey, running out of time and since this is in my customized child theme, I took the right to do it that way:)

    Anyhow, not sure if this can be made more generic and incorporated into the polylang plugin or if there is a more elegant way of doing this, but this problem costed me quite a few hours of troubleshooting and I felt that it might be worth mentioning:)

    You can try it on wformation.com

    Thanks!

    https://www.ads-software.com/plugins/polylang/

  • The topic ‘I10n theme transalation vs back and forth from 1 language to another’ is closed to new replies.