PHP Doku:: Setzt die Standarddomain - function.textdomain.html

Verlauf / Chronik / History: (10) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzUnterstützung menschlicher Sprache und ZeichenkodierungGettextGettext-Funktionentextdomain

Ein Service von Reinhard Neidl - Webprogrammierung.

Gettext-Funktionen

<<ngettext

iconv>>

textdomain

(PHP 4, PHP 5)

textdomainSetzt die Standarddomain

Beschreibung

string textdomain ( string $text_domain )

Diese Funktion setzt die Domain, in der bei einem gettext()-Aufruf gesucht werden soll; gewöhnlich wird die Domain nach einer Applikation benannt.

Parameter-Liste

text_domain

Die neue Text-Domain oder NULL, wenn die aktuellen Werte ohne Veränderung übernommen werden sollen.

Rückgabewerte

Im Erfolgsfall gibt die Funktion die aktuelle Text-Domain zurück, nachdem sie möglicherweise verändert wurde.


2 BenutzerBeiträge:
- Beiträge aktualisieren...
ninja (without a) at informance dot dot dot info
14.10.2010 18:00
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);
    }
}
?>
ipso at snappymail dot ca
27.04.2007 20:26
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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)