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

Translation based on hostname

Hi,

I'm looking for the best way to translate my project based on hostname.

I already use routing with hostname. It works well.

For example:

  • www.mywebsite.com => redirect to the best hostname (based on locale code)
  • mywebsite.fr => french
  • mywebsite.es => spanish
  • fr.mywebsite.com => french
  • en.mywebsite.com => english ...

I don't want to use a "huge" ControllerBase. Does it exist a way with routing or with dispatcher?

Cheers!

Emilien

edited Jun '16

Do you have a list of all domains and subdomains your app will be using? If answer is positive, then sure, there are ways to handle it. Otherwise using REGEXP is the only way.

First, which adapter do you use to read translation messages? Phalcon\Translate\Adapter\NativeArray ?

Right now, I have this code. And I use NativeArray. But later, i will can use another adapter (Gettext).

$di->setShared('router', function () use ($di, $config) {
    $router = new \Phalcon\Mvc\Router(false);
    $router->setDI($di);
    $router->setDefaultNamespace('controllers');
    $router->removeExtraSlashes(true);

    foreach ($config->sites as $hostname => $route) {
        $hostname_pattern =  '^(([a-z]{2}|www)\.)?' . str_replace('.', '\.', $hostname);
        $router->addGet('/', $route)->setHostname($hostname_pattern);
    }
    $router->notFound(['controller' => 'error', 'action' => 'route']);
    $router->handle();
    return $router;
});


85.5k

hmm i would do this the other way araound i tihink. I will use apache/nging to take care of this for me.

And base on this https://stackoverflow.com/questions/146354/setting-php-variables-in-httpd-conf

vhost 1:

root : /var/www/site/
SetEnv _lang  english

vhost 2: 

root: /var/www/site
SetEnv _lang french

so after that i can create my php function


function getLang(){
    // if isset($_SERVER["_lang"]; ...
    else 
    check the subdomain etc...
}
edited Jul '16

Good idea @Izopi4a

P.S. I wanted to upvote your reply, but accidentally I hit the downvote button instead, and now You have already voted this reply prevents me to change it. :/



85.5k

haha np :D