Multilingual site with Slim
Hello!
At first, thank you for the framework!
Could you help to understand how to create a multilingual site with Slim?
For example, I want my site to have such languages: en
(default), de, ru.
Language identifier is presented in URL: http://example.com/ru/hello/
But the default language ('en' for this example) has not to be
presented in URL.
An URL http://example.com/hello/ means
English page, but http://example.com/en/hello/
means 404 error.
How it is better to achieve this?
I found that slim.before.dispatch hook, probably, may be used for
this. Is it the right direction?
Or maybe you can advice some other techniques to create multilingual site with Slim?
Thank you and good luck!
Sincerely,
Yury Plashenkov
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by Brian Nesbitt on 24 Jun, 2012 03:20 AM
My first thought would be to have the language always in the URL. I think this will in general simplify your template logic. You can check if the language exists and if not assume english but have all languages (even default english) generate URLs with the language in it.
I think the hook is a good idea and is my first instinct as well. I think you need to get higher up in the hook chain than
slim.before.dispatch
. I am thinkingslim.before.router
and potentially parse the lang out of the URI so your routes are kept simple.Let me try and whip something quick up and I'll re-post when I have something worth showing.
2 Posted by Yury on 24 Jun, 2012 04:27 AM
Brian, thank you very much!
Some example would be very helpful.
I.e. I must take URL value, detect language by language ID in it, then remove the language ID, and URL will go to routes already without language ID. Is that right?
3 Posted by Yury on 25 Jun, 2012 06:48 AM
I'm trying to detect the language in 'slim.before.router' hook, and that's not a problem.
But the problem is to remove the language ID from the URL to have the same routes for different languages. I do not know how I can do that and it is possible or not.
And if we will remove this language ID, then urlFor() or some similar functions (sorry, I have not learned this area yet) will return incorrect result. Am I right?
4 Posted by Yury on 25 Jun, 2012 08:48 AM
I have come to the following solution.
Created file
multilingual.php
with the following content:Then
require_once
it inindex.php
, fill the language list and callLang::detect_lang($app)
. After that all the routes which must be multilingual, prepend withLang::$in_url
.Here is the example of
index.php
.And it seems that this solution works well.
Hope this will be useful for somebody.
Please if you can advice or correct something I will be very grateful.
Sincerely,
Yury Plashenkov
5 Posted by Yury on 25 Jun, 2012 08:57 AM
Defining the locale is optional, as well as the 'default' value. If no language is marked as 'default', the default language will be the first language in array.
You can also set default language before
Lang::detect_lang($app)
:Support Staff 6 Posted by Brian Nesbitt on 26 Jun, 2012 01:13 AM
You got the idea of parsing the language from the PATH_INFO (ie. $app->request()->getResourceUri()) but not extracting it. This means you must keep the language in the route definition which I think you can get away from, even though its a variable.
I am working on a blog post on how I would approach this. I will be posting it tomorrow morning (EST) and I'll post a fully working 3 page application on github with a few enhancements. Hope it helps!
7 Posted by Yury on 26 Jun, 2012 04:24 AM
Dear Brian,
Does extracting language from URL mean removing it from there?
But how is it possible?
As far as I could see, when we write:
$app = new Slim()
, the first thing Slim does is gets request string by reading it from $_SERVER['REQUEST_URI']. And after that we cannot change its value. When Slim searches for a suitable route, it uses this value which I do not see the way to change (remove the language from it).Or maybe I simply do not understand your idea (because of level of my English) or do not understand Slim good enough :-)
Therefore I am looking forward to your blog post. It will help without doubt.
Support Staff 8 Posted by Brian Nesbitt on 26 Jun, 2012 12:34 PM
You can read in the environment PATH_INFO variable and modify it. As long as it is done before the router starts to process it.
Here is the blog posted I promised that should explain it all :-)
http://nesbot.com/2012/6/26/multilingual-site-using-slim
9 Posted by Yury on 26 Jun, 2012 06:36 PM
Dear Brian,
Thank you! I did not guess to subclass the Slim application and other classes. Looks nice and clean.
Brian Nesbitt closed this discussion on 29 Jun, 2012 01:03 PM.