Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

What does the double underscore mean in Wordpress?

         

Planet13

10:24 pm on Nov 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey all:

What does the double underscore ( __ )mean in the following wordpress / php array statement:

genesis_register_sidebar( array(
'id' => 'after-post',
'name' => __( 'After Post', 'themename' ),
'description' => __( 'This is a widget area that can be placed after the post', 'themename' ),
) );


Note: This is from a genesis site so sorry if this is non-standard php / wordpress and is only specific to Genesis.

lorax

3:06 am on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The double underscore is the localization function (also referred to as the translate function because it makes translations easier):

[codex.wordpress.org...]

Make a string inside your plugin or theme translatable:

$translated = __( 'Hello World!', 'mytextdomain' );

Planet13

9:09 pm on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the response, Lorax:

You know, I read through the codex link you provided, and the other pages linked from the codex, and I have to admit that after reading them, I am probably as confused as ever.

I guess what it means is that if you are developing a plugin (or a function to post on github), and you want a specific string of text to appear in the native language of the user (such as a Widget Description, or a metabox label), then you would use that localization / translation function to allow someone to translate it into their native language, right?

Or am I totally wrong?

ergophobe

10:44 pm on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Essentially. It flags strings for parsing and then also passes them through for translation.

The first part of that sentence is important. This can't be done on the fly, so you can NOT use variables in the call to __(). If you do, there's no way for the localization parser to find it and make the string available to translators.

See this really important note among the examples

This works
$translated = __( 'Hello World!', 'mytextdomain' );


This does NOT work
$text_domain = 'mytextdomain';
$string = 'Hello World!';
$translated = __( $string, $text_domain );


src: [codex.wordpress.org...]

lorax

1:43 pm on Nov 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the clarification @ergophobe. I've never worked with this function so I'm on the edge of my knowledge about it.

Planet13

4:51 pm on Nov 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks from me as well, ergophobe.

It looks like this is useful for plugin developers only, because one needs to declare a text domain at the top of their plugin header:

/*
* Plugin Name: My Plugin
* Author: Otto
* Text Domain: my-plugin
*/


From the wordpress codex here:

[codex.wordpress.org...]

But I have come across it used in the functions.php file, as well as being used in little snippets that people give out.

In those instances (where it is in snippets), then it is pretty much useless, right? I mean, if it is put into a template, then it would already be too late for the localization parser, wouldn't it be?

(not to mention that there wouldn't be a proper text domain declaration.)

ergophobe

4:49 am on Nov 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think that's right. That provides the mechanism for gettext to distinguish among translations for the same term, which might vary depending on context.

TechBuddy

12:56 pm on Nov 24, 2014 (gmt 0)



<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) );
here "_" is another way to define : user define print function or translated to other languages.

Planet13

3:40 pm on Nov 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for pointing this out, TechBuddy.

TechBuddy

9:30 am on Nov 27, 2014 (gmt 0)



You Most welcome @Planet13..