What is bindtextdomain, textdomain in gettext?
Asked Answered
S

1

7

I've been learning a bit of gettext but I can't grasp those two functions. I've been wondering if I could use multiple translations in a APP written in PHP. For an instance, I've 1) the system translation 2) extensions translations 3) theme translations to divide those in different files. My question is, if I load the system translation, then load the theme translation will the first one be "unset"?

I'd appreciate any links related to gettext and php.

Thanks

Swafford answered 1/1, 2010 at 17:19 Comment(0)
H
27

You can readily swap between textdomains whenever you like. e.g:

Given

./locale/en/LC_MESSAGES/template.po 

with the contents

msgid "foo"
msgstr "foobar"

and

./locale/en/LC_MESSAGES/messages.po

with the contents

msgid "Basic test"
msgstr "A basic test"

You could use something like the following PHP code to switch from one textdomain to the other, and then back:

<?php
setlocale(LC_ALL, 'en_US.UTF-8');
bindtextdomain ("messages", "./locale");
bindtextdomain ("template", "./locale");

textdomain ("messages");
echo gettext("Basic test"), "\n";

textdomain ("template");
echo _("foo"), "\n";

textdomain ("messages");
echo gettext("Basic test"), "\n";
Hershey answered 1/1, 2010 at 18:44 Comment(1)
Thanks I think I got a very nice view of gettextSwafford

© 2022 - 2024 — McMap. All rights reserved.