伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: textdomain

来源:网络转载 浏览:31290次 时间:2024-02-23
iconv » « ngettext
  • PHP 手册
  • 函数参考
  • 国际化与字符编码支持
  • Gettext
  • Gettext 函数

textdomain

(PHP 4, PHP 5, PHP 7, PHP 8)

textdomain — Sets the default domain

说明

textdomain(?string $domain): string

This function sets the domain to search within when calls are made to gettext(), usually the named after an application.

参数

domain

The new message domain, or null to get the current setting without changing it

返回值

If successful, this function returns the current message domain, after possibly changing it.

注释

注意:

The textdomain() information is maintained per process, not per thread.

add a note

User Contributed Notes 2 notes

up down 0 ninja (without a) at informance dot dot dot info11 years ago When you set a text domain, it (obviously) erases the previous one.

This can be a problem when you use nested includes in which there can be textdomain() calls with different text domains. After returning from the include, your textdomain is changed, and the rest of the parent script fails in translating the remaining sentences.

I wrote these two small functions to avoid that. You should put the whole code into a file which you require_once in every script that needs gettext functionalities. Then, use only set_textdomain() and restore_textdomain() at the beginning and the end of every script/function that sets a text domain.

A call to set_textdomain() MUST correspond to a call to restore_textdomain(), and there is no limit in how many nested calls are done.

One optimization is done here: when the new text domain is the same as the current one, no change is made, and restore_textdomain() will be aware of this so you can still call it safely.

<?php
$_td_stack = array(); // text domains stack

/**
* Sets a new text domain after recording the current one
* so it can be restored later with restore_textdomain().
*
* It's possible to nest calls to these two functions.
* @param string the new text domain to set
*/
function set_textdomain($td)
{
    global $_td_stack;
   
    $old_td = textdomain(NULL);
   
    if ($old_td)
    {
        if (!strcmp($old_td, $td))
        {
            array_push($_td_stack, false);
        }
        else
        {
            array_push($_td_stack, $old_td);
        }
    }
   
    textdomain($td);
}

/**
* Restore the text domain active before the last call to
* set_textdomain().
*/
function restore_textdomain()
{
    global $_td_stack;
   
    $old_td = array_pop($_td_stack);
   
    if ($old_td)
    {
        textdomain($old_td);
    }
}
?>
up down 0 ipso at snappymail dot ca15 years ago I was having very strange issues with gettext only setting the locale once per Apache process (non-threaded), until I found this bug report:

http://bugs.php.net/bug.php?id=21965

It almost seemed like gettext was caching the data once per process, but in fact calling textdomain() fixed the issue for me.
add a note

官方地址:https://www.php.net/manual/en/function.textdomain.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net