daycry/codeigniter-language

Load language files as array in Codeigniter 4

v1.0.2 2023-03-06 14:41 UTC

This package is auto-updated.

Last update: 2024-11-06 18:13:36 UTC


README

Language for Codeigniter 4

Send translation files to Javascript

Installation via composer

Use the package with composer install

> composer require daycry/codeigniter-language

Manual installation

Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Language namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Daycry\Language' => APPPATH .'ThirdParty/codeigniter-language/src',
];

Usage Loading Library

$language = new \Daycry\Language\Language( \Config\Services::request()->getLocale() );
$language->loadFile( 'Validation' );

var_dump( $language->getTraductions() );

Usage as a Service

$language = \Config\Services::language();
$language->loadFile( 'Validation' );

var_dump( $language->getTraductions() );

Usage In Views

Twig

Use the package with composer install

> composer require daycry/twig
$this->twig->addGlobal( 'traductions', $this->language->getTraductions() );
<script>
    var traductions = {{ traductions|json_encode|raw }};

    console.log( traductions['File']['string'] );
</script>

Codeigniter Views

echo view( 'some_view' );
<script>
    var traductions = <?php echo json_encode( $this->language->getTraductions() ); ?>;

    console.log( traductions['File']['string'] );
</script>