Search code examples
phpgettext

How i can use multiple textdomain in php gettext?


I want to load multiple mo files in gettext at the same time

An example:

$lang = 'en_US';

putenv('LC_ALL=' . $lang);
setlocale(LC_ALL, $lang);

bindtextdomain('module', ROOT . 'Language');
bindtextdomain('default', ROOT . 'Language');

textdomain('module');
textdomain('default');

But I can not use two "textdomains" at the same time. This code gives only one language. I want to use multiple "bindtextdomain" and .mo files at the same time.

I have many modules. Each module must be have separate "mo" files. And each module loads at the same files.

How i can use multiple textdomain ?


Solution

  • You have need two function. First function is dynamically change gettext domain. Second function is working like default gettext method. Briefly; You must change gettext domain and select domain in a function.

    function selectDomain($trFile)
    {
        if ($trFile == 'default') {
            bindtextdomain($trFile, ROOT . 'Language');
        } else {
            bindtextdomain($trFile, MROOT . $trFile . '/Language');
        }
    
        textdomain($trFile);
    }
    
    
    function _e($msgid, $trFile = 'default')
    {
        selectDomain($trFile);
        return _($msgid);
    }
    
    echo _e('Test');
    echo _e('Test','another_text_domain');