We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Best Approach in Multi Language Site

Hi,

I am looking to create a multi-language site using phalcon. I would like to seek some advices from experienced peeps who did it before on what is the best approach in doing so.

Cheer Derek

Its hard to say, can you be more specific, what exactly do you want achieve? Multilanguage models, how to handle translations, ...?



12.4k

I use "symfony/translation". The best extension with PoEdit.

$di->setShared('translator', function() {
    $lang = Lang::getCurrentLanguage();

    $translator = new \Symfony\Component\Translation\Translator($lang, new \Symfony\Component\Translation\MessageSelector());
    $translator->setFallbackLocales(array('en_US'));
    $translator->addLoader('mo', new \Symfony\Component\Translation\Loader\MoFileLoader());

    if(file_exists(sprintf(__DIR__ . '/locale/%s.mo', $lang))) {
        $translator->addResource('mo', sprintf(__DIR__ . '/Resource/locale/%s.mo', $lang), $lang);
    } else {
        $translator->addResource('mo', __DIR__ . '/Resource/locale/en_US.mo', 'en_US');
    }

    return $translator;
});