There are “printf” statements all over the BuddyPress default theme. What do they mean?
Today, I decided to invest an hour into finding the answer to that question. Here is my answer.
In short, “printf” is php code used to help make themes display words and phrases from other languages. For a long answer and how-to, read this.
Here’s an example, in the BuddyPress default theme’s footer.php file, you will find the following line of code:
printf( __( '%s is proudly powered by WordPress and BuddyPress', 'buddypress' ), bloginfo('name') );
If you look at line #2963 in “buddypress –> bp-languages –> buddypress.pot”, you will see the same code as above.
Basically, these printf statements give us a central location to change certain “special” words and phrases across a BuddyPress website. For example, you might want to change all instances of “Login” to a different word (or language). You might want to change “Groups” to “Communities”, as the Custom Groups Name plugin does.
I am a beginner at this printf stuff, and that is as simple an answer as I can give right now. All you .pot and .mo gurus out there, please let me know if I misspoke at all. 🙂
Thanks!
4 Comments
More posts from themightymo.com
How to Convert .aiff to .mp3 files via Command Line
Yesterday I ran into an issue where I needed to convert .aiff files to .mp3. DBPowerAmp was recommended to me as a paid GUI tool. But I wanted something more lightweight, since this is a task I rarely need to do (most of the time, I’m already in Logic Pro, and exporting is simple there).…
Google Removed Our Business Listing – How we restored our biz to the Map and got our reviews back.
I was about to send an email to a potential customer pointing them to our 5-star Google Reviews via our Google Business profile (e.g. the Google Map), hoping this added information about our customers’ past experiences might help me close a deal. But when I checked the Google Reviews link, it was down. And after…
The Price We Pay: Cloudways Off-Site Backup vs. Budget WordPress Hosts
Cloudways Off-Site Backup Pricing Calculator: The Ultimate Cost Guide
Actually, “printf” itself doesn’t do any language stuff. It’s just a PHP function that prints things on the screen, just like the “echo” function. Many programming languages have a “printf” function to output text.
The function that allows for language translation is the one that goes inside the printf. Yes, that’s a function, the __() function. What it does is translate whatever text you put inside and feed it to the printf.
The code you posted could also be written as:
_e(‘my text’, ‘file);
which translates and then prints ‘my text’.
Here you can learn more about how internationalization works in WordPress/BuddyPress: http://codex.wordpress.org/I18n_for_WordPress_Developers
About the .pot and .mo stuff, it’s not that hard; using a program like PoEdit to makes it really easy to work with language files.
Thanks for the clarification, Julio!
We primarily use the printf and sprintf functions to provide variables within the core BuddyPress phrases that we can later fill with dynamic data.
Stuff like user names and page numbers are great examples. The string may be something like:
%s and %s are now friends
…and what we’ll do is figure out which two user names just became friends, and put a user name where each %s is.
%s means string
%d means integer
Then, when a pomo needs to localize that string, it can turn it into:
%s y %s son amigos
…and not need to all sorts of text replacement surgery to do it.
Hope that helps! 😀
Jes, but it faled to translate “Name” on my site…everything else is translated but when looking at his profile my user see plain old english “Name” in stead of correctly compiled translation from mo file….how to fix that?