Support the ongoing development of Laravel.io →
posted 9 years ago
Configuration
Last updated 1 year ago.
0
<?php
$lang  = [
    'en' => [
        'name' => 'English',
        'dir'  => 'ltr',
    ],
    'es' => [
        'name' => 'Spanish',
        'dir'  => 'ltr',
    ],
    'fr' => [
        'name' => 'French',
        'dir'  => 'ltr',
    ],
];
$lang2 = array_map(
    function ($key) use ($lang) {
        return [$key => $lang[$key]];
    },
    array_keys($lang)
);
print_r($lang2);

outputs ...

Array
(
    [0] => Array
        (
            [en] => Array
                (
                    [name] => English
                    [dir] => ltr
                )
        )
    [1] => Array
        (
            [es] => Array
                (
                    [name] => Spanish
                    [dir] => ltr
                )
        )
    [2] => Array
        (
            [fr] => Array
                (
                    [name] => French
                    [dir] => ltr
                )
        )
)

Hope that helps.

Last updated 9 years ago.
0

@RQuadling - Thanks for helping, but that actually wasn't what I wanted. In my original filters.php I have

App::before(function($request)
{
	$url = explode('.', $request->server('HTTP_HOST'));
	

    $langcode = $url[1];

    $languages = ['en','fr'];
 
    if ( in_array($langcode, $languages) ){
    	echo "yes";
        App::setLocale($langcode);
    }else{
    	echo "no";
    }

});

And this code allows me to be able to select what languages i'm using via the $languages array. So what I'm trying to do is create a php file that has a list of languages that I will be using, so instead of doing something like this

$languages = ['en','fr', 'es', 'ch', 'af', 'de', 'hr', 'ar', 'ru', 'mn', 'el', 'he', 'ja', 'pol',];

where if I have alot of languages to use it will start to look untidy. So all I would have to do is call it like this

$languages = Config::get('languages.supportedLang');

now the problem comes in around this line

if ( in_array($langcode, $languages) )

this line checks what the $langcode is and then it will have a look at $languages to see if it is in that array and if it is then it would do what it's suppose to do.

But $languages needs to be an array. Where the languages would look like this if I print_r $languages

Array
(
    [0] => en => Array
                (
                    [name] => English
                    [dir] => ltr
                )
    [1] => es => Array
                (
                    [name] => Spanish
                    [dir] => ltr
                )
    [2] => fr => Array
                (
                    [name] => French
                    [dir] => ltr
                )
)

Is this explanation better? I'm not sure how to word what I need.

Last updated 9 years ago.
0

Try replacing ...

if ( in_array($langcode, $languages) )

with ...

if ( in_array($langcode, array_keys($languages) ) )
0

Or use array_key_exists()

0

or isset($languages[$langcode])

0

Many cats were skinned.

0

@RQuadling - Yep that worked as I wanted. Thank you soooo much

0

I'd go with the isset().

0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiva shiva Joined 24 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.