PHP: What does __('Some text') do?

PhpKohana 3

Php Problem Overview


Reading about Kohana templates and saw something I've never seen before:

$this->template->title = __('Welcome To Acme Widgets');

What does __('Text') mean? What is it? What does it do?

Php Solutions


Solution 1 - Php

In Kohana (version 3) the function is defined in system/base.php and is a convenience function to aid (as the other answers have mentioned) internationalization. You provide a string (with, optionally, some placeholders to substitute values into the finished text) which is then interpreted and, if required, a translation is returned.

Contrary to assumptions in other answers, this does not use gettext.

A very basic example would be (this particular string is already translated into English, Spanish and French in Kohana):

// 1. In your bootstrap.php somewhere below the Kohana::init line
I18n::lang('fr');

// 2. In a view
echo __("Hello, world!"); // Bonjour, monde!

Solution 2 - Php

The double '__' is used for Localization in CakePHP (and possible other frameworks)

http://book.cakephp.org/view/163/Localization-in-CakePHP

Solution 3 - Php

It means someone created a function named __ (That's two underscores next to one another.)

My guess is it defined somewhere in the Kohana documentation.

Solution 4 - Php

It's string gettext ( string $message ): http://php.net/manual/en/function.gettext.php

> Returns a translated string if one is > found in the translation table, or the > submitted message if not found.

The __() is just an alias for it. So __("some text") is equivalent to gettext("some text")

edit: Actually if it's two underscores than it isn't gettext(). The alias for gettext() is one underscore.

Second edit: It looks like __() might be another alias for gettext(). With a slightly different meaning from _(). See here: http://groups.google.com/group/cake-php/browse_thread/thread/9f501e31a4d4130d?pli=1

Third and final edit: Here's an article explaining it in more detail. Looks like it isn't a built in function, but rather something that is commonly added in a lot of frameworks. It is essentially an alias of gettext - it performs the same function. However, it isn't a direct alias (I don't think). It is implemented in and is specific to the framework. It searches for and returns a localization or translation of the string it is given. For more, see this blog post: http://www.eatmybusiness.com/food/2007/04/13/what-on-earth-does-a-double-underscore-then-parenthesis-mean-in-php-__/7/

Solution 5 - Php

// Display a translated message
echo __('Hello, world');
 
// With parameter replacement
echo __('Hello, :user', array(':user' => $username));

See http://kohanaframework.org/3.2/guide/api/I18n for details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSvishView Question on Stackoverflow
Solution 1 - PhpsalatheView Answer on Stackoverflow
Solution 2 - PhpValsView Answer on Stackoverflow
Solution 3 - PhpjmucchielloView Answer on Stackoverflow
Solution 4 - PhpDaniel BinghamView Answer on Stackoverflow
Solution 5 - PhpVolshebnikView Answer on Stackoverflow