multilang plugin and hyperlinks

60 views
Skip to first unread message

v.

unread,
Nov 7, 2019, 4:35:37 AM11/7/19
to f3-fra...@googlegroups.com
I am trying to use the multilang plugin but fail to understand it.
I have configured the languages in the config file.
When going to the root of the site I am automatically redirected to the browser language's page (so site.tld redirects to site.tld/en/) as expected.

How am I supposed to handle hyperlinks? My menu fetches links from the database with some hardcoded links.
eg.:
<a href="/about">about</a> <a href="/shop">shop</a> <a href="/contact">contact</a>

These links are not automatically being redirected to /en/... and generate a 404.
From what I undertand from the documentation the routes should be automatically changed with the prefix (as does the index page).Why is this not happening?

I can solve this by getting the languagae from the ml instance:
$this->f3->set('SESSION.lang', $this->ml->current);
and use that in the template file:
<a href="{{ @SESSION.lang }}/about">about</a>

But I don't think this is how it is supposed to work?

ved

unread,
Nov 7, 2019, 8:45:06 AM11/7/19
to Fat-Free Framework
You can either change the way you're creating the links to be something like:

{{ @ML->current }}{{ @your_url }}

AFAIK, there's no need to save your current language in the session.

Alternatively, enable the MULTILANG.migrate option.

ikkez

unread,
Nov 7, 2019, 9:08:23 AM11/7/19
to Fat-Free Framework
as far as I know, you should be using named routes and use the ml->alias function in your templates to generate the links.. preferably with a custom filter

xfra35

unread,
Nov 7, 2019, 2:55:12 PM11/7/19
to Fat-Free Framework
That's right:
  • either you prefix all your links with {{ @ml->current }}
  • or you define route aliases and make use of the $ml->alias() function
Either way requires to rewrite your links. Link generation can be made easier by using a custom filter. E.g:

<a href="{{ '/about' | lang }}">about</a>
(in this example, the "lang" template filter automatically prefixes the current language)

<a href="{{ 'about' | url }}">about</a>
(in this example, the "url" template filter automatically computes the given alias URL)

There's also a third way, which would be to set the base tag with the current language and use relative URLs inside links. But beware of side-effects..
E.g:
<base href="/{{ @ml->current }}"/>
<a href="about">about</a>


NB: I personally use route aliases (the 2nd way), as it gives more flexibility over the URL formats (translate URLs by language, adjust URLs for SEO, etc..).



v.

unread,
Nov 8, 2019, 3:05:11 AM11/8/19
to Fat-Free Framework
ok thanks guys.
I will dig into it some more.
For some reason my app will not accept @ml->current.
My index contains
Multilang::instance();

My Controller construct
$ml=Multilang::instance();
$this
->ml=$ml;

If I set the lang (or session.lang) in my controller's before route :
$this->f3->set('SESSION.lang', $this->ml->current);
it works, but for some reason I am unable to access the ml or this->ml in the template directly.

ved

unread,
Nov 8, 2019, 3:40:43 AM11/8/19
to Fat-Free Framework
Hi,

You'll need to add it as a regular F3 variable:

$f3->set('ML', $ml);

If you just use $this->ml = $ml on a controller, that will be assigned as a controller property and not as an F3 variable.

Cheers

v.

unread,
Nov 8, 2019, 5:50:22 AM11/8/19
to Fat-Free Framework
ah! I see, that makes sense....
Anyone else have this thing where your brain gets mushy and you fail to see the obvious after spending too much time in front of a computer screen?
Reply all
Reply to author
Forward
0 new messages