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