0

So I am trying to show text in a select of a form in HTML depending of the language of the user, I am using Laravel and I have a simple way to do it:

Lang::get('texts.whatever');

That goes to texts.php file that changes 'whatever' for a proper text.

Besides, I have an associative array like this: $dropdown = ['1'=>'France','2'=>'Germany', '3'=>'Greece', '4'=>'Portugal', '5'=>'Spain', '6'=>'United Kingdom'];

And something like this:

@foreach($dropdown as $id=>$name)           
<?php
    $newArray[$id]=Lang::get('texts.'.$name);
?>
@endforeach

<?php
    $dropdown = $newArray;
?>

After that, I use $dropdown in my select of the form and it shows texts plus whatever it is in $name. It does not "get" the text in texts.php. However, if I change 'texts.'.$name to 'texts.yeah' (for example) it fills the select properly.

I tried eval(), exec(), etc. and I don't know what else to do. Maybe I am not using them right.

Any tips or advice? Thank you in advance.

1 Answer 1

1

How does your texts.php file look?

I made a little test case and it works fine for me

lang/en/countries.php

return array(
    'France' => 'France in english',
    'Germany'     => 'Germany in english',
    'Spain'     => 'Spain in english',
);

routes.php

Route::get('countries', function()
{
    $countries = [1 => 'France', 2 =>'Germany', 3 => 'Spain'];
    $dropdown = [];

    foreach ($countries as $key => $value) {
        $dropdown[$key] = Lang::get('countries.'.$value);
    }
    dd($dropdown);
});

returns:

array (size=3)
  1 => string 'France in english' (length=17)
  2 => string 'Germany in english' (length=18)
  3 => string 'Spain in english' (length=16)

Even more simple routes.php example

Route::get('countries', function()
{
    $countries = Lang::get('countries');
    $dropdown = [1 => $countries['France'], 2 => $countries['Germany'], 3 => $countries['Spain']];
    dd($dropdown);
});

returns:

array (size=3)
  1 => string 'France in english' (length=17)
  2 => string 'Germany in english' (length=18)
  3 => string 'Spain in english' (length=16)
Sign up to request clarification or add additional context in comments.

3 Comments

I have a file with ALL the text in the website, not just a 'countries.php' file, but that seems to work properly. Maybe I should give it a chance.
@Zariweya: Can you add your texts.php file in your question? Perhaps there is something different in that file.
Or if it's really big only a snippet with the countries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.