How to load Codeigniter 4 lang file into an array variable

1.5k views Asked by At

Is there a way to load a file from Codeignier 4 Language directory and assign the array content to a variable. Instead of returning the translated line, I want the whole array.

Like in CI3 we could do $language = $this->lang->load('lang_file','english', TRUE);. But in CI4 I cant seem to find a workaround.

2

There are 2 answers

0
marcogmonteiro On

If your lang array has a parent you can just do it like this:

return [
    'list' => [
        'Apples',
        'Bananas',
        'Grapes',
        'Lemons',
        'Oranges',
        'Strawberries'
    ]
];


$foo = lang('Fruit.list');

If that is not your case you can just do like @Lawrence Cherone said in the comments and just include the file.

$language = include('./Language/english.php')
1
Osama Al-Theebah On

please use the below code to load the required language file as array

<?php
$locale = service('request')->getLocale();
$path = "Language/{$locale}/Validation.php";
$lang =  require APPPATH.$path; ;
var_dump($lang);
?>