phpday 2027 - Call for Papers

bindtextdomain

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

bindtextdomain — ドメインのパスを取得/設定する

説明

function bindtextdomain(string $domain, ?string $directory = null): string|false

bindtextdomain() 関数は、ドメインへのパスを取得/設定します。

パラメータ

domain

ドメイン。

directory

ディレクトリのパス。 空文字列または "0" を指定すると、カレントディレクトリを指定したことになります。 null を指定すると、現在設定されているディレクトリが返されます。

戻り値

現在設定されているドメインへのフルパスを返します。 失敗した場合に false を返します。 directory が存在しない場合にも false を返します。

エラー / 例外

domain が空の string であるか、1024 バイトより長い場合、 ValueError がスローされます。

domain または directory にヌルバイトが含まれている場合、 ValueError がスローされます。

変更履歴

バージョン 説明
8.4.4 domain または directory にヌルバイトが含まれている場合、 ValueError をスローするようになりました。
8.4.0 directory パラメータが任意になりました。 これより前のバージョンでは、常に指定しなければなりませんでした。
8.0.3 directory は、nullable になりました。 これより前のバージョンでは、現在設定されているディレクトリを取得できませんでした。
8.0.0 domain が空の string であるか、長すぎる場合、 ValueError をスローするようになりました。 これより前のバージョンでは、E_WARNING が発生し、false を返していました。

例

例1 bindtextdomain() の例

<?php

$domain = 'myapp';
echo bindtextdomain($domain, '/usr/share/myapp/locale');

?>

上の例の出力は以下となります。

/usr/share/myapp/locale

注意

注意:

bindtextdomain() 関数の情報はプロセス単位で管理されます。 スレッドではありません。

+add a note

User Contributed Notes 2 notes

up
6
n8klatt
12 years ago
The name of your .mo file must match the $domain, e.g. name your files messages.mo and call bindtextdomain("messages", $directory).
up
4
roel dot vermeulen at gmail dot com
10 years ago
I recommend using absolute paths in the $directory parameter. This caused me several hours to debug as Ajax calls to my localization functions messed up the path. And since no error if thrown if the path in $directory cannot be found, one should check the result always:

<?php
    // Imagine the path for this file is "/localization" and your locales are in the "/locale" directory.
    $pathToDomain = __DIR__ . "/../locale";
    if ($pathToDomain != bindtextdomain($domain, $pathToDomain)) {
        // Error handling.
    }
?>
To Top