• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
load a lang file into an array

#1
Hi!

Anyone who knows how I can populate an array with key/values from a lang file WITHOUT loading any from system first? I tried this:  

PHP Code:
$this->lang->load('some_file_lang','english'); 


If I var_dump $this->lang->language I will see an 'array' starting with the key/values from the system files, and then the ones from some_file_lang above. The system key/values are already the docs says. 
Any way to get an array from my own file only?
Reply

#2
Workaround:

open the lang file
    $string = file_get_contents($file);
  $lang_file_rows = array();
  $lang_file_rows = preg_split("/\\r\\n|\\r|\\n/", $string);


Loop through the array of rows and check if the row begins with $lang....
if so you explode each of this lines with "=" and save it into another array like $lang_file_variables
But before that you need to trim the key and the value as follows:


Run a rtrim on the key element, then remove the frist 7 ( $lang[' ) and the last two charaters ( '] )
Then run a ltrim on the value element, them remove the first character ( ' )  and the two last ( '; )

Now you have created an array from a lang file.



Reply

#3
Why would you want to take an array and put it in a new array?

If the lang file is loaded and you know the key name al you need todo to get the value is

PHP Code:
$language_value $this->lang->line('language_key'); 

OR use the helper

PHP Code:
$this->load->helper('language');

$language_value lang('language_key'); 

Info here: Language documentation CI 3.x
Reply

#4
Because when you do it like that CodeIgniter has already loaded its own lang file from the System into that array. 
Reply

#5
I just noticed something, you can pass more than two arguments when loading a language file

If you pass TRUE as the third argument, the loaded array is returned. So you don't need to use that loop you created.

PHP Code:
$your_language_array $this->lang->load('some_file_lang','english'TRUE); 

$your_language_array should be populated with the key=>value pairs of the loaded language file
Reply

#6
Thanks Martin! That made it a lot more easy. +1
Reply

#7
(04-06-2017, 05:50 AM)muuucho Wrote: Thanks Martin! That made it a lot more easy. +1

You are welcome. I also keep learning new CI secrets when helping others out
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.