Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Programmatically changing the buddypress language

  • @jdurrant

    Participant

    I’ve created a plugin to detect the user’s language and modify my site’s interface accordingly. The code uses several methods to determine language. The main method is to look at meta data associated with each post. I’d like to expand this functionality to the buddypress interface as well, but I’m having a very hard time of it. Here’s some code to illustrate my problem:

    `
    add_action( ‘bp_include’, ‘language_plugin_init’ ); # only load the plugin once buddypress is online because it uses a buddypress-specific function

    function language_plugin_init() {add_action(‘wp’, ‘id_language’);} # if I hook earlier, I have trouble accessing the $post variable

    function id_language() {

    # get the page language from the post metadata. preferred method.
    $GLOBALS[‘CustomInfo’] = get_post_custom();
    if ($GLOBALS[‘Lang’] == ”) {$GLOBALS[‘Lang’] = $GLOBALS[‘CustomInfo’][‘Lang’][0];}

    # if that doesn’t work, try to get language information from buddypress userdata instead
    if ($GLOBALS[‘Lang’] == ”) {
    global $current_user;
    get_currentuserinfo();
    $user_id = $current_user->ID;
    $alang = xprofile_get_field_data(“Language”, $user_id); # “Language” is a custom buddypress field I created

    if ($alang == “English”) {$GLOBALS[‘Lang’] = ‘en-US’;}
    else if ($alang == “Português”) {$GLOBALS[‘Lang’] = ‘pt-BR’;}
    else if ($alang == “Español”) {$GLOBALS[‘Lang’] = ‘es-ES’;}
    }

    # now look to a cookie if the above failed
    if ($GLOBALS[‘Lang’] == ”) {if ($_COOKIE[‘Lang’] != ”) {$GLOBALS[‘Lang’] = $_COOKIE[‘Lang’];}}

    # now look at browser settings to try to discover the language
    if ($GLOBALS[‘Lang’] == ”) {
    $langid = substr($_SERVER[“HTTP_ACCEPT_LANGUAGE”],0,2);

    if ($langid == ‘en’) {$GLOBALS[‘Lang’] = ‘en-US’;}
    if ($langid == ‘es’) {$GLOBALS[‘Lang’] = ‘es-ES’;}
    if ($langid == ‘pt’) {$GLOBALS[‘Lang’] = ‘pt-BR’;}
    }

    # now update the locale according to the identified language
    function wpsx_redefine_locale($locale) {return str_replace(‘-‘,’_’,$GLOBALS[‘Lang’]);}
    add_filter(‘locale’,’wpsx_redefine_locale’,10);

    load_default_textdomain(); # now that the locale has been set, this correctly changes the language for all of wordpress (admin bar)

    #### HERE I WANT TO PUT CODE TO CHANGE THE BUDDYPRESS LANGUAGE ACCORDING TO THE VALUE OF $GLOBALS[‘Lang’] AS WELL!!!####

    }
    `

    Any help you could offer would be most appreciated. I’ve really bruised my brain trying to figure this out. Thanks.

Viewing 1 replies (of 1 total)
  • @jdurrant

    Participant

    Of course as soon as I post I find a solution to my problem. Here’s what worked for me:

    `
    $mofile = ABSPATH . ‘wp-content/languages/buddypress-‘.str_replace(‘-‘,’_’,$GLOBALS[‘Lang’]).’.mo’;
    load_textdomain(‘buddypress’, $mofile );
    `

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Programmatically changing the buddypress language’ is closed to new replies.
Skip to toolbar