fgirard on "[Plugin: Polylang] I10n theme transalation vs back and forth from 1 language to another"

ساخت وبلاگ

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 havinga 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 in 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)){ retu $currentLocale; } else { retu $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); retu $deserialized['locale']; } retu $currentLocale;
}

Here are the objects being called by the above functions:

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); retu $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){ retu $results[0]->ID; } else { retu null; } }
}

Now, I am no WordPress expert and I know that I could have used wpdb 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 mentining:)

You can try it on wformation.com

Thanks!

https://wordpress.org/plugins/polylang/

WordPress ...
ما را در سایت WordPress دنبال می کنید

برچسب : نویسنده : استخدام کار wpss بازدید : 326 تاريخ : پنجشنبه 7 مرداد 1395 ساعت: 23:31