CodeIgniter Forums
Get array lang variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Get array lang variables (/showthread.php?tid=75252)



Get array lang variables - mintwint - 01-17-2020

Hello!

Is there method to get an array lang file app/language/en/Test.php ?

Thank you?


RE: Get array lang variables - JNapolitanoIT - 01-17-2020

Could you please be more specific? Are you wanting to get all of the values of an array inside of a language file? Or are you trying to get a specific language string from an array?

If it is the latter, then all you need to do is create your language file and access its values using the lang() helper function. For example let's say you have a language file called app/Language/en/Test.php. Inside of this file, you have the following array:

Code:
return [
  'key_one' => 'Value One',
  'key_two' => 'Value Two
];

All you need to do to retrieve the values from this file is call the lang() function from within your application, like so. Remember, you call these in the form of lang('Filename.Key'):
Code:
echo lang('Test.key_one');
echo lang('Test.key_two');

If you are doing this inside of a module then you need to make sure it is found by the Autoload file in app/Config/Autoload.php by putting it inside of the PSR4 array like so:
Code:
$psr4 = [
  'LibraryNamespace' => 'path/to/library/root',
];

I hope this helps you achieve your goal and any other information you need can be found here.


RE: Get array lang variables - mintwint - 01-18-2020

First of all, thank you for your post!

I need to get the all array in file without user lang key.
I want to send an array of all keys to Vue frontend and use indexes to determine keys.


RE: Get array lang variables - littlej - 01-19-2020

Hello there !

Here is how you can achieve what you are asking:

PHP Code:
// Path to the language file, adapt it
$languageFilePath APPPATH 'Language/en/colors.php';

// Get the array, this is what you are asking
$languageFile = include $languageFilePath;

// And use it the way you want :-)
echo '<pre>';
print_r($languageFile);
echo 
'</pre>';
exit; 

Want to know how the magic happens ? :-D
Read more here: https://www.php.net/manual/en/function.include.php#example-127


RE: Get array lang variables - mintwint - 01-20-2020

(01-19-2020, 04:17 PM)littlej Wrote: Hello there !

Here is how you can achieve what you are asking:

PHP Code:
// Path to the language file, adapt it
$languageFilePath APPPATH 'Language/en/colors.php';

// Get the array, this is what you are asking
$languageFile = include $languageFilePath;

// And use it the way you want :-)
echo '<pre>';
print_r($languageFile);
echo 
'</pre>';
exit; 

Want to know how the magic happens ? :-D
Read more here: https://www.php.net/manual/en/function.include.php#example-127

Thank you! It,s work, but I was hoping I could use system API)


RE: Get array lang variables - JNapolitanoIT - 01-20-2020

You could try using nested arrays inside of your language files, as seen here.  You can then access the values like below.

Using the same example, let's assume you've got a file living inside of app/Language/en/Test.php:

Code:
return [
    'lang_array_1' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ],
    'lang_array_2' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ]

];

You can now access it like so:

Code:
return lang('Test.lang_array_1');
return lang('Test.lang_array_2')

To get the values inside of the lang_array's. This may seem a little tedious but it does what you'd like it to.

I have to agree on not using any require's and require_once's in your code and using the systems API instead as it's more consistent and full proof should anything change internally within the framework itself.

I hope that this helps Smile


RE: Get array lang variables - mintwint - 01-21-2020

(01-20-2020, 01:58 PM)JNapolitanoIT Wrote: You could try using nested arrays inside of your language files, as seen here.  You can then access the values like below.

Using the same example, let's assume you've got a file living inside of app/Language/en/Test.php:

Code:
return [
    'lang_array_1' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ],
    'lang_array_2' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ]

];

You can now access it like so:

Code:
return lang('Test.lang_array_1');
return lang('Test.lang_array_2')

To get the values inside of the lang_array's. This may seem a little tedious but it does what you'd like it to.

I have to agree on not using any require's and require_once's in your code and using the systems API instead as it's more consistent and full proof should anything change internally within the framework itself.

I hope that this helps Smile

Thank you!

 This is what I was looking for! Simple and out of the box!


RE: Get array lang variables - JNapolitanoIT - 01-21-2020

(01-21-2020, 08:09 AM)mintwint Wrote:
(01-20-2020, 01:58 PM)JNapolitanoIT Wrote: You could try using nested arrays inside of your language files, as seen here.  You can then access the values like below.

Using the same example, let's assume you've got a file living inside of app/Language/en/Test.php:

Code:
return [
    'lang_array_1' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ],
    'lang_array_2' => [
        'key_one' => 'Value One',
        'key_two' => 'Value Two
    ]

];

You can now access it like so:

Code:
return lang('Test.lang_array_1');
return lang('Test.lang_array_2')

To get the values inside of the lang_array's. This may seem a little tedious but it does what you'd like it to.

I have to agree on not using any require's and require_once's in your code and using the systems API instead as it's more consistent and full proof should anything change internally within the framework itself.

I hope that this helps Smile

Thank you!

 This is what I was looking for! Simple and out of the box!

That's awesome news! Congratulations and good luck in your coding adventures. Remember, if you need help there is always someone here to assist Smile