There are probably endless approaches to building the frontend logic regarding multi-language with Lime framework. But does anyone have a simple, proven method that works?
My site has English as default and German as secondary language. So the URL structure I’d like for the homepage is:
Default homepage: https://domain.tld
German homepage: https://domain.tld/de
For this I used the $params variable, which works well:
$app->bind("/:lang", function($params) use($app) {
$posts = cockpit(‘collections’)->find(‘news’, [“lang” => $params[‘lang’]]);
return $this->render(“views/frontpage.php with views/layout.php”, array(‘posts’ => $posts));
});
But the $params does not work for subpages, for example:
Default subpage: https://domain.tld/subpage_slug
German subpage: https://domain.tld/de/subpage_slug
For the subpage /article/ I tried the following without success:
$app->bind("/:lang/article/:id", function($params) use($app) {
$post = cockpit(‘collections’)->findOne(‘news’, ["_id" => $params[‘id’], “lang” => $params[‘lang’]]);
return $this->render(“views/article.php with views/layout.php”, array(‘post’ => $post));
});
How would you build the logic?
Also, do you have any tips on creating a language switcher?
This code seems a bit hacky:
$config = cockpit()[‘app.config’];
$db = cockpit()->db;
$locales = array_merge([$config[‘i18n’]], $db->getKey(“cockpit/settings”, “cockpit.locales”, []));
Thanks!