Download the PHP package michalsn/codeigniter-gettext without Composer
On this page you can find all versions of the php package michalsn/codeigniter-gettext. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download michalsn/codeigniter-gettext
More information about michalsn/codeigniter-gettext
Files in michalsn/codeigniter-gettext
Package codeigniter-gettext
Short Description Gettext support for the CodeIgniter 4 framework
License MIT
Homepage https://github.com/michalsn/codeigniter-gettext
Informations about the package codeigniter-gettext
CodeIgniter Gettext
This library gives users the ability to use gettext in a friendlier way.
Requirements
- PHP 8.2 or higher
- CodeIgniter 4.3 or higher
- PHP gettext extension enabled
Installation
Composer
composer require michalsn/codeigniter-gettext
Manually
In the example below we will assume, that files from this project will be located in app/ThirdParty/gettext directory.
Download this project and then enable it by editing the app/Config/Autoload.php file and adding the Michalsn\CodeIgniterGettext namespace to the $psr4 array, like in the below example:
Configuration
Run the publish command to create the configuration file:
This will create a Gettext.php config file in the app/Config/ folder with the following options:
Config Options
Important: Configure Supported Locales
You also need to configure supported locales in app/Config/App.php:
Folder structure
This is how your folder structure should look like.
Usage
Basic Example
Using with Different Locales
Using with context
Handling plural forms with context
Translates with both domain and context
Combines domain, context, and plural handling.
Setting Locale and Domain
Using Multiple Domains
If you want to organize translations into different domains (e.g., 'messages', 'errors', 'emails'):
-
Update
app/Config/Gettext.php: -
Create corresponding
.poand.mofiles: - Switch domains as needed:
Translation Workflow
1. Create Translation Files
Create a .po (Portable Object) file for each locale in the appropriate directory:
2. Edit .po Files
You can edit .po files manually or use tools like Poedit, which provides a user-friendly interface.
Example .po file structure:
3. Compile to .mo Files
After editing .po files, compile them to binary .mo (Machine Object) files for PHP to use.
Using msgfmt (Command Line)
Compile All Locales at Once
4. Restart Your Application
After compiling new translations, you may need to restart your web server or PHP-FPM to clear the gettext cache.
Troubleshooting
Translations Not Working
-
Check if gettext extension is installed:
-
Verify locale is installed on your system:
If not found, install the locale package for your OS.
-
Check locale format: Ensure your locale strings in
app/Config/Gettext.phpmatch your system's locale format. Common formats:- Linux:
pl_PL.utf8orpl_PL.UTF-8 - macOS:
pl_PL.UTF-8
- Linux:
-
Verify .mo files exist and are up-to-date: Ensure you've compiled
.pofiles to.mofiles after making changes. - Clear gettext cache: Restart your web server or PHP-FPM after updating translations.