Skip to content

How to load multiple *.mo files (general and for specific textdomain) #272

@jasomdotnet

Description

@jasomdotnet

First of all, thank you for a library. Second, I don't know how to load multiple *.mo translation files and use them with textdomain context.

This is my code so far:

use Gettext\Translator;
use Gettext\Loader\MoLoader;

$lang = 'sk' // set by a side function
define('ROOT', '/path/to/root');

function __( string $string, string $textdomain = null ) {

    static $t = null;

    global $basic_lang;

    $translation = ROOT . '/languages/' . $lang . '.mo';

    if (!file_exists( $translation )) {
        return $string;
    }

    if ($t === null) {

        $loader = new MoLoader();
        $t = Translator::createFromTranslations( $loader->loadFile( $translation ) );
    }

    //return $textdomain ? $t->dgettext( $textdomain, $string ) : $t->gettext( $string );
    return $t->gettext( $string );

}

//echo __( 'Buy a ticket', 'reservation' ); // does not work right now, how to make it work?
echo __( 'Buy a ticket' ); // works

I guess that mode code samples would be helpful for others too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions