• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get array lang variables

#1
Question 
Hello!

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

Thank you?
Reply

#2
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.
A reader lives a thousand lives before he dies. The man who never reads lives only one.
George R.R. Martin

Reply

#3
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.
Reply

#4
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.i...xample-127
Reply

#5
(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.i...xample-127

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

#6
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
Reply

#7
(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!
Reply

#8
(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
A reader lives a thousand lives before he dies. The man who never reads lives only one.
George R.R. Martin

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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