PHP code example of michalsn / codeigniter-gettext

1. Go to this page and download the library: Download michalsn/codeigniter-gettext library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michalsn / codeigniter-gettext example snippets




namespace Config;

use CodeIgniter\Config\AutoloadConfig;

class Autoload extends AutoloadConfig
{
    // ...
    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
        'Michalsn\CodeIgniterGettext' => APPPATH . 'ThirdParty/gettext/src',
    ];

    // ...



namespace Config;

use Michalsn\CodeIgniterGettext\Config\Gettext as BaseGettext;

class Gettext extends BaseGettext
{
    // Directory where translation files are stored
    public string $dir = APPPATH . 'Gettext' . DIRECTORY_SEPARATOR;

    // Default domain name (usually 'messages')
    public string $domain = 'messages';

    // List of allowed domains
    public array $allowedDomains = ['messages'];

    // Character encoding
    public string $codeset = 'UTF-8';

    // Locale mapping - maps CodeIgniter locales to system locales
    public array $locales = [
        'en' => 'en_US.utf8',
        'pl' => 'pl_PL.utf8',
        'de' => 'de_DE.utf8',
        'fr' => 'fr_FR.utf8',
    ];
}

public array $supportedLocales = ['en', 'pl', 'de', 'fr'];

// In your controller
service('gettext')->setLocale('pl');

echo _('Hello');  // Outputs: Cześć

// Set locale based on user preference
service('gettext')->setLocale('de');

echo _('Hello');  // Outputs: Hallo
echo _('Goodbye');  // Outputs: Auf Wiedersehen

service('gettext')->setLocale('de');

echo pgettext('verb', 'Post');      // "Veröffentlichen"
echo pgettext('noun', 'Post');      // "Beitrag"

// npgettext($context, $singular, $plural, $count)

$count = 3;
// Insert values with sprintf()
echo sprintf(npgettext('email', '%d message', '%d messages', intval($count)), $count);
// Email context: "3 messages"

echo sprintf(npgettext('chat', '%d message', '%d messages', intval($count)), $count);
// Chat context: "3 chats" or "3 texts"


// dpgettext($domain, $context, $message)

// Using different translation domains
echo dpgettext('admin', 'button', 'Delete');    // "Remove permanently"
echo dpgettext('frontend', 'button', 'Delete'); // "Move to trash"

// dnpgettext($domain, $context, $singular, $plural, $count)

$files = 5;
// Insert values with sprintf()
echo sprintf(dnpgettext('filesystem', 'trash', '%d file', '%d files', intval($files)), $files);
// Result: "5 files in trash"

echo sprintf(dnpgettext('filesystem', 'upload', '%d file', '%d files', intval($files)), $files);
// Result: "5 files uploaded"

// If you need to change both locale and domain
service('gettext')->setLocale('pl')->setDomain('messages');

echo _('Hello');

public array $allowedDomains = ['messages', 'errors', 'emails'];

service('gettext')->setLocale('pl')->setDomain('errors');
echo _('File not found');
bash
php spark gettext:publish

app/
├── Gettext/                  # Base directory for all gettext locales
│   ├── pl_PL/                # Polish locale
│   │   └── LC_MESSAGES/
│   │       ├── messages.po   # Editable source file
│   │       └── messages.mo   # Compiled binary file for PHP
│   │
│   └── de_DE/                # Example: German locale
│       └── LC_MESSAGES/
│           ├── messages.po
│           └── messages.mo
│
├── Controllers/
│   └── Home.php
├── Views/
│   └── welcome_message.php
└── ...
po
msgid ""
msgstr ""
"Project-Id-Version: MyApp 1.0\n"
"Language: pl_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "Hello"
msgstr "Cześć"

msgid "Goodbye"
msgstr "Do widzenia"