dev-master
9999999-dev https://github.com/davispuh/FuelPHP-LangExtended Lang class for FuelPHP
Unlicense
Wallogit.com
2017 © Pedro Peláez
Extended Lang class for FuelPHP
This extended Lang class supports localized language URLs, (*1)
http://domain/lang1/controller/method http://domain/lang2/controller/method
Only Lang class is changed and without introducing any side-effects thus making it fully backward compatible and keeping FuelPHP API same, but adding additional functionality., (*2)
You can install it with composer or just copy lang.php to your app/classes/, (*3)
then in your app/bootstrap.php add, (*4)
Autoloader::add_class('Lang', VENDORPATH . 'davispuh/fuelphp-lang/classes/lang.php');
or if you copied it then, (*5)
Autoloader::add_class('Lang', APPPATH . 'classes/lang.php');
To use this extended Lang class there are required few code changes, but nothing much., (*6)
Lang::localized($uri), eg. <a href="<?php echo Lang::localized('/blog/article/')?>"/>Article</a>
(Note: currently localized doesn't support schema or relative urls, and it expects that uri starts with / but it can be implemented if needed, now can do just 'https://domain'.Lang::localized('/blog/article/'))Lang::localized('/blog/article/','ru') or directly write <a href="/ru">RU</a>
config.php add available languages 'languages' => Array('en', 'ru')
routes.php config to also include localized routing.routes.php config changes explainedWill need routes for both cases when language is included in URI and when isn't.
So will need to add another _root_ ie. $routes[$langs] and all other routes prefixed with $routes[(({$langs})/)?route], (*7)
There's 4 possible configurations., (*8)
SEO friendly. Default language without any prefix. ie. http://domain/controller and all other languages http://domain/lang/controller. BUT http://domain/default_lang/controller returns 404, (*9)
config.php: 'language' => 'en' // specify which language will be served as default. without included in URL, (*10)
routes.php, (*11)
<?php
$languages = Config::get('languages');
unset($languages[0]);
$langs = implode('|', $languages);
return array(
'_root_' => 'welcome/index', // The default route
"({$langs})" => 'welcome/index', // The default route in other language
"(({$langs})/)?hello(/:name)?" => array('welcome/hello', 'name' => 'hello'),
);
Default language without any prefix. ie. http://domain/controller, BUT http://domain/default_lang/controller will still work., (*12)
everything same as previous, just without unset($languages[0]);, (*13)
All languages http://domain/lang/controller. BUT http://domain/controller will return 404. There's exception to _root_, it will give default language (language_fallback)., (*14)
config.php: 'language' => '' // no default language, MUST be in URL
'language_fallback' => 'en' // Fallback language if language isn't in URL, (*15)
routes.php, (*16)
<?php
$langs = implode('|', Config::get('languages'));
return array(
'_root_' => 'welcome/index', // The default route
"({$langs})" => 'welcome/index', // The default route in other language
"(({$langs})/)?hello(/:name)?" => array('welcome/hello', 'name' => 'hello'),
);
Make your own routes, unlimited possibilities. Maybe you don't have some article in that language? No problem, just give in default language or give page informing that it's not available., (*17)
This extended Lang class is implemented by me @davispuh, (*18)
Original FuelPHP Lang class is made by Fuel Development Team under MIT license, (*19)
All text, documentation, code and files in this repository are in public domain (including this text, README). It means you can copy, modify, distribute and include in your own work/code, even for commercial purposes, all without asking permission., (*20)
Feel free to improve anything what you see is improvable., (*21)
Warning: By sending pull request to this repository you dedicate any and all copyright interest in pull request (code files and all other) to the public domain. (files will be in public domain even if pull request doesn't get merged), (*22)
Also before sending pull request you acknowledge that you own all copyrights or have authorization to dedicate them to public domain., (*23)
If you don't want to dedicate code to public domain or if you're not allowed to (eg. you don't own required copyrights) then DON'T send pull request., (*24)
Extended Lang class for FuelPHP
Unlicense