textdomain

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

textdomain β€” УстанавливаСт Π΄ΠΎΠΌΠ΅Π½ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ

ОписаниС

function textdomain(?string $domain = null): string

Ѐункция устанавливаСт Π΄ΠΎΠΌΠ΅Π½, Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ часто совпадаСт с Π½Π°Π·Π²Π°Π½ΠΈΠ΅ΠΌ прилоТСния, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π±ΡƒΠ΄Π΅Ρ‚ ΠΈΡΠΊΠ°Ρ‚ΡŒ сообщСниС функция gettext().

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

domain

Новый Π΄ΠΎΠΌΠ΅Π½ сообщСний ΠΈΠ»ΠΈ null, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π±Π΅Π· измСнСния.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ Π΄ΠΎΠΌΠ΅Π½ сообщСний послС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ³ΠΎ измСнСния, Ссли Π½Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Ошибки

Ѐункция выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ domain ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ ΠΏΡƒΡΡ‚ΡƒΡŽ строку (string).

Бписок измСнСний

ВСрсия ОписаниС
8.4.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ domain стал Π½Π΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΌ. РаньшС ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ Ρ‚Ρ€Π΅Π±ΠΎΠ²Π°Π»ΠΎΡΡŒ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΌ Π²Ρ‹Π·ΠΎΠ²Π΅.
8.4.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ domain ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ ΠΏΡƒΡΡ‚ΡƒΡŽ строку (string).

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ Π΄ΠΎΠΌΠ΅Π½Π΅, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ устанавливаСт ΠΈΠ»ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ функция textdomain(), сохраняСтся для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ процСсса, Π° Π½Π΅ для ΠΏΠΎΡ‚ΠΎΠΊΠ°.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 2 notes

up
2
ninja (without a) at informance dot dot dot info ΒΆ
15 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
0
ipso at snappymail dot ca ΒΆ
19 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.