CakeFest 2024: The Official CakePHP Conference

bindtextdomain

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

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

説明

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

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

パラメータ

domain

ドメイン。

directory

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

戻り値

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

変更履歴

バージョン 説明
8.0.3 directory は、nullable になりました。 これより前のバージョンでは、現在設定されているディレクトリを取得できませんでした。

例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
9 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
7 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