Search code examples
cakephptranslationgettext

Define the language when using gettext __d() on CakePHP


I want to define with language I want to use on a single call to __d() (the gettext() CakePHP translation function).

I want something like:

__d('domain', 'Hello World!', 'pt_BR');

Even if my site is shown on en_US, I want that very message to be shown on a specific language.

Thanks!


Solution

  • Something like this could work (untested though):

    function __dl($domain, $str, $lang, $return = false) {
        $original = Configure::read('Config.language');
        Configure::write('Config.language', $lang);
        $str = __d($domain, $str, true);
        Configure::write('Config.language', $original);
    
        if ($return) {
            return $str;
        }
        echo $str;
    }
    
    __dl('domain', 'Foo bar', 'pt_BR');