Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple domains in gettext in a PHP application

Tags:

php

gettext

By domain I mean the gettext domain. I have this code

$domain = "default";
$locale = 'en_US';
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain($domain, 'my_path'); 
bind_textdomain_codeset($domain, 'UTF-8');    
textdomain($domain);

So It will use the my_path/en_US/LC_MESSAGES/default.po

But I want to use more than 1 file so I can override overwrite the value in default.po with another po file like admin.po, blog.po, etc

like image 430
Julio Avatar asked Mar 12 '12 14:03

Julio


1 Answers

First define all your domains something like this:

bindtextdomain('domain1', DIR_LOCALE);
bindtextdomain('domain2', DIR_LOCALE);
bindtextdomain('domain3', DIR_LOCALE);
textdomain('domain1'); // set default domain for _() function

Now if you want to grab strings from a different domain, you'd use either dgettext() or dcgettext() (if not from LC_MESSAGES) to retrieve just a single string from a different, specified domain. Example:

echo dgettext('domain2', "msgid");
like image 139
DanMan Avatar answered Oct 01 '22 23:10

DanMan