Skip to content

Commit

Permalink
Added support for custom order (#406)
Browse files Browse the repository at this point in the history
* Added support for custom order

* Update Readme for custom order helper function
  • Loading branch information
sarotnem authored and Marc Cámara committed Feb 18, 2017
1 parent 8e01575 commit c27cb0d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -254,6 +254,23 @@ It returns a route, localized to the desired locale using the locale passed. If

This function will return all supported locales and their properties as an array.

### Get Supported Locales Custom Order

```php
/**
* Return an array of all supported Locales but in the order the user
* has specified in the config file. Useful for the language selector.
*
* @return array
*/
public function getLocalesOrder()

//Should be called like this:
{{ LaravelLocalization::getLocalesOrder() }}
```

This function will return all supported locales but in the order specified in the configuration file. You can use this function to print locales in the language selector.

### Get Supported Locales Keys

```php
Expand Down
25 changes: 23 additions & 2 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Expand Up @@ -385,6 +385,27 @@ public function getSupportedLocales()
return $locales;
}

/**
* Return an array of all supported Locales but in the order the user
* has specified in the config file. Useful for the language selector.
*
* @return array
*/
public function getLocalesOrder()
{
$locales = $this->getSupportedLocales();

$order = $this->configRepository->get('laravellocalization.localesOrder');

uksort($locales, function ($a, $b) use ($order) {
$pos_a = array_search($a, $order);
$pos_b = array_search($b, $order);
return $pos_a - $pos_b;
});

return $locales;
}

/**
* Returns current locale name.
*
Expand Down Expand Up @@ -423,9 +444,9 @@ public function getCurrentLocaleDirection()
case 'Mong':
case 'Tfng':
case 'Thaa':
return 'rtl';
return 'rtl';
default:
return 'ltr';
return 'ltr';
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/config/config.php
Expand Up @@ -309,4 +309,9 @@
//
'hideDefaultLocaleInURL' => false,

// If you want to display the locales in particular order in the language selector you should write the order here.
//CAUTION: Please consider using the appropriate locale code otherwise it will not work
//Example: 'localesOrder' => ['es','en'],
'localesOrder' => [],

];

0 comments on commit c27cb0d

Please sign in to comment.