Welcome Guest, Not a member yet? Register   Sign In
load a lang file into an array

(This post was last modified: 03-28-2017, 10:56 AM by muuucho.)


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:

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?

(This post was last modified: 04-04-2017, 01:24 AM by muuucho.)


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.


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:

$language_value lang('language_key'); 

Info here: Language documentation CI 3.x

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

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

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

(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

Theme © iAndrew 2016 - Forum software by © MyBB