I'd like to require files in my index.php file and use the array set in those files. The tree structure from where to get the files is:
root/lang/nl/dashboard/default.php
root/lang/nl/dashboard/users.php
The code for those files is
default.php
// Default DASHBOARD language list
if (empty($lang) || !is_array($lang)){
$lang = [];
}
$lang = array_merge($lang, [
'DASH_LANG_DE' => 'Duits',
'DASH_LANG_FR' => 'Frans',
'DASH_LANG_GB' => 'Engels',
'DASH_LANG_NL' => 'Nederlands',
'DASH_SET_LANG' => 'Stel hier uw taal in'
]);
users.php
// Default USERS language list
if (empty($lang) || !is_array($lang)){
$lang = [];
}
$lang = array_merge($lang, [
'USERS_DEFAULT' => 'Gebruikers',
'USERS_CREATE_NEW' => 'Maak een nieuwe gebruiker aan',
'USERS_DELETE' => 'Delete de gebruiker',
'USERS_NAME' => 'Naam'
]);
index.php is placed in the root with the following code
define ('LANG','lang/');
function lang_files($language,$path,$file){
foreach ($file as $value) {
if(file_exists(''.LANG.$language.'/'. $path .'/'. $value .'.php')){
require_once ''.LANG.$language.'/'. $path .'/'. $value .'.php' ;
}
else{
echo ''.LANG.$language.'/'. $path .'/'. $value .'.php <b>does not exist!!</b>' ;echo "<br>";
}
}
}
$language = "nl";
$path = 'dashboard';
$file =['default',
'users'
];
lang_files($language,$path,$file);
echo $lang['DASH_SET_LANG'];
When I get this to work I will place the function as a method in my class file but first it has to work.
At this time I can not acces the array with
echo $lang['DASH_SET_LANG'];
When I require the files directly I have no issues.
When I echo out the require_once part in my function I can see that the path to my file is correct.
Does anybody have a clue where I'm going wrong?
Update
With the help I got from @vvondra I was able to figure out where I went wrong.
Below you will see the changed function. Minor change tho.
function lang_files($language,$path,$file){
$lang = [];
foreach ($file as $value) {
if(file_exists(''.LANG.$language.'/'. $path .'/'. $value .'.php')){
require_once ''.LANG.$language.'/'. $path .'/'. $value .'.php' ;
}
else{
echo ''.LANG.$language.'/'. $path .'/'. $value .'.php <b>does not exist!!</b>' ;echo "<br>";
}
}
return $lang;
}
Thank you @vvondra for putting me in the right direction.