4

I've been looking for an answer to this question for a while. Buddypress works very well on multilingual websites, especially with "WPML" and "BuddyPress Multilingual" plugins, but there was still a problem with the global Activity Stream module (a bit like a social wall that allows everyone to publish content).

Indeed, no matter which language is selected, all publications in all languages are always mixed in the same feed. As there is no possibility (as far as I know) to display a feed for each language, I had to find a workaround.

So my idea was to store the language currently displayed when the user clicks on the "publish" button, add this value as an attribute on each post, then filter them with CSS.

Here is how it works:

Step 1 : Add a language class to body in order to identify the currently displayed language. This first step is the simplest but depends on the plugin you use (WPML, Polylang, Weglot, qTranslate...). For WPML, for example, you just have to add this function in the functions.php file of your child theme:

function body_class_language( $classes ) {
    if ( ICL_LANGUAGE_CODE == 'en' ) {
        $classes[] = 'en';
    } elseif ( ICL_LANGUAGE_CODE == 'fr' ) {
        $classes[] = 'fr';
    }
    return $classes;
}
add_filter('body_class','body_class_language');

Step 2 : Add a function, also in the functions.php child-theme, that first identifies the currently displayed language, then saves it in the database, when the user clicks on the "publish" button.

function save_activity_extra_fields( $activity ) {
    global $wpdb;
    $activity_id = $activity -> id;
    $save = $wpdb->insert(
        'wp_bp_activity_meta',
        array( 'activity_id' => $activity_id,  'meta_key' => 'lang','meta_value' => get_bloginfo( 'language' ) ),
        array( '%d', '%s', '%s' ),
    );
} 
add_action('bp_activity_after_save', 'save_activity_extra_fields');

/!\ Just be sure to change the table prefix wp_ according to your DB (wp_bp_activity_meta) !

Step 3 : Now you have to add a language attribute on each post (li). Edit this file "plugins/buddypress/bp-templates/bp-nouveau/buddypress/activity/entry.php" and change line 14 :

<li class="<?php bp_activity_css_class(); ?>" id="activity-<?php bp_activity_id(); ?>" <?php bp_nouveau_activity_data_attribute_id(); ?> data-bp-timestamp="<?php bp_nouveau_activity_timestamp(); ?>">

Into this

global $wpdb;
$lang = $wpdb->get_results( "SELECT `meta_value` FROM `wp_bp_activity_meta` WHERE `meta_key` = 'lang' AND `activity_id` = ". bp_get_activity_id() );
$lang = $lang[0]->meta_value;
?>

<li class="<?php bp_activity_css_class(); ?>" id="activity-<?php bp_activity_id(); ?>" <?php bp_nouveau_activity_data_attribute_id(); ?> data-bp-timestamp="<?php bp_nouveau_activity_timestamp(); ?>" data-bp-lang="<?php echo $lang; ?>">

/!\ Again, don't forget to change the table prefix wp_ according to your DB (wp_bp_activity_meta) !

Then save the file to "themes/YOUR-CHILD-THEME/buddypress/activity/" so that your changes are not deleted when Buddypress is updated

Step 4 : Now you just have to filter each post according to the language currently displayed by the site. And for that, the easiest and fastest way is to add these lines in the CSS of your child theme.

body.fr li[data-bp-lang="en-US"],
body.en li[data-bp-lang="fr-FR"] {
    display: none;
}

You can also create a dropdown list allowing each user to display the posts in the language he wants, but this is another story.

VoilĂ , in my example, on a site in two languages (English and French), the posts of the activity in French will be displayed only on the French site, and the posts in English will be displayed only on the English site.

Hope this help.

dragoweb
  • 699
  • 1
  • 8
  • 17

0 Answers0