Using the LocalMenu
template from fluent to get a language switcher gives me "wrong" links for my custom controllers and dataobjects which I display as page.
So I have both. Controllers as pages and dataobjects on other custom controllers which are displayed as pages.
Instead of en/sompage/foobar
I receive somepage/foobar?l=en_US
I think that's caused by my wrong implementation of the Link
function on those dataobjects and controllers.
How can I do it correct? I can't find anything about that. That's my current code:
public function Link($action = null) {
$parent = MyParentPage::get()->first()->URLSegment;
$home = RootURLController::get_default_homepage_link();
if($parent != $home) {
$url = $parent . '/';
} else {
$url = null;
}
return Director::baseURL() . $this->fluentUrlSegment() . $url . 'controller-segment';
}
and
public function fluentUrlSegment() {
if(class_exists('Fluent')) {
$locale = Fluent::current_locale();
$fluentConfig = Fluent::create()->config();
if($fluentConfig->default_locale != $locale) {
if($fluentConfig->aliases && isset($fluentConfig->aliases[$locale])) {
$locale = $fluentConfig->aliases[$locale];
}
$locale = $locale . '/';
} else {
$locale = '';
}
} else {
$locale = '';
}
return $locale;
}
fluent config
---
Name: projectfluentconfig
After: '#fluentconfig'
---
Fluent:
disable_default_prefix: true
detect_locale: true
remember_locale: true
default_locale: de_DE
locales:
- de_DE
- en_US
aliases:
de_DE: de
en_US: en
field_exclude:
---
Name: projectfluentconfig
After: '#fluenti18nconfig'
---
i18n:
default_locale: de_DE
Thanks to wmk's answer I could improve my link function. Unfortunately it still didn't change anything on the links in fluents locale menu.
Instead of geschichte/..
and en/geschichte/..
I receive geschichte/..?l=de_DE
and geschichte/..?l=en_US
on the german site and en/geschichte/..?l=de_DE
and en/geschichte/..?l=en_US
on the english one.
Could it be that fluent is ignoring those link functions completely? Did I still made a mistake or missed something? Here's my shortend code Controller, DataObject, Routes, Extensions & Fluent Config
Any suggestions?
The LocaleLink
function in the FluentSiteTree
class seems to be the "problem"
You already grab a Page from the DB and take the URLSegment. Why not utilize that page's Link()
method? This should already have the fluent stuff set.
Then concat the link parts with Controller::join_links()
public function Link($action = null) {
//maybe parent::Link() also works, depending on your code structure
$parent = MyParentPage::get()->first()->Link();
return Controller::join_links($parent, 'controller-segment', $action);
}
That's not really an answer but a workaround.
I managed it to get it working by adding the FluentExtension
also to the Controller and on that Controller I've implemented my own LocaleLink
function.
For any one else stuck here and until there's a real solution, here's my working code Controller, DataObject, Routes, Extensions & Fluent Config
And if you wan't to use a translated urlsegment for the controller you could use this link function
public function Link($action = null) {
$homepage = Page::get()->find('URLSegment', RootURLController::get_homepage_link());
$parent = $homepage->Link();
$urlByLocale = [
'de_DE' => 'geschichte',
'en_US' => 'story',
];
return Controller::join_links($parent, $urlByLocale[Fluent::current_locale()], $action);
}
and change the routes like that
'de/geschichte//$URLSegment!' => [
'Controller' => 'Story_Controller',
'l' => 'de_DE'
],
'en/story//$URLSegment!' => [
'Controller' => 'Story_Controller',
'l' => 'en_US'
],
Would be great if someone will find a "nicer" way to achieve that.