|
| 1 | +# domProjects CodeIgniter 4 Localize |
| 2 | + |
| 3 | +Locale management and URL localization filter for CodeIgniter 4. |
| 4 | + |
| 5 | +This package adds a reusable localization filter that can: |
| 6 | + |
| 7 | +- detect the visitor locale |
| 8 | +- redirect root requests like `/` to `/{locale}` |
| 9 | +- redirect requests without locale prefix to `/{locale}/...` |
| 10 | +- persist the active locale in session and cookie |
| 11 | +- validate locale segments against `Config\App::$supportedLocales` |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require domprojects/codeigniter4-localize |
| 17 | +``` |
| 18 | + |
| 19 | +## How It Works |
| 20 | + |
| 21 | +The package registers a `localize` filter automatically through a `Registrar`. |
| 22 | + |
| 23 | +It does not modify `app/Config/Filters.php` manually. |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +The package uses two configuration sources: |
| 28 | + |
| 29 | +1. `Config\App` |
| 30 | +2. `domProjects\CodeIgniterLocalize\Config\Localize` |
| 31 | + |
| 32 | +You should configure your project locales in `app/Config/App.php`: |
| 33 | + |
| 34 | +```php |
| 35 | +public string $defaultLocale = 'en'; |
| 36 | +public array $supportedLocales = ['en', 'fr']; |
| 37 | +``` |
| 38 | + |
| 39 | +Optional package behavior can be customized by creating: |
| 40 | + |
| 41 | +```text |
| 42 | +app/Config/Localize.php |
| 43 | +``` |
| 44 | + |
| 45 | +Example: |
| 46 | + |
| 47 | +```php |
| 48 | +<?php |
| 49 | + |
| 50 | +namespace Config; |
| 51 | + |
| 52 | +use domProjects\CodeIgniterLocalize\Config\Localize as BaseLocalize; |
| 53 | + |
| 54 | +class Localize extends BaseLocalize |
| 55 | +{ |
| 56 | + public bool $redirectRoot = true; |
| 57 | + public bool $redirectMissingLocale = true; |
| 58 | + public string $invalidLocaleBehavior = '404'; |
| 59 | + |
| 60 | + public array $excluded = [ |
| 61 | + 'api/*', |
| 62 | + 'assets/*', |
| 63 | + 'favicon.ico', |
| 64 | + 'robots.txt', |
| 65 | + ]; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Recommended Behavior |
| 70 | + |
| 71 | +Locale priority: |
| 72 | + |
| 73 | +1. locale found in URL |
| 74 | +2. locale stored in session |
| 75 | +3. locale stored in cookie |
| 76 | +4. browser language |
| 77 | +5. `Config\App::$defaultLocale` |
| 78 | + |
| 79 | +Recommended request behavior: |
| 80 | + |
| 81 | +- `/` redirects to `/{locale}` |
| 82 | +- `/about` redirects to `/{locale}/about` |
| 83 | +- `/fr/about` applies `fr` |
| 84 | +- `/zz/about` returns `404` when `invalidLocaleBehavior = '404'` |
| 85 | +- assets and technical paths stay excluded from localization |
| 86 | + |
| 87 | +## Routes |
| 88 | + |
| 89 | +For locale-prefixed URLs, a route group like this is recommended: |
| 90 | + |
| 91 | +```php |
| 92 | +$routes->useSupportedLocalesOnly(true); |
| 93 | + |
| 94 | +$routes->group('{locale}', static function ($routes) { |
| 95 | + $routes->get('/', 'Home::index'); |
| 96 | + $routes->get('about', 'Pages::about'); |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +If your application does not use locale prefixes in routes, you can disable automatic redirection in the package config. |
| 101 | + |
| 102 | +## Package Structure |
| 103 | + |
| 104 | +```text |
| 105 | +src/ |
| 106 | + Config/ |
| 107 | + Localize.php |
| 108 | + Registrar.php |
| 109 | + Filters/ |
| 110 | + Localize.php |
| 111 | +``` |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +MIT |
0 commit comments