84

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?

2
  • 1
    Weird... I downloaded Kohana and I can't find any reference to such function in the source code. Mar 11, 2010 at 17:44
  • @Alvaro Maybe you downloaded version 2.x. Try and find version 3.
    – alex
    Mar 12, 2010 at 5:50

5 Answers 5

54

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!
1
14

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

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

2
  • 1
    The question of your comment is, What do you mean by "Localization"? :D Oct 10, 2011 at 9:24
  • 1
    @ElsonSolano, No that should be pretty well known and if it's not, there are other questions here answering what it is :)
    – Svish
    Feb 17, 2012 at 9:54
7

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.

3
  • 6
    He asked what does it mean. That implies to me he doesn't know it is function call. YMMV. Mar 11, 2010 at 17:34
  • 1
    @Adriano I disagree, the question at least seems to suppose this is something other than a function. Just like $ in jQuery, this is a weird function name that may be confused as something else.
    – Tim Lytle
    Mar 11, 2010 at 17:36
  • 2
    +1, not entirely useless and I agree with Tim Lytle. I'd only be upset if this was accepted over the other finer answers.
    – alex
    Jul 22, 2010 at 0:37
6

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/

4
  • 1
    Isn't that just one _, not two? Mar 11, 2010 at 17:29
  • I didn't know that. But the question asks about the function "two underscores". Mar 11, 2010 at 17:29
  • @T.J. Crowder Yeah, my first search turned up that article and I didn't notice that it was only one and not two. However, on a second search it looks like it does basically the same thing. Just implemented specific to the framework. Mar 11, 2010 at 17:37
  • 1
    I suspect the framework reimplement it so you don't need to mess around with configuring gettext on top of configuring the framework. Mar 11, 2010 at 18:02
6
// 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.

1
  • You can use this if the above not working echo __('Hello, %1', $username); Aug 18, 2021 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.