This extension allows to use URLs that contain a language code like
/en/some/page
/de/some/page
http://www.example.com/en/some/page
http://www.example.com/de/some/page
Since 1.1.3 you can also configure friendlier language names if you want:
http://www.example.com/english/some/page
http://www.example.com/deutsch/some/page
The language code is automatically inserted into every URL created and read back on every request. No extra URL rules are required. For best user experience the language is also restored from session/cookie if the user returns to a URL without a language code.
Installation ¶
Extract the package to your protected/extensions
directory and rename
it to localeurls
. Then configure the components in your protected/config/main.php
:
<?php
return array(
// ...
'components' => array(
// ...
'urlManager' => array(
'class' => 'ext.localeurls.LocaleUrlManager',
// Advanced configuration with defaults (see below)
//'languageParam' => 'language',
),
'request' => array(
'class' => 'ext.localeurls.LocaleHttpRequest',
'languages' => array('en_us','en','de','fr'),
// Since version 1.1.3 you can also map url => language
// 'languages' => array(
// 'english' => 'en',
// 'deutsch' => 'de',
// 'fr',
// )
// Advanced configuration with defaults (see below)
//'persistLanguage' => true,
//'languageCookieLifetime' => 31536000,
//'redirectDefault' => false,
),
// ...
),
);
Please head over to the github project page for all the details.
Changelog ¶
1.1.6 ¶
- Fix issue where URLs that contained the language code a second time where messed up
1.1.5 ¶
- Fix issue where the query string was lost when switching back to default language
1.1.4 ¶
- Fix issue where you could not create URLs to reset to default language
1.1.3 ¶
- Add mapping feature.
- Add debug output under category
ext.localeurls
(only ifYII_DEBUG
is set)
1.1.2 ¶
- Preferred user language is detected from the HTTP headers now. To disable, set
detecLanguage
tofalse
in therequest
component.
1.1.1 ¶
- The parameter
defaultLanguage
was removed. You should configure the default language in your main application config instead. If you want to redirect to your default language, you can setredirectDefault
to true.
Languages with "- or _"
EDIT: Mike is right, the locales do work fine. Just need to add them first in the array. Sorry missed that note in the instructions.
Hey Mike,
I was using your extension on a project and noticed that languages with a "-" or "_" in them brings up a 404 error: "Unable to resolve the request -ca", this was attempting to use en-ca or en_ca.
I've added the supported languages to the config array/
'languages' => array('en_us','en','de','fr','en_ca','en_gb'),
Is there a preferred format to enter the supported languages?
Thanks for your help.
Re: Languages with "- or _"
It's no problem as long as you list e.g.
en_gb
anden_us
beforeen
.localurl
thank you for this extension, i tried to use it but i got this error; LanguageUrlManager only works with urlFormat 'path', what is that means?
Re: localurl
@rasha1489: It means exactly what it says :). You have to use URLs in path format. See here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url
folder directories
thank you, i follow the steps to create a multilanguage app using the localeurls extension,but still i need to know about folder directories. where to put the folders/files or do i have to rename them?
i put the folder under:
where is the problem, i still dont know. kindly help me
Re: folder directories
@rasha1489:
You'll find the answer you are looking for in the "File Translation" section of this page:
http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n
If it's still unclear please ask on the forum. This comment section is only for comments that are closely related to this extension. Your question is rather a general question about i18n.
problem url in modules
hi.thank you
module page http://localhost/news/fr/bankSystem/bankUser/index
change language=> module page http://localhost/news/ar/**bankSystem**/bankSystem/bankUser/index
help!!!!!!
problem url in modules
@morteza
Hmm. Could you do me a favour and open an issue over at github? Could you also explain what you mean by "change language"? Did you manually create an URL? If yes, how?
Change language
Hey, do you have an example code for changing the language? I don't really know how to do it, and keep the user on the same URL.
Re: Change Language
Have you checked the example widget on the github page?
https://github.com/mikehaertl/localeurls#how-to-switch-languages
@Mike
Missed that. I tried it, but here's my problem. I am creating a multilanguage app, and all my controllers have URL rules like :
en -> appname/controller-name/1-english-name/ other-lang -> appname/other-lang/other-lang-controller-name/1-other-lang-name/
So every controller name has a different version depending on the language, and the slug (the id + name combination which retrieves my model for view) is also custom and specified in each language as a URL rule.
Your widget is great, but it generates the link for switching to the other language like :
appname/other-lang/**controller-name/1-english-name/**
which gives a 404 error. So it adds the get language param, but it generates the link based on current defined URL rules .. which is normal for the way the widget was created. The URL rules are added dynamically, based on current language selection.
If you have any idea how I could modify this, that would be awesome. Thanks again.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.