Skip to content

Commit 604c573

Browse files
author
Emmanuel Campait
committed
Initial release
0 parents  commit 604c573

7 files changed

Lines changed: 457 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
/composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 domProjects
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "domprojects/codeigniter4-localize",
3+
"description": "Locale management and URL localization filter for CodeIgniter 4 projects.",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"codeigniter4",
8+
"codeigniter",
9+
"localization",
10+
"locale",
11+
"language",
12+
"filter"
13+
],
14+
"homepage": "https://github.com/domProjects/codeigniter4-localize",
15+
"support": {
16+
"issues": "https://github.com/domProjects/codeigniter4-localize/issues",
17+
"source": "https://github.com/domProjects/codeigniter4-localize"
18+
},
19+
"authors": [
20+
{
21+
"name": "domProjects",
22+
"homepage": "https://github.com/domProjects",
23+
"role": "Maintainer"
24+
}
25+
],
26+
"require": {
27+
"php": "^8.2",
28+
"codeigniter4/framework": "^4.7.2"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"domProjects\\CodeIgniterLocalize\\": "src/"
33+
}
34+
},
35+
"extra": {
36+
"branch-alias": {
37+
"dev-main": "1.x-dev"
38+
}
39+
}
40+
}

src/Config/Localize.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace domProjects\CodeIgniterLocalize\Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class Localize extends BaseConfig
8+
{
9+
public bool $enabled = true;
10+
public bool $detectFromBrowser = true;
11+
public bool $storeInSession = true;
12+
public bool $storeInCookie = true;
13+
public string $sessionKey = 'locale';
14+
public string $cookieName = 'locale';
15+
public int $cookieExpire = 31536000;
16+
public bool $redirectRoot = true;
17+
public bool $redirectMissingLocale = true;
18+
public string $invalidLocaleBehavior = '404';
19+
20+
/**
21+
* @var list<string>
22+
*/
23+
public array $excluded = [
24+
'api/*',
25+
'assets/*',
26+
'favicon.ico',
27+
'robots.txt',
28+
'sitemap.xml',
29+
];
30+
}

src/Config/Registrar.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace domProjects\CodeIgniterLocalize\Config;
4+
5+
use domProjects\CodeIgniterLocalize\Filters\Localize;
6+
7+
class Registrar
8+
{
9+
/**
10+
* @return array<string, array<string, mixed>>
11+
*/
12+
public static function Filters(): array
13+
{
14+
return [
15+
'aliases' => [
16+
'localize' => Localize::class,
17+
],
18+
'filters' => [
19+
'localize' => [
20+
'before' => ['*'],
21+
],
22+
],
23+
];
24+
}
25+
}

0 commit comments

Comments
 (0)