PHP:__('Some text') 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2427204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: What does __('Some text') do?
提问by Svish
Reading about Kohana templatesand saw something I've never seen before:
阅读Kohana 模板并看到一些我以前从未见过的东西:
$this->template->title = __('Welcome To Acme Widgets');
What does __('Text')mean? What is it? What does it do?
什么__('Text')意思?它是什么?它有什么作用?
采纳答案by salathe
In Kohana (version 3) the function is defined in system/base.phpand 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.
在 Kohana(第 3 版)中,该函数在system/base.php 中定义,是一个方便的函数,可以帮助(如其他答案所述)国际化。您提供一个字符串(可以选择使用一些占位符将值替换为完成的文本),然后对其进行解释,并在需要时返回翻译。
Contrary to assumptions in other answers, this does not use gettext.
与其他答案中的假设相反,这不使用gettext。
A very basic example would be (this particular string is already translated into English, Spanish and French in Kohana):
一个非常基本的例子是(这个特定的字符串已经在 Kohana 中被翻译成英语、西班牙语和法语):
// 1. In your bootstrap.php somewhere below the Kohana::init line
I18n::lang('fr');
// 2. In a view
echo __("Hello, world!"); // Bonjour, monde!
回答by Vals
The double '__' is used for Localization in CakePHP (and possible other frameworks)
双 '__' 用于 CakePHP(和可能的其他框架)中的本地化
回答by Volshebnik
// Display a translated message
echo __('Hello, world');
// With parameter replacement
echo __('Hello, :user', array(':user' => $username));
See http://kohanaframework.org/3.2/guide/api/I18nfor details.
回答by jmucchiello
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.
我的猜测是它在 Kohana 文档中的某处定义。
回答by Daniel Bingham
It's string gettext ( string $message ): http://php.net/manual/en/function.gettext.php
它是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")
__() 只是它的别名。所以__("some text")相当于gettext("some text")
edit:Actually if it's two underscores than it isn't gettext(). The alias for gettext() is one underscore.
编辑:实际上,如果它是两个下划线而不是 gettext()。gettext() 的别名是一个下划线。
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
第二次编辑:看起来 __() 可能是 gettext() 的另一个别名。与 _() 的含义略有不同。请参阅此处: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/
第三次也是最后一次编辑:这是一篇更详细解释它的文章。看起来它不是内置功能,而是许多框架中通常添加的功能。它本质上是 gettext 的别名 - 它执行相同的功能。但是,它不是直接别名(我不认为)。它在框架中实现并且特定于框架。它搜索并返回给定字符串的本地化或翻译。有关更多信息,请参阅此博客文章:http: //www.eatmybusiness.com/food/2007/04/13/what-on-earth-does-a-double-underscore-then-parenthesis-mean-in-php- __/7/

