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

Reading about Kohana templates and saw something that I had never seen before:

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

What does __('Text') mean? What is it? What is he doing?

+60
php kohana-3
Mar 11 '10 at 17:26
source share
5 answers

In Kohana (version 3), the function is defined in system / base.php and is a convenient function to help (as other answers mentioned) internationalization. You provide a string (optionally, some placeholders to replace the values ​​in the finished text), which are then interpreted and, if necessary, the translation is returned.

Unlike the assumptions in other answers, this does not use gettext .

The simplest example: this line has already been translated into English, Spanish and French in Cohan:

 // 1. In your bootstrap.php somewhere below the Kohana::init line I18n::lang('fr'); // 2. In a view echo __("Hello, world!"); // Bonjour, monde! 
+38
Mar 11 '10 at 19:12
source share

The double "__" is used for localization in CakePHP (and other possible frameworks)

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

+13
Mar 11 '10 at 17:31
source share

This means that someone created a function called __ (These are two underscores next to each other.)

I assume it is defined somewhere in the Kohana documentation.

+6
Mar 11 '10 at 17:27
source share

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

Returns the translated string if it is found in the translation table, or the sent message if not found.

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

edit: Actually, if these are two underscores, this is not gettext (). The alias for gettext () is one underscore.

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

Third and final editing: Here is an article explaining this in more detail. This doesn't seem to be a built-in function, but rather something that is usually added in a variety of frameworks. This is essentially an alias of gettext - it performs the same function. However, this is not a direct alias (I do not think so). It is implemented and specific to the structure. It searches and returns the localization or translation of the string that it specifies. For more details, 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 /

+6
Mar 11 '10 at 17:28
source share
 // 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 more details.

+4
Oct 25 '11 at 1:56 a.m.
source share



All Articles