Create a BuddyPress Language File For Sitewide Label and Message Editing

Create a BuddyPress Language File For Sitewide Label and Message Editing

You’ve probably heard about the BuddyPress language file, but maybe you’ve never taken the time to find out how it works and how it can help you customize your community.

If you’re creating a highly customized niche network with BuddyPress, then the language file is going to be one of your most important and time-saving tools. It allows you to change text sitewide without digging into all of the code.

What is a language file?

A language translation file is not for translating international languages. It is used specifically for changing various labels and messages in BuddyPress in such a way that you don’t have to hack the core files. Otherwise, you’d have to hack quite a few core files to customize BuddyPress labels, messages, and URL slugs.  Not only is that time-consuming; it’s just not good practice.

How Can I Use the BuddyPress Language File?

You can find the BuddyPress language file in buddypress/bp-languages/buddypress.pot. Copy that file and rename it to a .po extension – ie. buddypress-mysite.po. Now you can start editing it in a text file with the help of the search and replace function. Make sure you remember this critical bit of information:

Do not edit the “msgid” entries.

Edit only the “msgstr” entries.

For example, let’s say you want to change the message your users receive when they create a password that doesn’t match:

#: bp-core/bp-core-settings.php:84
msgid "Your passwords did not match"
msgstr "Oh noes! Your passwords don't match. Try again!"

Simple, isn’t it? Let’s do one more example just for fun. This would be if you’re creating more of a business networking site and you’d like to change “Friend” to “Associate” Here’s how you would edit one of the entries:

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

#: bp-friends/bp-friends-templatetags.php:143
#: bp-themes/bp-default/_inc/ajax.php:343
msgid "Add Friend"
msgstr "Connect to Associate"

Of course, you’re going to have to change more “Friend” instances in that scenario, but it’s not difficult. When you’ve finally completed all the editing on your language file, there’s one more step to making it work.

How to Create the .MO File

Upload your .po language file to: wp-content/plugins/buddypress/bp-languages. Now you need to run a command from the command line inside this directory to tell your server to create the .mo file. Login as root using a program such as PuTTY for Windows or Terminal.app for Mac. Depending on your server’s file structure, you’ll want to CD to this directory:

cd /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/buddypress/bp-languages

Now run the following command: msgfmt -o buddypress-mysite.mo buddypress-mysite.po

This will save the newly created .mo file in the /bp-languages/ directory for you.

Next step is to call the buddypress-mysite.mo file from wp-content/plugins/bp-custom.php like so:

define( 'BPLANG', 'mysite' );
if ( file_exists( BP_PLUGIN_DIR . '/bp-languages/buddypress-' . BPLANG . '.mo' ) ) {
load_textdomain( 'buddypress', BP_PLUGIN_DIR . '/bp-languages/buddypress-' . BPLANG . '.mo' );
}

You’re done! Refresh your site and check out the changes.

When upgrading, if you use the automatic upgrade method to update BuddyPress, you should know that your custom language files will be removed. You’ll need to back those up before upgrading so that you can use them again.

If you have any more questions, please ask in the comments or check out the BuddyPress Codex for more information.

Tags: