'How to load Codeigniter 4 lang file into an array variable
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.
Solution 1:[1]
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')
Solution 2:[2]
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);
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | marcogmonteiro |
Solution 2 | Osama Al-Theebah |